My Wiki

July 25, 2006

Lookup an EJB from a different application, deployed on the same server, in JBoss

Filed under: EJB, JBoss — jaikiran @ 7:57 am

Question:
I have a web application through which i want to access an EJB which is deployed as a separate appliaction on the same server. How do i do it?

Answer:

In the web.xml of your war(the web application through which you want to access the EJB), have the following entry:

<ejb-ref>
    <ejb-ref-name>GiveAnyNameByWhichYouWouldLikeToReferTheBeanInYourWebApp</ejb-ref-name>
    <ejb-ref-type>session</ejb-ref-type>
    <home>packageName.ClassNameOfTheHomeObjectOfTheBeanYouWantToRefer</home>
    <remote>packageName.ClassNameOfTheRemoteObjectOfTheBeanYouWantToRefer</remote>
  </ejb-ref>
 
  In the jboss-web.xml of your war, have the following entry:
 
  <ejb-ref>
    <ejb-ref-name>GiveAnyNameByWhichYouWouldLikeToReferTheBeanInYourWebApp(This should be same as the one given in the web.xml above)</ejb-ref-name>
    <jndi-name>TheJndiNametoWhichTheBeanIsBound(Example:somecontext/somejndiName)YouWillFindThisJndiNameInTheJboss.xmlOfTheEJB</jndi-name>
   </ejb-ref>
   
For more info, have a look at the dtds of web.xml (http://java.sun.com/dtd/web-app_2_3.dtd) and jboss-web.xml(http://www.jboss.org/j2ee/dtd)
 
  In your code, do the lookup as:
 
  Context ic = new InitialContext();
  Object ejbHome = ic.lookup(“java:comp/env/TheNameThatYouHadGivenInTheEJB-REF-NAMETagOfJbossWeb.xmlAbove”);

Here’s an example:

web.xml:
<ejb-ref>
    <ejb-ref-name>MyTestBean</ejb-ref-name>
    <ejb-ref-type>session</ejb-ref-type>
    <home>com.test.ejb.MyBeanHome</home>
    <remote>com.test.ejb.MyBeanRemote</remote>
  </ejb-ref>

jboss-web.xml:
 <ejb-ref>
  <ejb-ref-name>MyTestBean</ejb-ref-name>
  <jndi-name>myejb/test/MyTestBean</jndi-name>
 </ejb-ref>
 
Lookup code:

Context ic = new InitialContext();
  Object ejbHome = ic.lookup(“java:comp/env/MyTestBean”);
 

Advertisements

5 Comments »

  1. Thanks for the clear explanation

    Comment by siva — August 17, 2006 @ 5:08 pm

  2. Thanks for the clear explanation. Hope to learn more j2ee from your blogs.

    Comment by siva — August 17, 2006 @ 5:09 pm

  3. How can the same be acheived without using ejb-jar file but only using jdk1.5 annotations ?(like @EJB ? or anything else?)

    Thanks !

    Comment by Bala — October 20, 2006 @ 5:24 am

  4. Bala,
    I have just started off with a few examples in EJB3. It will take some time for me to understand how this can be done using annotations. I will provide an update once i get to know.

    -Jaikiran

    Comment by jaikiran — October 28, 2006 @ 6:16 am

  5. I would like to see a continuation of the topic

    Comment by Maximus — December 20, 2007 @ 8:52 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: