Standalone client with Glassfish V3
I’m kind of excited with all the functionality packed in JEE6. As you know, Glassfish v3 will be the reference implementation for JEE6 so I wanted to give it a try with the current project I working on. This project is currently being developed using Glassfish v2. It is the typical client-server architecture using a swing standalone client as the front-end.
If you have search for some examples on how to get an standalone client running with Glassfish v3, you should have probably already noticed that there is not such an example. Everywhere you look, you will find examples using JSF2, Servlets or other web based front-ends.
I tried to search for help via Google and all I found was this post at java.net. I think, that post is the main cause of the confusion, at least for me. On the post it is said that you should use the appserv-rt.jar from Glassfish V2 since back then that jar wasn’t provided with Glassfish v3. Additionally, the post says you should use the gf-client.jar shipped with Glassfish v3 because the required jndi.properties file was inside that jar. It was a lot of confusion because 1) back then (Glassfish v3 b55 I think), well, the gf-client.jar wasn’t shipped with any jndi.properties and 2) it wasn’t clear how using a jar from Glassfish v2 should work fine with a jar from Glassfish v3.
So, I installed Glassfish v3 and tried to work around those problems without success. A couple of weeks ago I noticed that the appser-rt.jar was shipped with the new build from Glassfish v3. I thought then that the standalone client was going to work. I was wrong. I got several kinds of exceptions: NamingExceptions, Corba related Exceptions, ClassNotFoundExceptions, etc etc. Until now.
As for Glassfish v3 build 71 I have successfully run an standalone client and I want to share it with you.
Actually, the post at java.net wasn’t all that wrong. All you have to do is put the appserv-rt.jar and gf-client.jar on the classpath of you client and that’s it. Fortunately this time, both jars are shipped with Glassfish v3. I tested it with a client running on the same host as the server, so I didn’t have to configure anything in the jndi properties.
You should find the appserv-rt.jar under {yourGlassfishV3Dir}/glassfish/lib and the gf-client.jar under {yourGlassfishV3Dir}/glassfish/modules
Getting the context is as straightforward as:
InitialContext ctx=new InitialContext();
Then just do the lookup:
MyRemoteInterface myInterface=(MyRemoteInterface) ctx.lookup("com.test.MyRemoteInterface");
And that’s it.
Now, if you want to try some new JEE6 features with your EJB, you will have to add javax.ejb.jar to your classpath in order to be able to use annotations such as @Singleton. The javax.ejb.jar is also to be found under {yourGlassfishV3Dir}/glassfish/modules
I would also like to test brand new features like CDI aka JSR-299 and Bean Validation. I haven’t figured out which jars I should add to the classpath but stay tuned for the next posts.
I hope this information is helpful for you. If you have any suggestion or question, please leave a comment.
Thanks for talking to time to try this out and to share your findings.
My understanding is that “appserv-rt.jar” is there for compatibility reasons with GFv2 and that you shouldn’t need it to run your example. “gf-client.jar” should be enough.
Thanks for the post
Thanks for your help, but I can not get it to work.
I use Eclipse Galileo and Glassfish V3 (74.2).
I successfully installed my EJB with @Remote interface on Glassfish and the console will appear:
Message ID: Portable JNDI names for EJB SimpleBeanImpl
Complete Message: [java:global/EX_SVR/SimpleBeanImpl!Antonio.ejb.SimpleBean, java:global/EX_SVR/SimpleBeanImpl]
I created a new Java Project ‘TestClient’, I added AppServ-rt.jar and gf-client.jar and I run ‘Run as application’ from Eclipse.
Test client source:
InitialContext ctx = new InitialContext ();
SimpleBean bean = (SimpleBean) ctx.lookup (“java:global/EX_SVR/SimpleBeanImpl”);
String result = bean.sayHello (“Bob”);
Error:
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: 645)
at javax.naming.InitialContext.getDefaultInitCtx (InitialContext.java: 288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx (InitialContext.java: 325)
at javax.naming.InitialContext.lookup (InitialContext.java: 392)
at antonio.client.TestClient.main (TestClient.java: 22)
Thanks for any help.
Regards.
Antonio
I can not understand, how you can write the statement
InitialContext ic = new InitialContext(), in a
standalone java program.
You can only write it inside the container, say servlet.
Pl clarify.
Sujatha,
I think that once you import all of the necessary jars, your application behaves like a container to some degree.
I’m looking for a quick way to determine if the connection is going to timeout (or to set the timeout period). Currently, connecting to an instance of glassfish with incorrect connection information or an instance that is down takes -forever- to timeout. Anyone know how to do that?
I’m having the same issue as Antonio P, any resolution?
try adding this
Properties props = new Properties();
props.setProperty(“java.naming.factory.initial”, “com.sun.enterprise.naming.SerialInitContextFactory”);
props.setProperty(“java.naming.factory.url.pkgs”, “com.sun.enterprise.naming”);
props.setProperty(“java.naming.factory.state”, “com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl”);
props.setProperty(“org.omg.CORBA.ORBInitialHost”, “127.0.0.1″);
props.setProperty(“org.omg.CORBA.ORBInitialPort”, “3700″);
InitialContext ctx = new InitialContext (props);
I still have the problem to get a standalone application running. the appserv-rt.jar has only 3KB in the lib-directory and the gf-client.jar wasn’t in the modules directory but in the lib-directory.
so i searched the internet an found a appserv-rt-3.1.jar with a size about 15MB. with this i get a classnotfound exception from some sun-logging-classes.
i’m very frustrated why this must be so complicated….
why has the appserv-rt.jar only 3KB ???
I should probably write a new post about the standalone client in Glassfish. In the latest version, 3.1, you need to copy about 35 jars. Yes you read it right. 35! If you want to find out which jars, you can take a look at the manifest file inside the gf-client.jar. I strongly recommend that you use another technology like hessian if you need the performance of a binary based protocol. Hessian provides that and the size of the hessian jar is about 400 KB.
As you can see the appserv-rt.jar ist almost empty, it contains just paths to other jars in the manifest.
In glasfish you can find the program appclient. Now you can run jour application using this program by:
../../../glassfish3/glassfish/bin/appclient -client HelloClient.jar
The problem is you have to have instaled thi whole glasfish on the client computer.
But you have the possibility to take only part of the glasfish to the client. Run first
(from glasfish)
package-appclient
il will produce the file
…glassfish3/glassfish/lib/appclient.jar
This is all you need.
Take it ad the client application to the client computer.
Copy the both to a directory.
unzip appclient.jar
Now yo can run your application (say HelloClient.jar) using
./appclient/glassfish/bin/appclient -client HelloClient.jar
This is all.
Did you wrote this new post about the standalone client in Glassfish 3.1??
Best regards?
Excellent post – this really helped me. Thanks.