Friday 11 November 2011

Configure JMS Resources and create Message Driven Bean in Websphere 7 (Step 4)

Step 4: Send a queue JMS message

4.1 Access the JMS resources in EJB

4.1.1 Access the JMS resources through Context Lookup

In ibm-ejb-jar-bnd.xml under META-INF


<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-bnd
        xmlns="http://websphere.ibm.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee 
        http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd"
        version="1.0">
    <session name="jmsService">
        <resource-ref name="jms/MyQueueConnectionFactory" 
            binding-name="jms/QueueConnectionFactory">
        </resource-ref>
        <message-destination-ref name="jms/MyQueue" 
            binding-name="jms/Queue"/>
    </session>
</ejb-jar-bnd>

Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 2 and Step 3.

In ejb-jar.xml under META-INF

<session>
    <ejb-name>jmsService</ejb-name>
    <resource-ref>
        <res-ref-name>jms/MyQueueConnectionFactory</res-ref-name>
            <res-type>javax.jms.QueueConnectionFactory</res-type>
            <res-auth>Container</res-auth>
            <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <message-destination-ref>
        <message-destination-ref-name>
            jms/MyQueue
        </message-destination-ref-name>
        <message-destination-type>
            javax.jms.Queue
        </message-destination-type>
        <message-destination-usage>
            ConsumesProduces
        </message-destination-usage>
        <message-destination-link>
            jms/Queue
        </message-destination-link>
    </message-destination-ref>
</session> 

Note: The value of <res-ref-name> and <message-destination-ref-name> (jms/MyQueueConnectionFactory and jms/MyQueue) should match the name attribute of < resource-ref> and <message-destination-ref> element respectively in ibm-ejb-jar-bnd.xml.

Create a stateless session bean

@Stateless(name="jmsService")
@Local(JMSService.class)
public class JMSServiceBean implements JMSService {

    @Override
    public void sendMessage() {
        try{
            Context ctx = new InitialContext();
            QueueConnectionFactory cf = 
                (QueueConnectionFactory)
                ctx.lookup
                ("java:comp/env/jms/MyQueueConnectionFactory");
            Queue dest = 
                (Queue)ctx.lookup("java:comp/env/jms/MyQueue");
            QueueConnection connection = cf.createQueueConnection();
            QueueSession session = 
                connection.createQueueSession
                (false, javax.jms.Session.AUTO_ACKNOWLEDGE);
            QueueSender queueSender = session.createSender(dest);
            TextMessage message = session.createTextMessage();
            message.setText("Hello World");
            queueSender.send(message);
            System.out.println("Message Sent");
        }catch (Exception e) {
            throw new EJBException(e);
        }
    }
}

Note: The lookup string (jms/MyQueueConnectionFactory and jms/MyQueue) should match the value of <res-ref-name> and <message-destination-ref-name> element respectively in ejb-jar.xml.

4.1.2 Access the JMS resources through Dependency Injection

In ibm-ejb-jar-bnd.xml under META-INF, same as 4.1.1

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-bnd
        xmlns="http://websphere.ibm.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee 
        http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd"
        version="1.0">
    <session name="jmsService">
        <resource-ref name="jms/MyQueueConnectionFactory" 
            binding-name="jms/QueueConnectionFactory">
        </resource-ref>
        <message-destination-ref name="jms/MyQueue" 
            binding-name="jms/Queue"/>
    </session>
</ejb-jar-bnd>

Note: The binding-name attribute of and element should match the jndi name specified in Step 2 and Step 3.

No changes needed in ejb-jar.xml

Create a stateless session bean


@Stateless(name = "jmsService")
@Local(JMSService.class)
public class JMSServiceBean implements JMSService {

    @Resource(name = "jms/MyQueueConnectionFactory")
    private QueueConnectionFactory qcf;

    @Resource(name = "jms/MyQueue")
    private Queue queue;

    @Override
    public void sendMessage() {
        try {
            QueueConnection connection = qcf.createQueueConnection();
            QueueSession session 
                = connection.createQueueSession(false,
                    QueueSession.AUTO_ACKNOWLEDGE);
            QueueSender queueSender = session.createSender(queue);
            TextMessage message = session.createTextMessage();
            message.setText("Hello World");
            queueSender.send(message);
        } catch (JMSException e) {
            throw new EJBException(e);
        }
        System.out.println("Message Sent");
    } 
}

Note: the name attribute of @Resource annotation should match name attribute of <resource-ref> and <message-destination-ref> element, not the binding-name attribute.

