• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying access a remote EJB from a stand-alone, non-EE Java client

 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two laptops. Both are connected to the Internet via my home wi-fi router.

laptop1: testing Java Enterprise Edition 7 applications, using NetBeans 8 and GlassFish 4
laptop2: testing Java Standard Edition 7 applications, using Eclipse Mars

Here is what I did on laptop1:

- Packaged a simple EJB component and a library containing the EJB's remote interface into an EAR.
- Deployed it successfully on GlassFish.
- Packaged an enterprise application client and the same library containing the EJB's remote interface into a module and deployed it separately on GlassFish.
- Both applications (EAR and client module) were running fine and the client was able to invoke the ejb component's method using dependency injection (@EJB).

Here is what I am facing problems with while doing something involving both laptop1 and laptop2:
I am trying to create a stand-alone non-EE client on laptop2 in order to invoke methods on the remote EJB component residing on laptop1. I am following this document https://glassfish.java.net/docs/4.0/application-development-guide.pdf

The steps written in that document are below. Please see my questions embedded below.

STEP1
InitialContext ctx = new InitialContext();
BeanRemote bean = (BeanRemote) ctx.lookup("com.acme.BeanRemote");
It does not look like JNDI to me. Where is java:global in it? Is this really portable or GlassFish specific?

STEP2
Copy the as-install/lib/gf-client.jar file to the client machine and include it in the classpath on the client side. How do I do it and where do I find the classpath on the client side?

If there is no GlassFish Server installation on the client machine (duh!), you must also copy the as-install/modules directory to the client machine and maintain its directory structure relative to the as-install/lib/gf-client.jar file. Relative in the classpath?


STEP3
Set the following system properties for the JVM startup options:
-Dorg.omg.CORBA.ORBInitialHost=${ORBhost}
-Dorg.omg.CORBA.ORBInitialPort=${ORBport}

Here ORBhost is the GlassFish Server hostname and ORBport is the ORB port number (default is 3700 for the default server instance, named server).
How do I set the above startup options for the JVM on the client side?

STEP4
Make sure the etc/hosts file on the client machine maps the GlassFish Server hostname and external IP address. In this file on each laptop do I need to tag a fully qualified domain to the computer name and place it next to the IP assigned by the wi-fi network?
 
Tim Holloway
Bartender
Posts: 18414
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't done a remote EJB lookup in a long time and never with Glassfish, so I'm not too sure about the JNDI lookup string, but I can answer some of the other questions.

On Step 2, every remote client needs some sort of stub interface that bridges the gap to the server. In older EJBs, that was a complete set of classes generated as part of the build process, but I think that's changed a bit now. In any event, the jar in question should have been included with your Glassfish distro (or at least be readily available on the Glassfish site or via a public library repo such as maven.org. When running Java from the command line, the default classpath is only the current directory, so you'd need a "-classpath" option on the command line. Like so:


Note that the exact syntax of the "classpath" option varies depending on whether the client is running a Unix/Linux OS or Windows.


On Step 3, the "-D" directives also go on the command line. They define properties that the application (and its libraries) can retrieve via the Java System Properties interface. So, that now gives us:



On Step 4, an entry in /etc/hosts (or its Windows equivalent) is only required if your server host reference is via domain name. If your client references the host directly via IP address, you don't need a hosts entry. A name is preferable for the same reason that you'd prefer named constants in code over "magic numbers", but not essential.

The normal format for a Unix-style hosts entry looks like this:

That defines the simple hostname "fish" and its FQDN (fully-qualified Domain Name) hostname as both resolving to IP address 192.168.1.11. That's the common "best practice" form.
 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Tim, for the reply!

Do I have to run commands in step2 and step3 everytime I need to run the client?
 
Tim Holloway
Bartender
Posts: 18414
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you've copied the required files in Step 2, you shouldn't have to copy them again unless you erase them. However, any generated files need to be refreshed after a build, since otherwise the client-side stub and corresponding server-side code may not match up.

Step 3 is the command to run the client, so of necessity you have to include those runtime options every time. Java doesn't store them in any type of persistent storage.
 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done work with remote EJBs and GlassFish/Payara. I wrote an blog article about it an number of years ago: http://mjremijan.blogspot.com/2011/06/secure-ssl-ejb-communication-with.html. This article details GlassFish-to-GlassFish remote EJB communcation. It also details how to do the communication between the two serves over SSH (which was my requirement at the time and prompted the writing of the article) which you won't need. I've not done this with a stand-alone Java SE client, but my article could provide you with some information that you need, especially when it comes to JNDI names. Hope this helps somewhat.
 
Mohammad Ali Asgar
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks to both of you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!