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!