-
1. Re: Simple session bean
anonym365454 Apr 8, 2006 2:18 PM (in response to anonym365454)Please help me!
-
2. Re: Simple session bean
peterj Apr 10, 2006 12:07 PM (in response to anonym365454)This is the client code I use to access my stateless session bean:
public void main(String[] args) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL, "localhost:1099"); Context ctx = new InitialContext(env); Object obj = ctx.lookup("HelloRemote"); HelloRemoteHome home = (HelloRemoteHome)PortableRemoteObject.narrow(obj, HelloRemoteHome.class); HelloRemote hello = home.create(); System.out.println(hello.sayHello()); }
You will, of course, have to change the above code to conform with the names of the classes and methods associated with your session bean. -
3. Re: Simple session bean
anonym365454 Apr 10, 2006 12:46 PM (in response to anonym365454)First of all, thanks a lot for your answer!
I tried but I still get some errors (hopefully different ones!)
This is the error I get:
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.init(InitialContext.java:219)
at javax.naming.InitialContext.(InitialContext.java:195)
at tutorial.ejb.Client.main(Client.java:17)
Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:219)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:649)
... 4 more
Exception in thread "main"
And this is your code with my methods:
package tutorial.ejb;
import java.util.Hashtable;
import javax.rmi.PortableRemoteObject;
import javax.naming.Context;
import javax.naming.InitialContext;
import tutorial.interfaces.*;
public class Client {
public static void main(String[] args) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
Context ctx = new InitialContext(env);
Object obj = ctx.lookup("HelloRemote");
FiboHome home = (FiboHome)PortableRemoteObject.narrow(obj, FiboHome.class);
Fibo hello = home.create();
System.out.println(hello.compute(1)[2]);
}
}
Do you know where could my problem come from? -
4. Re: Simple session bean
anonym365454 Apr 10, 2006 12:48 PM (in response to anonym365454)Ok I corrected the "HelloRemote" by "Fibo" in my code but it still doesn't work...
-
5. Re: Simple session bean
peterj Apr 10, 2006 1:14 PM (in response to anonym365454)Did you include the client/jbossall-client.jar file in your classpath for your client?
-
6. Re: Simple session bean
anonym365454 Apr 11, 2006 5:20 AM (in response to anonym365454)First of all, thank you for your help!
I added the jbossall-client.jar but it still doesn't work...
I think that I am missing smg huge...
Let me explain what I want to do so that maybe you can lead me to the right solution:
What I want is simply to deploy an EJB and access it locally with my little client application. The statless EJB will just print out a simple "Helloworld"
I am using jboss Eclipse IDE, and I launched the creation of an EJB, and then added a business method that looks like this:
public void Affichage() {
// TODO Auto-generated method stub
System.out.println("Helloworld");
}
I launched the Xdoclet generation after entering all the information needed, and what I have next is the following:
-source
...-tutopack.ejb
.....+Hello.java
.....+HelloBean.java
.....+HelloHome.java
.....+Client.java
.....+ejb-jar.xml
.....+jboss.xml
+JRE System Library [JRE 5]
+javax.servlet.jar
+jboss-j2ee.jar
+JRE System Library [JDK 1.4 2005Q1]
+JUnit Libraries (Jboss-IDE)
+jbossall-client.jar
-Adrien.ear
-application.xml
-HelloBean.jar
-HelloTestClient.jar
-packaging-build.xml
-xdoclet-build.xml
My packaging looks like this:
HelloBean.jar
+/Tuto4/output/ejb-jar.xml -> META-INF
+/Tuto4/output/jboss.xml -> META-INF
+/Tuto4/application.xml -> META-INF
+/Tuto4/source/tutopack/ejb/Hello.java
+/Tuto4/source/tutopack/ejb/HelloBean.java
+/Tuto4/source/tutopack/ejb/HelloHome.java
HelloTestClient.jar
+/Tuto4/output/tutopack/ejb/Client.java
+/Tuto4/application.xml -> META-INF
Adrien.ear
+/Tuto4/HelloBean.jar
+/Tuto4/application.xml ->META-INF
I suspect a LOT of errors in that, but since I'm a total beginner to jBoss and EJB (never actually managed to see an EJB working...) I don't know where to look...
Once more, thank you very much for your support because I'm totally lost with that. I would really like to find a tutorial somewhere for total beginners in EJB, but I can't, everytime I find one it's running on a different server (not jboss) and I get confused. The only tutorial with jboss and Eclipse I found is using those servlets... -
7. Re: Simple session bean
peterj Apr 11, 2006 2:11 PM (in response to anonym365454)I really don't see anything specifically wrong based on the file information you posted. But, when you changed your classpath, you should have now gotten a different error message, but since you didn't post the new error, I can't tell what is still wrong (possibly you are not including your ejb jar file in the client classpath?).
I decided to post a full EJB. I am using JBoss 4.0.3SP1, Sun JVM 1.5.0_06, XDoclet 1.2.3 and Ant 1.6.5.
File: src/main/java/peter/HelloBean.javapackage peter; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; /** * @ejb.bean name="Hello" display-name="HelloBean" jndi-name="HelloRemote" * type="Stateless" transaction-type="Container" view-type="remote" */ public class HelloBean implements SessionBean { private SessionContext ctx; public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { this.ctx = ctx; } public void ejbRemove() throws EJBException, RemoteException {} public void ejbActivate() throws EJBException, RemoteException {} public void ejbPassivate() throws EJBException, RemoteException {} public void ejbCreate() throws CreateException {} /** * @ejb.interface-method view-type="remote" * @ejb.transaction type="Supports" */ public String sayHello() throws EJBException { return "You say good-bye, but I say hello."; } }
File: src/main/java/peter/Client.javapackage peter; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class Client { public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL, "localhost:1099"); Context ctx = new InitialContext(env); Object obj = ctx.lookup("HelloRemote"); HelloRemoteHome home = (HelloRemoteHome)PortableRemoteObject.narrow(obj, HelloRemoteHome.class); HelloRemote hello = home.create(); System.out.println(hello.sayHello()); } }
File: build.xml<project name="helloejb" default="all" basedir="."> <property environment="env" /> <property name="dir.java" location="src/main/java" /> <property name="dir.gen.java" location="target/gen/java" /> <property name="dir.gen.ejb" location="target/gen/ejb" /> <property name="dir.class" location="target/classes" /> <property name="dir.dist" location="target" /> <property name="jar.name" value="${ant.project.name}.jar" /> <property name="jar.path" location="${dir.dist}/${jar.name}" /> <path id="compile.classpath"> <fileset dir="${env.XDOCLET_HOME}/lib" includes="**/*.jar" /> <fileset dir="${env.JBOSS_HOME}/client" includes="**/*.jar" /> </path> <target name="all" depends="jar" /> <target name="clean"> <delete dir="target" includeemptydirs="true" /> </target> <target name="compile"> <mkdir dir="${dir.class}" /> <javac debug="true" srcdir="${dir.java}:${dir.gen.java}" destdir="${dir.class}"> <classpath refid="compile.classpath" /> </javac> </target> <target name="jar" depends="ejbdoclet, compile"> <mkdir dir="${dir.dist}" /> <jar destfile="${jar.path}"> <fileset dir="${dir.class}" /> <metainf dir="${dir.gen.ejb}" /> </jar> </target> <target name="ejbdoclet"> <taskdef name="ejbdoclet" classname="xdoclet.modules.ejb.EjbDocletTask" classpathref="compile.classpath" /> <mkdir dir="${dir.gen.java}" /> <mkdir dir="${dir.gen.ejb}" /> <ejbdoclet destdir="${dir.gen.java}" ejbspec="2.1"> <fileset dir="${dir.java}"> <include name="**/*Bean.java" /> </fileset> <remoteinterface pattern="{0}Remote" /> <homeinterface pattern="{0}RemoteHome" /> <deploymentdescriptor destdir="${dir.gen.ejb}" /> <jboss version="4.0" destdir="${dir.gen.ejb}" /> </ejbdoclet> </target> </project>
To use:
1) Create the above files
2) Set the XDOCLET_HOME and JBOSS_HOME environment variables
3) Build using Ant
4) Copy the ejbhello.jar file to JBOSS_HOME/server/default/deploy
5) Start JBoss
6) Run the client, as follows (assumes Windows, adjust for *nix):java -cp target\classes;%JBOSS_HOME%\client\jbossall-client.jar peter.Client