0
votes

I am new to EJB. I tried an example from java_for_web_with_servlets_jsp_and_ejb book. The following code creates an session been called Adder which adds two integers:

Adder.java:

package com.brainysoftware.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

public interface Adder extends EJBObject{
    public int add(int a,int b) throws RemoteException;

}

AdderBean.java

package com.brainysoftware.ejb;

import java.rmi.RemoteException;

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

public class AdderBean implements SessionBean{
    public int add(int a, int b){
        System.out.println("From AdderBean");
        return (a+b);
    }

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

    }

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

    }

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

    }

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

    }
}

AdderHome.java

package com.brainysoftware.ejb;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface AdderHome  extends EJBHome{
    Adder create() throws RemoteException, CreateException;
}

The deployment descriptor is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
  <description>Your first EJB application</description>
    <display-name>Adder Application</display-name>
    <enterprise-beans>
     <session>
      <ejb-name>Adder</ejb-name>
      <home>com.brainysoftware.ejb.AdderHome</home>
      <remote>com.brainysoftware.ejb.Adder</remote>
      <ejb-class>com.brainysoftware.ejb.AdderBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Bean</transaction-type>
     </session>
    </enterprise-beans> 
 </ejb-jar> 
  • I created a jar file of this project and placed the jar in the tomcat's lib.
  • Now, for the client I created a dynamic web project having the class BeanClient.java that uses the Adder bean:

    import java.rmi.RemoteException; import java.util.Properties; import javax.ejb.CreateException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import com.brainysoftware.ejb.Adder; import com.brainysoftware.ejb.AdderHome; import javax.rmi.PortableRemoteObject;

    public class BeanClient { public static void main(String[] args){ Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(Context.PROVIDER_URL, "localhost:1099"); try{ InitialContext jnic = new InitialContext(props); System.out.println("Get context"); Object ref = jnic.lookup("Adder"); System.out.println("Got reference"); AdderHome home = (AdderHome) PortableRemoteObject.narrow(ref,AdderHome.class); Adder adder = home.create(); System.out.println("Adding 2 and 5:"+adder.add(2,5)); } catch(NamingException e){ System.out.println(e.toString()); } catch(CreateException e){ System.out.println(e.toString()); } catch (RemoteException e) { System.out.println(e.toString()); }
    } }

On executing this class in eclipse, I get the following error:

Get context 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]]]

Could you please help me out with this?

1

1 Answers

0
votes

to look up and ejb firstbale :

you must have an interface and it's implementation.

you have to annotate your interface (@Local ou @Remote) The implementation must be annotated @statless ou @statful You have to add on you project a jndi.properties. your ejb project must be deployed on your server to be easy to look up it. i have for you an example with jboss here : https://www.youtube.com/watch?v=mBwgHuBg96g

i hope that my answer is useful pour you