CuriousMind
CuriousMind

Reputation: 8903

How to access EJB 2.0 using a standalone program

I am developing EJB 2.0 project using Eclipse and Jboss AS. The below are the code samples:

Component interface:

package com.example.ejb;

import javax.ejb.*;
import java.rmi.RemoteException;

public interface Advice extends EJBObject {

    public String getAdvice() throws RemoteException;
}

Home interface:

package com.example.ejb;

import java.rmi.RemoteException;

import javax.ejb.*;

public interface AdviceHome extends EJBHome {

    public Advice create() throws CreateException, RemoteException;
}

Bean implementation:

package com.example.ejb;

import java.rmi.RemoteException;

import javax.ejb.*;

public class AdviceBean implements SessionBean {


private static final long serialVersionUID = 1L;

private String[] adviceStrings = {"One word: inappropriate.", 
           "You mightwant to    rethink that haricut.", "Your boss will respect "};

    public void ejbPassivate() {

            System.out.println("ejb activate");
    }

    public void ejbRemove() {

            System.out.println("ejb remove");

    }

    public void setSessionContext(SessionContext ctx) {

            System.out.println("session context");

    }

    public String getAdvice() {

            System.out.println("in get advice");

            int random = (int) (Math.random() * adviceStrings.length);
            return adviceStrings[random];
    }

    public void ejbCreate() {

            System.out.println("in get create");
    }

    public void ejbActivate() throws EJBException, RemoteException {
        System.out.println("in ejbActivate");

    }
}

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 id="ejb-jar_ID">
<display-name>Ejb1</display-name>

<enterprise-beans>  
<session>
   <display-name>AdviceBean</display-name>
   <ejb-name>AdviceBean</ejb-name>
   <home>com.example.ejb.AdviceHome </home>
   <remote>com.example.ejb.Advice</remote>
   <ejb-class>com.example.ejb.AdviceBean</ejb-class>
   <session-type>Stateless</session-type>
   <transaction-type>Bean</transaction-type>
   <security-identity>
     <description></description>
     <use-caller-identity></use-caller-identity>
   </security-identity>
 </session>  
</enterprise-beans>
<ejb-client-jar>EJBProjectClient.jar</ejb-client-jar>
</ejb-jar>

I deployed this code in Jboss AS, and when i run this "in server", the below details i get in JBOSS start up logs for JNDI:

09:33:50,273 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-3) JNDI bindings for session bean named AdviceBean in deployment unit deployment "EJBProject1.jar" are as follows:

java:global/EJBProject1/AdviceBean!com.example.ejb.Advice
java:app/EJBProject1/AdviceBean!com.example.ejb.Advice
java:module/AdviceBean!com.example.ejb.Advice
java:jboss/exported/EJBProject1/AdviceBean!com.example.ejb.Advice
java:global/EJBProject1/AdviceBean!com.example.ejb.AdviceHome
java:app/EJBProject1/AdviceBean!com.example.ejb.AdviceHome
java:module/AdviceBean!com.example.ejb.AdviceHome
java:jboss/exported/EJBProject1/AdviceBean!com.example.ejb.AdviceHome

09:33:50,299 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named AdviceBean in deployment unit subdeployment "EJBProject.jar" of deployment "EnterpriseApplicationProject.ear" are as follows:

java:global/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.Advice
java:app/EJBProject/AdviceBean!com.example.ejb.Advice
java:module/AdviceBean!com.example.ejb.Advice
java:jboss/exported/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.Advice
java:global/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.AdviceHome
java:app/EJBProject/AdviceBean!com.example.ejb.AdviceHome
java:module/AdviceBean!com.example.ejb.AdviceHome
java:jboss/exported/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.AdviceHome

My client code which is trying to access the EJB is:

package com.example.localclient;

import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import javax.ejb.*;

import com.example.ejb.Advice;
import com.example.ejb.AdviceHome;

@SuppressWarnings("unused")
public class AdviceClient {

public static void main(String[] args) throws Exception {
    new AdviceClient().go();

}

public void go() throws Exception{

    Context ic = new InitialContext();
    Object o = ic.lookup("java:jboss/exported/EJBProject1/AdviceBean");
    AdviceHome home = (AdviceHome) PortableRemoteObject.narrow(o, AdviceHome.class);
    Advice advisor = home.create();
    System.out.println(advisor.getAdvice());
}

}

However when i run this standalone client i get the following exception:

Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344) at javax.naming.InitialContext.lookup(InitialContext.java:411) at com.example.localclient.AdviceClient.go(AdviceClient.java:24) at com.example.localclient.AdviceClient.main(AdviceClient.java:17)

It seems that it is not able to to find the JNDI name, i tried for various combinations of JNDI names that i saw while JBoss was getting started (that info is few paragraphs above).

I need some information, i did google, however still facing the same issue.

1) what is the JNDI name that i have to provide in the Standalone client?

2) Since client needs the client jar (which has interface info and other details), where can i see that? FYI in Jboss i saw the client jar as "EJBProjectClient.jar", however not able to physically locate where this files is in Workspace of eclipse.

Any help highly appreciated.

Thanks!

Upvotes: 1

Views: 1752

Answers (1)

M.E
M.E

Reputation: 998

Your client code is running in a different JVM other than the server's.

What you need is very simple. You need to modify your code to start the initial context with properties of the server and not local.

Take a look at this link here for more details on how to do this: http://docs.oracle.com/javase/jndi/tutorial/basics/prepare/initial.html

You'll need to change the following values according to your environment, provider URL and context factory.

  • The provider URL is where your server is with an IIOP port (refer to your server settings to get the port).
  • Check the server's documentations for the conetxt factory (sorry have no idea about exact values for JBoss).

Hope this helps.

Upvotes: 1

Related Questions