• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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%
 
ArunKumar Kandregula
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iam using Glass Fish application server.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if we want to call EJB from plain Java main class ?
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic