This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes EJB3 Dependency Injection: How to write ApplicationClient injecting @EJB correctly ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3 Dependency Injection: How to write ApplicationClient injecting @EJB correctly ?" Watch "EJB3 Dependency Injection: How to write ApplicationClient injecting @EJB correctly ?" New topic
Author

EJB3 Dependency Injection: How to write ApplicationClient injecting @EJB correctly ?

ArunKumar Kandregula
Greenhorn

Joined: Jun 05, 2009
Posts: 10
Hello,

I have following client code :




How to inject remote reference to EJB into helloUser object ?

I am getting the infamous NullPointerException when I am executing >>java HelloUserClient.

I want to write portable code for client. In otehr words , I am intending to write an application client, rather than, stand alone client.

Please answer.

Arun Kumar Kandregula.

SCJP 1.4 98%
SCBCD 1.3 92%

Thank you, Arun Kumar Kandregula.
SCJP 1.4 98%, SCBCD 1.3 92%
ArunKumar Kandregula
Greenhorn

Joined: Jun 05, 2009
Posts: 10
Iam using Glass Fish application server.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

You will need a Application Client Container (ACC) which will invoke that class. You can't use it as a normal standalone java class because the container is responsible for injecting the resources. I guess Glassfish provides a ACC and their documentation might contain more details.


[My Blog] [JavaRanch Journal]
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
What if we want to call EJB from plain Java main class ?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

You can call a EJB from a plain java class using JNDI lookup. However injecting the bean in that plain java class won't work because there will be no container which processes this plain java file for injecting the resource.
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Does the plain java class must be run on the same PC which is the server is running ?

If we can do the lookup to JNDI server outside the PC which is the server is running, do we need the IP address of the server ?

Jeffry Kristianto Yanuar SCJP, SCJA, SCJD
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Jeffry Kristianto Yanuar wrote:Does the plain java class must be run on the same PC which is the server is running ?

You can lookup a EJB located on a remote server. The client need not be on the same server.

Jeffry Kristianto Yanuar wrote:
If we can do the lookup to JNDI server outside the PC which is the server is running, do we need the IP address of the server ?



The JNDI lookup usually looks like this:


In this case a file named jndi.properties is searched in the classpath to determine the "context" information which includes the server address.

You can even pass the server address in the code itself through a Properties parameter to the InitialContext



The java.lang.InitialContext javadocs has good information
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Thanks Jaikiran Pai !!!

What if the server's address is changed. Does it mean we have to change the jndi.properties configuration too ?

Jeffry Kristianto Yanuar SCJP, SCJA, SCJD
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Yes that would have to be changed too, unless you use a hostname and a DNS server which can take care of the changes.
ArunKumar Kandregula
Greenhorn

Joined: Jun 05, 2009
Posts: 10
.
Arun Kandregula
Greenhorn

Joined: Dec 19, 2005
Posts: 26
Thank you Jai for your reply.

After doing some research, I succeeded in injecting @EJB into application client as follows:

1. I created a jar with MANIFEST.MF ( containing entry for Main-Class ) , client class, Remote interface class

2. I used appclient tool of Glassfish and invoked the ApplicationClient.

3. Client ran successfully.


Thanks.
Arun Kumar Kandregula.
SCBCD 1.3 92%
SCJP 1.4 98%
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
If we deploy an EJB, what is its JNDI name if we don't specify the JNDI name ?

Jeffry Kristianto Yanuar SCJP, SCJA, SCJD
Arun Kandregula
Greenhorn

Joined: Dec 19, 2005
Posts: 26
Jeffry Kristianto Yanuar wrote:If we deploy an EJB, what is its JNDI name if we don't specify the JNDI name ?

Jeffry Kristianto Yanuar SCJP, SCJA, SCJD


Hi Jeffry,

By default, for GlassFish, JNDI name of an EJB is fully qualified name of the Remote bussiness interface.

In the above example, it would be "ejb3inaction.example.HelloUser".

JNDI names are not yet standardized in portable fasion by EJB spec yet. So even default JNDI names vary from app server to app server.

Thanks.
Arun.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Arun Kandregula wrote:
JNDI names are not yet standardized in portable fasion by EJB spec yet. So even default JNDI names vary from app server to app server.



Arun is right, the default JNDI names are app server specific. However, the EJB 3.1 spec will introduce a standard global JNDI naming scheme which will help standardize the default JNDI names.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB3 Dependency Injection: How to write ApplicationClient injecting @EJB correctly ?
 
Similar Threads
EJB simple client is not running
How to run Hello World example in Eclipse with Glassfish v3 as appserver
Why EJB running slow
Trouble in getting started with EJB3
Facing problem in dependecy injection of EJB 3.0 session bean