这几天学习了一下EJB,刚开始搭建环境,配置JBOSS服务器后,创建第一个无状态会话Bean( stateless Session bean)HelloWord,运行客户端连接时报异常:java.net.SocketTimeoutException: Receive timed out。
下面是我的ejb client:
package com.ejb.junit; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; import com.ejb.HelloWorldRemote; public class EJBTest { public static void main(String[] args) { Properties prop=new Properties(); prop.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); prop.setProperty("java.nameing.provider.url", "jnp://127.0.0.1:1099"); prop.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); // prop.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); // prop.setProperty(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // prop.setProperty(InitialContext.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); try { InitialContext ctx=new InitialContext(prop); HelloWorldRemote helloworld=(HelloWorldRemote)ctx.lookup("HelloWorld/remote"); System.out.println(helloworld.sayHello("World")); System.out.println(helloworld.sayHello("China")); } catch (NamingException e) { e.printStackTrace(); } } }
运行时异常堆栈如下:
javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out] at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1690) at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1812) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:695) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688) at javax.naming.InitialContext.lookup(InitialContext.java:392) at com.ejb.junit.EJBTest.main(EJBTest.java:28) Caused by: java.net.SocketTimeoutException: Receive timed out at java.net.PlainDatagramSocketImpl.receive0(Native Method) at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:145) at java.net.DatagramSocket.receive(DatagramSocket.java:725) at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1659) ... 5 more
网上找了好些方法都没法解法,最后在一个网站上看着直接用类的常量来代替,就是将上面绿字注释去掉,三行红字打上注释后,居然奇迹般的运行正常了。。真是让我费解,如果大家碰到这种问题,可以试下这个方法。我把类的常量打印出来,跟上面的是一样的,哎,想不明白,如果有知道问题答案或是有解决办法的高人的话,还请指点在下。。。