I'm studying EJB by reading the Mastering EJB2 available from
TheServerSide.com.
I followed the chapter in the book and created a Stateless
SessionBean. I put the jar into the deploy folder of jboss and notice
that the jar was loaded successfully by jboss.
My question is how to use the ejb in a java client application. What
should I set for the client app to make the following call work.
Properties props = System.getProperties();
Context ctx = new InitialContext(props);
Object obj = ctx.lookup("HelloHome");
The client application resides on the same computer as jboss. TIA!
Peter
> Greetings,
>
> I'm studying EJB by reading the Mastering EJB2 available from
> TheServerSide.com.
>
> I followed the chapter in the book and created a Stateless
> SessionBean. I put the jar into the deploy folder of jboss and notice
> that the jar was loaded successfully by jboss.
>
> My question is how to use the ejb in a java client application. What
> should I set for the client app to make the following call work.
>
>
> Properties props = System.getProperties();
> Context ctx = new InitialContext(props);
> Object obj = ctx.lookup("HelloHome");
Hi,
here is a code snippet from a current project:
Properties p = System.getProperties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url", "127.0.0.1:1099");
p.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
try {
//
// Get JNDI context
//
mJNDIContext = new javax.naming.InitialContext(p);
//
// Find home object
//
Object foo = mJNDIContext.lookup("FoobarEntity");
FoobarEntityHomeHome = (FoobarEntityHome)PortableRemoteObject.narrow(
foo, FoobarEntityHome.class);
System.out.println(" OK - have home interface");
}
catch (Exception __e){
System.out.println(" ARRGHH caught exception: " +
__e.getClass().getName());
throw new Error(__e.toString());
}
}
HTH
Andreas
--
/toe/ff
el@/treu/
f/uss.
de//
Thank you, Andreas.
I tried your code in my test project but got the following errors.
C:\JDK\bin\javaw.exe -classpath
C:\JDK\jre\lib\rt.jar;C:\J2EE\lib\j2ee.jar;C:\Documents and Settings\Peter
Wu\My Documents\My Projects\TestApp\bin examples.HelloClient
javax.naming.NoInitialContextException: Cannot instantiate class:
org.jnp.interfaces.NamingContextFactory. Root exception is
java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at
com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:45)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
at javax.naming.InitialContext.init(InitialContext.java:222)
at javax.naming.InitialContext.<init>(InitialContext.java:198)
at examples.HelloClient.main(HelloClient.java:35)
Exception in thread "main" Process terminated with exit code 1
If I debug the code, the exception occurs at the following line:
mJNDIContext = new javax.naming.InitialContext(p);
What do I need to add to my client application so that it can be aware of
the existence of JBoss? Thanks again! :)
--
Peter Wu
pet...@sunguru.com
You need the JBoss client stuff on the classpath as well. The jars are in
the client subdir of your JBoss installation. You probably don't need all of
them. Either check the docs or keep adding jars until it works. The JNDI
stuff is in jnp-client.jar.
HTH,
Michiel
Thanks a lot, Michiel!
I added jboss-client.jar to the classpath and my test client application
works perfectly! :)
Peter
Cool :)
Peter Wu wrote:
you should read a config file (.properties) containing the URL for the JNDI server location (usually
port 1099 for Jboss)
these are the properties I'm using (richelieu is the name of the machine
where the App server is):
java.naming.provider.url=richelieu:1099
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
Jerome