1 Reply Latest reply on Aug 26, 2008 8:38 AM by Rajat Bhardwaj

    error while running EJB from java client on JBOSS

    gagan narula Newbie

      Hi

      As i am new to EJB i have created a helloworld application in ejb which is working fine when i try to call it from servlet but when i try to invoke the same ejb from java client (i.e from diff jvm) on jboss i got the following error:

      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]]]
      at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1399)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:579)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
      at javax.naming.InitialContext.lookup(InitialContext.java:351)
      at com.gl.TestClient.main(TestClient.java:39)
      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]]
      at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:254)
      at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1370)
      ... 4 more
      Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused]
      at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:228)
      ... 5 more
      Caused by: java.net.ConnectException: Connection refused
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:519)
      at java.net.Socket.connect(Socket.java:469)
      at java.net.Socket.(Socket.java:366)
      at java.net.Socket.(Socket.java:266)
      at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:69)
      at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:62)
      at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:224)
      ... 5 more

      Following is my code:

      Home Interface:

      package com.gl;

      import javax.ejb.CreateException;

      public interface testHome extends EJBHome {
      String JNDI_NAME = "testBean";
      public test create()
      throws java.rmi.RemoteException,CreateException;
      }

      Remote Interface:

      package com.gl;

      import java.rmi.RemoteException;

      import javax.ejb.EJBObject;

      public interface test extends EJBObject {
      public String welcomeMessage() throws RemoteException;
      }

      Bean:

      package com.gl;

      import java.rmi.RemoteException;

      import javax.ejb.EJBException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;

      public class testbean implements SessionBean {

      public void ejbActivate() throws EJBException, RemoteException {
      // TODO Auto-generated method stub

      }

      public void ejbPassivate() throws EJBException, RemoteException {
      // TODO Auto-generated method stub

      }

      public void ejbRemove() throws EJBException, RemoteException {
      // TODO Auto-generated method stub

      }

      public void setSessionContext(SessionContext arg0) throws EJBException,
      RemoteException {
      // TODO Auto-generated method stub

      }

      public void ejbCreate(){}

      public String welcomeMessage(){
      return "Welcome to the World of EJB";
      }

      }

      ejb-jar.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

      <ejb-jar>
      <enterprise-beans>

      <ejb-name>testBean</ejb-name>
      com.gl.testHome
      com.gl.test
      <ejb-class>com.gl.testbean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>

      </enterprise-beans>
      </ejb-jar>

      jboss.xml:

      <?xml version='1.0' ?>
      <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">

      <enterprise-beans>

      <ejb-name>testBean</ejb-name>
      <jndi-name>testBean</jndi-name>

      </enterprise-beans>


      Client code:

      package com.gl;

      import java.util.Properties;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.rmi.PortableRemoteObject;

      public class TestClient {

      public static void main(String[] args) throws Exception{

      try{
      /* Properties props=new Properties();
      props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      */

      Properties props = new Properties();
      props.put(Context.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      props.put(Context.PROVIDER_URL, "localhost:1099");

      System.out.println("Properties ok");

      //env.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.HttpNamingContextFactory");
      //env.put(Context.PROVIDER_URL,"http://localhost:8080");
      //env.put(Context.SECURITY_PRINCIPAL, "");
      //env.put(Context.SECURITY_CREDENTIALS, "");


      Context ctx=new InitialContext(props);
      System.out.println("context ok");
      //testHome home = (testHome)ctx.lookup("testBean");
      Object obj = ctx.lookup ("testBean");
      System.out.println("ojb = " + obj);

      testHome ejbHome = (testHome)PortableRemoteObject.narrow(obj,testHome.class);
      test ejbObject = ejbHome.create();
      String message = ejbObject.welcomeMessage();
      System.out.println("home ok");

      System.out.println("remote ok");

      System.out.println(message);


      }

      catch(Exception e){e.printStackTrace();}

      }

      }

      I am able to successfully deployed my ejb on JBOSS but i m getting above error when i am trying to invoke ejb from java client.

      kindly suggest me something to solve this issue.

      Regards
      Gagan