我在EJB 3.0中使用Jboss 5创建了一个helloworld应用程序。当我尝试运行我的客户端类EjbClientApplication时,它会引发异常。
我经常遇到异常。我已经做了很多关于它的事情,但我找不到它,因为我是EJB的新手,请帮忙。
例外:
javax.naming.CommunicationException:无法获取连接 任何这些网址:localhost:1099并且发现失败并显示错误: javax.naming.CommunicationException:接收超时[Root exception 是java.net.SocketTimeoutException:接收超时] [Root 异常是javax.naming.CommunicationException:连接失败 到服务器localhost / 127.0.0.1:1099 [root例外是 javax.naming.ServiceUnavailableException:无法连接到服务器 localhost / 127.0.0.1:1099 [根异常是java.net.ConnectException: 拒绝连接:连接]]] org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1763)at at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:693)at at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)at at javax.naming.InitialContext.lookup(InitialContext.java:392)at com.hex.client.EjbClientApplication.main(EjbClientApplication.java:28) 引起:javax.naming.CommunicationException:连接失败 server localhost / 127.0.0.1:1099 [root例外是 javax.naming.ServiceUnavailableException:无法连接到服务器 localhost / 127.0.0.1:1099 [根异常是java.net.ConnectException: 连接被拒绝:连接]] org.jnp.interfaces.NamingContext.getServer(NamingContext.java:335)at at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1734)... 4更多引起:javax.naming.ServiceUnavailableException:失败 连接到服务器localhost / 127.0.0.1:1099 [根异常是 java.net.ConnectException:连接被拒绝:连接] at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:305)... 5更多引起:java.net.ConnectException:拒绝连接: 在java.net.PlainSocketImpl.socketConnect(Native Method)连接 java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)at java.net.Socket.connect(Socket.java:529)at org.jnp.interfaces.TimedSocketFactory.createSocket (TimedSocketFactory.java:97)at org.jnp.interfaces.TimedSocketFactory.createSocket (TimedSocketFactory.java:82)at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:301)... 另外5个
Jboss服务器端口127.0.0.1:10001,但是我试图点击这个网址127.0.0.1:1099是这个ryt?
服务器属性:
MyCode:这些代码包含在单个EJB项目中
RemoteInterfaceClass:
package com.hex.statelessbean;
import javax.ejb.Remote;
@Remote
public interface StatelessSessionBeanRemote {
public String displayMessage();
}
StatelessBean:
/**
* Session Bean implementation class StatelessSessionBean
*/
package com.hex.statelessbean;
import javax.ejb.Stateless;
@Stateless
public class StatelessSessionBean implements StatelessSessionBeanRemote {
/**
* Default constructor.
*/
public StatelessSessionBean() {
// TODO Auto-generated constructor stub
}
@Override
public String displayMessage() {
// TODO Auto-generated method stub
return "Hello world";
}
}
客户端:
package com.hex.client;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.hex.statelessbean.StatelessSessionBeanRemote;
public class EjbClientApplication {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming");
props.setProperty("java.naming.provider.url", "localhost:1099");
InitialContext ctx = new InitialContext(props);
StatelessSessionBeanRemote bean = (StatelessSessionBeanRemote) ctx
.lookup("StatelessSessionBean/remote");
System.out.println("Message from Bean :" + bean.displayMessage());
} catch (NamingException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
更改localhost:1099 to jnp://localhost:1099
和org.jboss.naming to org.jboss.naming:org.jnp.interfaces
并检查ejb的名称。使用注释@RemoteBinding
,您可以为ejb命名。你有端口偏移吗?
答案 1 :(得分:0)
Corrected Code:
**Interface class:**
package com.hex.statelessbean;
import javax.ejb.Remote;
@Remote
public interface StatelessSessionBeanRemote {
public String displayMessage();
}
**Stateless Bean class:**
package com.hex.statelessbean;
import javax.ejb.Stateless;
/**
* Session Bean implementation class StatelessSessionBean
*/
@Stateless
public class StatelessSessionBean implements StatelessSessionBeanRemote {
/**
* Default constructor.
*/
public StatelessSessionBean() {
// TODO Auto-generated constructor stub
System.out.println("************* Calling from Default constructor ******************");
}
@Override
public String displayMessage() {
// TODO Auto-generated method stub
System.out.println("************* Calling from displayMessage() ******************");
return "Hello world";
}
}
**Client class:**
package com.hex.client;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.hex.statelessbean.StatelessSessionBeanRemote;
public class EjbClientApplication {
/**
* @param args
* @throws NamingException
*/
public static void main(String[] args) {
try {
Properties properties = new Properties();
properties.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
properties.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
properties.setProperty("java.naming.provider.url",
"jnp://localhost:1099");
InitialContext ctx = new InitialContext(properties);
StatelessSessionBeanRemote bean = (StatelessSessionBeanRemote) ctx
.lookup("StatelessSessionBean/remote");
System.out.println("Message from Bean :" + bean.displayMessage());
} catch (NamingException e) {
e.printStackTrace();
}
}
}
**LookUp:**
Message from Bean :Hello world