File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Accessing a Remote EJB in Glassfish from Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Accessing a Remote EJB in Glassfish from Tomcat" Watch "Accessing a Remote EJB in Glassfish from Tomcat" New topic
Author

Accessing a Remote EJB in Glassfish from Tomcat

Daniel Jabonete
Ranch Hand

Joined: Apr 01, 2007
Posts: 78
Hi Ranchmates,

I am having some troubles in accessing a Remote EJB (version 3.0) in Glassfish v2.1 from Tomcat 5.

I’d already copied the following required jar below in %CATALINA_HOME\shared\lib:

- appserv-rt
- appserv-ext
- appserv-deployment-client
- javaee

My Servlet:


My SessionBean:


I deployed my web app in a war file.

As I’d follow the implementations in this link https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#nonJavaEEwebcontainerRemoteEJB

&

http://technology.amis.nl/blog/1368/connecting-to-an-ejb-30-remote-sessionbean-from-tomcat


Unfortunately, lots of errors appears (the usual error for this topic), naming, classNotFound, NoClassDefFoundError, etc…

Unless I’ve miss something important!

Your response is highly appreciated.

Thank you very much!

Daniel
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Daniel,

I would first check with the GlassFish folks to see if the resources you are using are really up to date. Then, I would post specific questions here.

Regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Hi,



Based on your configuration, the glassfish app server is on the same machine as the tomcat. If not, then please change it to the actual IP.

Given that all jars are present, then if the configuration of the host and port are incorrect then you would only get NamingException. Other exceptions would point to configuration and environment problems.

Regards.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
Daniel Jabonete
Ranch Hand

Joined: Apr 01, 2007
Posts: 78
Hi,

I am running my Glassfish and Tomcat in the same machine without any conflicts, host or ports. I’ve also tested it in a peer-2-peer environment without any issues, aside from the accessing the Remote EJB using Tomcat.

I’ve tested it via stand-alone client and it works, in a network environment, as well in the same machine (localhost/127.0.0.1), please see below:



With regards to the results of the test, it seems that I am missing something in the configurations for Tomcat 5.

As in my original post I’d included/copied the required jar files in %CATALINA_HOME%\shared\lib folder in my Tomcat.

I’ve checked the MANIFEST.MF of appserv-rt.jar, in which referring to the ff. jar below:

appserv-rt_ja.jar
appserv-rt_zh.jar
appserv-rt_fr.jar
appserv-rt_de.jar
appserv-rt_es.jar
appserv-rt_it.jar
appserv-rt_sw.jar
appserv-rt_ko.jar
appserv-rt_zh_TW.jar
appserv-rt_zh_CN.jar

I am not sure where this jar files are located.

As I’ve ran the client successfully which is specifically referring its classpath to the required jars, how can we do this in Tomcat.

Any help will be highly appreciated.

Thanks!

Daniel
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Hi,

Is it possible for you to post the stacktrace here?

Regards.
Daniel Jabonete
Ranch Hand

Joined: Apr 01, 2007
Posts: 78
Hi,

This is the latest stackTraceError for this issues, please see below:



By the way, the version of my Glassfish is "Sun GlassFish Enterprise Server v2.1 (9.1.1) (build b60e-fcs)", as I understand that there are bugs fixes related to this.

This is the latest promoted builds in https://glassfish.dev.java.net/public/downloadsindex.html.

So more or less my Glassfish is up to date.

Regards,

Daniel
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Hi,

The application failed to load the JAR file of the EJB. Please check whether the ejb-jar is in the classloader path of your web application (WEB-INF\lib).

Regards.
Daniel Jabonete
Ranch Hand

Joined: Apr 01, 2007
Posts: 78
I included the “interface class” in my war file (web app) in Tomcat and it works.

It seems that when accessing a Remote EJB deployed in Glassfish from Tomcat, the web application deployed in Tomcat need also the jar or classes/interfaces like the one deployed in Glassfish.

Is this only for Glassfish Application Server, or is this stated in the EJB 3.0 Specs?

This seems not portable in a sense that the “remote interface class” should be equal to each other when doing this set-up.

Thanks very much, I appreciate it.

Cheers!

Daniel
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I included the “interface class” in my war file (web app) in Tomcat and it works.


Yes, you need the interface to the EJB in order to connect to it and call it's business methods. This is how EJBs work. This is true whether the client is local or remote, and is common with all Java Application servers.

Keep in mind that there is a lot of EJB code that is generated by the application server, not you the programmer. So, the interface API on a remote client is not the same as the code that is in the application server.



This seems not portable in a sense that the “remote interface class” should be equal to each other when doing this set-up.


This sentence is confusing. "What" should be equal to each other when doing "what" set-up? Please explain.

The remote objects that enable client code to connect to the EJB are not required on the server. An EJB is never a client of itself. This does not make any sense.





Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Daniel,

Hmm...is there some confusion here?

All you should need from the EJB code is the business interfaces for the client to make the call through. Although simply copying over the interface classes to the WAR is fine unless it is within the same EAR as the EJB JAR (because the class would be loaded twice in the same class loader hierarchy, which is wrong), the best practice in this case would be to separate the interfaces out into a separate EJB client JAR (http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.jst.ejb.doc.user/topics/ceclientjars.html) and have both the WAR and EJB JAR reference the common JAR with the interfaces. Most IDEs support this, Eclipse certainly does...

What is the portability issue that you see? There is nothing all that different here than using any ordinary Java interface to any old API? What do you think the expected behavior should be?

Thanks,
Reza
Daniel Jabonete
Ranch Hand

Joined: Apr 01, 2007
Posts: 78
Hi Guys,

My apologies for the confusion, please let me clear the set-up I am illustrating.

Machine 1:
IP Address = 192.168.1.1
App Server = Glassfish (latest version)
Deployed EJB (ver. 3.0):


Machine 2:
IP Address = 192.168.1.2
Web Server = apache-tomcat-5.5.27
Deployed WAR file:


The SimpleBean:
In which this "interface" was deployed in Glassfish & Tomcat.


The Servlet in the Web App deployed in Tomcat.


Basically, as illustrated above, if the SimpleBean interface changes you need to apply those changes in machine 1 & 2.
We need to copy over the modified (if there are) SimpleBean.class in Glassfish (machine 1) and in Tomcat (machine 2).

So the client (particularly in web application) need the copy of the EJB in order to connect to a Remote Machine with the same EJB.

Now, is this a binding contract in accessing Remote EJB? Is it written in the specs?

Thanks very much, really appreciate your time and effort. If there are still some confusions, please let me know.

P.S.
I'd contacted the author of the blog from my first post, and he re-read his blog and he said that he failed to mention that the EJB needs to be included in Tomcat.

Cheers!

Daniel
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Daniel,

OK. As I mentioned, the best practice in your case would be to extract just the EJB remote interface to a separate JAR. The EJB spec calls this the EJB client JAR (as described in the link above). This jar would just contain some simple POJOs (for example DTOs passed as EJB parameters) and POJIs (for example, the business interfaces), no container generated stuff.

This common interface then needs to be referenced both by the client on Tomcat and the EJB JAR containing the EJB implementation. This way, there is no copying of source code. You simply have your build system generate the JAR ans place it as a dependency for both the client and the EJB jar. For example, Eclipse will help you with this, as will ANT, Maven, etc.

This make more sense? Or do you see a possibility of doing this better with existing Java semantics. modularization and class-loading?

Regards,
Reza
 
GeeCON Prague 2014
 
subject: Accessing a Remote EJB in Glassfish from Tomcat