环境
jboss jboss7.1.1
所使用的包 有 jboss/bin/client/jboss-client.jar
还有从网上找的jnpserver.jar
代码不部分很简单
HelloWorld 接口
Java code
public interface HelloWorld {
public String SayHello(String name);
}
HelloWorldBean
Java code
import com.jav.impl.HelloWorld;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote(HelloWorld.class)
public class HelloWorldBean implements HelloWorld {
@Override
public String SayHello(String name) {
// TODO Auto-generated method stub
return name+"Welcome Test";
}
}
Test代码
Java code
Properties props=new Properties();
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url","localhost:1099");
props.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
try
{
InitialContext context=new InitialContext(props);
HelloWorld hello=(HelloWorld)context.lookup("HelloWorldBean/remote");
System.out.println(hello.SayHello("Hello Kitty"));
}
catch(NamingException e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
部署上面的HelloWorld 和HelloWorldBean 的jar包之后
java:global/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:app/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:module/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:jboss/exported/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:global/Hello/HellWorldBean
java:app/Hello/HellWorldBean
java:module/Hello/HellWorldBean
启动之后出现上面的代码
但是我Test类做测试的时候还是报出异常,但是我用jboss-6.1.0.Final 做测试的同样的代码没有错误,问题应该还是在jboss7上
错误时
javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1452)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:597)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:590)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.jav.test.Test.main(Test.java:20)
Caused by: javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:272)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1423)
... 4 more
Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:246)
... 5 more