2017-10-11 16 views
0

我在EJB 3.0中用Jboss 5创建了一个helloworld应用程序。当我尝试运行我的客户端类EjbClientApplication时,它抛出异常。在运行客户端类的EJB项目中发生CommunicationException:无法获得与任何这些URL的连接:localhost:1099

我经常得到下面的异常。我已经做了很多工作,但是我无法找到它,因为我是EJB新手请帮助。

例外:

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/127.0.0.1:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]] at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1763) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:693) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686) at javax.naming.InitialContext.lookup(InitialContext.java:392) at com.hex.client.EjbClientApplication.main(EjbClientApplication.java:28) Caused by: javax.naming.CommunicationException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect]] at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:335) at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1734) ... 4 more Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect] at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:305) ... 5 more Caused by: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at 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 more

JBoss服务器是了端口127.0.0.1:10001但我试图打这个网址127.0.0.1:1099是这个RYT?

服务器属性:

  • 地址:127.0.0.1
  • 端口:8080
  • JNDI端口:1099

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

更改localhost:1099 to jnp://localhost:1099org.jboss.naming to org.jboss.naming:org.jnp.interfaces并检查ejb的名称。使用注释@RemoteBinding可以给出ejb的名称。你有端口偏移吗?

+0

我已经做了更改,但即使我得到相同的异常。 –

+0

@JayanthiM你的服务器配置中的JNDI端口是什么?您是否在服务器日志或JMX控制台中看到了您的EJB?您可以尝试将java.naming.factory.url.pkgs值更改为org.jboss.naming.client吗? – awagenhoffer

+0

在服务器配置中,JNDI端口是1099,端口是8080,并且我已将值更改为org.jboss.naming:org.jnp.interfaces,但我经常收到此异常。 javax.naming.CommunicationException:无法获得连接到任何这些URL:localhost:1099和发现失败,错误:javax.naming.CommunicationException:接收超时[根异常是java.net.SocketTimeoutException:接收超时] –

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 
相关问题