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”);
 

July 24, 2006

How to create a Queue/Topic in JBoss?

Filed under: JBoss, JMS — jaikiran @ 9:09 am

Queues/Topics are known as administered objects and in jboss they can be deployed through ServiceDotXml files. Let’s name our file, myapp-destination-service.xml. The contents of the file should look like:

<?xml version=”1.0″ encoding=”UTF-8″?>

<server>
   <!–My queue, named: myAppQueue –>
  <mbean code=”org.jboss.mq.server.jmx.Queue”
  name=”jboss.mq.destination:service=Queue,name=myAppQueue“>
    <depends optional-attribute-name=”DestinationManager”>jboss.mq:service=DestinationManager</depends>
  </mbean>
  
 <!– My topic, named: myAppTopic –>
  <mbean code=”org.jboss.mq.server.jmx.Topic”
  name=”jboss.mq.destination:service=Topic,name=myAppTopic“>
    <depends optional-attribute-name=”DestinationManager”>jboss.mq:service=DestinationManager</depends>
  </mbean>
  
    
  </server>

 Place this file in your %JBOSS_HOME%/server/default/deploy directory and start the server.(Note: If you are using the ‘all’ configuration or the ‘minimal’ configuration then you will have to place this file in the %JBOSS_HOME%/server/all/deploy  or %JBOSS_HOME%/server/minimal/deploy   directory, as appropriate).

The above queue/topic will be bound to the jndi name: queue/myAppQueue and topic/myAppTopic respectively.

Note:

1) The above file is meant for JBoss-3.2.3. Though there might be slight changes to later versions of JBoss, the basic configuration mechanism remains the same.

2) JBoss also comes with preconfigured queues/topics. These configurations can be found in the jbossmq-destinations-service.xml file which is present in %JBOSS_HOME%/server/default/deploy/jms directory

Create a free website or blog at WordPress.com.