4.1.3 Write a servlet to test

public class JMSSenderServlet extends HttpServlet {

    @EJB(beanName="jmsService")
    private JMSService jmsService;
    
    @Override
    protected void doGet(HttpServletRequest req, 
            HttpServletResponse resp)
            throws ServletException, IOException {
        jmsService.sendMessage();
    }
}

The result:

[25/08/11 14:07:40:109 EST] 0000001a SystemOut O Message Sent

4.2 Access the JMS resources in servlet

4.2.1 Access the JMS resources through Context Lookup

In ibm-web-bnd.xml under WEB-INFO


<?xml version="1.0" encoding="UTF-8"?>
<web-bnd 
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee 
    http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
    version="1.0">

    <virtual-host name="default_host" />
    <resource-ref name="jms/ConnectionFactory" 
            binding-name="jms/QueueConnectionFactory" />
    <message-destination-ref name="jms/Queue" 
            binding-name="jms/Queue"/>
</web-bnd>

Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 2 and Step 3.

In web.xml under WEB-INFO

<resource-ref>
    <res-ref-name>jms/MyQueueConnectionFactory</res-ref-name>
        <res-type>javax.jms.QueueConnectionFactory</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
    
<message-destination-ref>
    <message-destination-ref-name>
        jms/MyQueue
    </message-destination-ref-name>
    <message-destination-type>
        javax.jms.Queue
    </message-destination-type>
    <message-destination-usage>
        ConsumesProduces
    </message-destination-usage>
</message-destination-ref>

Note: The value of <res-ref-name> and <message-destination-ref-name> (jms/MyQueueConnectionFactory and jms/MyQueue) should match the name attribute of < resource-ref> and <message-destination-ref> element respectively in ibm-web-bnd.xml.

Create the servlet

public class JMSQueueSenderServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest req, 
            HttpServletResponse resp)
            throws ServletException, IOException {
        
        try{
            Context ctx = new InitialContext();
            QueueConnectionFactory qcf = 
                (QueueConnectionFactory)
                ctx.lookup
                ("java:comp/env/jms/MyQueueConnectionFactory");
            Queue queue = 
                (Queue)ctx.lookup("java:comp/env/jms/MyQueue");
            QueueConnection connection = qcf.createQueueConnection();
            QueueSession session = 
                connection.createQueueSession
                (false, QueueSession.AUTO_ACKNOWLEDGE);
            QueueSender queueSender = session.createSender(queue);
            TextMessage message = session.createTextMessage();
            message.setText("Hello World");
            queueSender.send(message);
            System.out.println("Message Sent");
        }catch (Exception e) {
            throw new EJBException(e);
        }
    }
}

4.2.2 Access the JMS resources through Dependency Injection

In ibm-web-bnd.xml under META-INF, same as 4.2.1

<?xml version="1.0" encoding="UTF-8"?>
<web-bnd 
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee 
    http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
    version="1.0">

    <virtual-host name="default_host" />
    <resource-ref name="jms/ConnectionFactory" 
            binding-name="jms/QueueConnectionFactory" />
    <message-destination-ref name="jms/Queue" 
            binding-name="jms/Queue"/>
</web-bnd>

No changes in web.xml

Create the servlet

public class JMSQueueSenderServlet extends HttpServlet {

    @Resource(name = "jms/MyQueueConnectionFactory")
    private QueueConnectionFactory qcf;
    
    @Resource(name = "jms/MyQueue")
    private Queue queue;
    
    protected void doGet(HttpServletRequest req, 
            HttpServletResponse resp)
            throws ServletException, IOException {
        try {
            QueueConnection connection = qcf.createQueueConnection();
            QueueSession session 
                = connection.createQueueSession(false,
                    QueueSession.AUTO_ACKNOWLEDGE);
            QueueSender queueSender = session.createSender(queue);
            TextMessage message = session.createTextMessage();
            message.setText("Hello World");
            queueSender.send(message);
        } catch (JMSException e) {
            throw new EJBException(e);
        }
        System.out.println("Message Sent");
    }

}

Note: the name attribute of @Resource annotation should match name attribute of <resource-ref> and <message-destination-ref> element respectively in ibm-web-bnd.xml, not the binding-name attribute.

The result:

[25/08/11 14:20:55:218 EST] 00000020 SystemOut O Message Sent

1 comment:

  1. Thanks so much for this example - it helped and worked, I was hoping IBM would contain a simple step-through example like this, but we have to rely on each other.

    ReplyDelete