Show TOC

Background documentationExample Locate this document in the navigation structure

 

This is an example of a servlet that sends messages to a queue destination. The SendingServlet servlet sends messages to the MyServletQueue queue. Instead of a JNDI lookup, the resources are injected using the @Resource annotation. The attribute mappedName configures the connection factory and the destination you use to send the messages. In this example, the connection factory is MyServletQueueConnectionFactory and the destination is MyServletQueue. After specifying the connection factory and the destination using the @Resource annotation, you have to create the respective variables to assign the injected resources.

Syntax Syntax

  1. package com.sap.test.jms;
    
    
    
    import java.io.IOException;
    
    import java.io.PrintWriter;
    
    import javax.annotation.Resource;
    
    import javax.jms.JMSException;
    
    import javax.jms.Queue;
    
    import javax.jms.QueueConnection;
    
    import javax.jms.QueueConnectionFactory;
    
    import javax.jms.QueueSender;
    
    import javax.jms.QueueSession;
    
    import javax.jms.Session;
    
    import javax.jms.TextMessage;
    
    import javax.servlet.ServletException;
    
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    
    
    
    /**
    
     * Servlet implementation class for Servlet: SendingServlet
    
     * 
    
     */
    
    
    
    public class SendingServlet extends javax.servlet.http.HttpServlet implements
    
          javax.servlet.Servlet {
    
    
    
       private static final long serialVersionUID = 1L;
    
    
    
       private PrintWriter printWriter;
    
    
    
       // Inject QueueConnectionFactory to the field queueConnectionFactoryAnnot
    
       @Resource(mappedName = "MyServletQueueConnectionFactory")
    
       private QueueConnectionFactory queueConnectionFactoryAnnot;
    
    
    
       // Inject Queue to the field queueAnnot
    
       @Resource(mappedName = "MyServletQueue")
    
       private Queue queueAnnot;
    
    
    
       public SendingServlet() {
    
          super();
    
       }
    
    
    
       protected void doGet(HttpServletRequest request,
    
             HttpServletResponse response) throws ServletException, IOException {
    
          doTest(request, response);
    
       }
    
    
    
       protected void doPost(HttpServletRequest request,
    
             HttpServletResponse response) throws ServletException, IOException {
    
          doTest(request, response);
    
       }
    
    
    
       private void doTest(HttpServletRequest request, HttpServletResponse response)
    
             throws ServletException {
    
          QueueConnection queueConnection = null;
    
          QueueSession queueSession = null;
    
          QueueSender queueSender = null;
    
          TextMessage message = null;
    
    
    
          try {
    
             printWriter = response.getWriter();
    
          } catch (IOException ioe) {
    
             ioe.printStackTrace();
    
             throw new ServletException("");
    
          }
    
    
    
          System.out.println("Start.");
    
          printWriter.println("Start.");
    
    
    
          try {
    
             // queueConnectionFactoryAnnot is injected via annotation
    
             queueConnection = queueConnectionFactoryAnnot
    
                   .createQueueConnection();
    
             queueSession = queueConnection.createQueueSession(false,
    
                   Session.AUTO_ACKNOWLEDGE);
    
             // queueAnnot is injected via annotation
    
             queueSender = queueSession.createSender(queueAnnot);
    
             message = queueSession.createTextMessage();
    
             for (int i = 0; i < 3; i++) {
    
                message.setText("This is message " + (i + 1));
    
                System.out.println("Sending message: " + message.getText());
    
                printWriter.println("<br>" + "Sending message: "
    
                      + message.getText());
    
                queueSender.send(message);
    
             }
    
          } catch (JMSException e) {
    
             System.out.println("Exception occurred: " + e.toString());
    
             throw new ServletException("");
    
          } finally {
    
             if (queueConnection != null) {
    
                try {
    
                   queueConnection.close();
    
                } catch (JMSException e) {
    
                   throw new ServletException("");
    
                }
    
             }
    
          }
    
    
    
          System.out.println("End.");
    
          printWriter.println("<br>End.");
    
       }
    
    }
    
    
End of the code.
Implementing the Consumer

This is an example of a message-driven bean that receives the messages sent from a servlet. You have to use the @MessageDriven annotation to specify that you are implementing a message-driven bean. Using the mappedName attribute you specify that you are listening for messages in MyServletQueue.

Syntax Syntax

  1. package com.sap.jms.test.MDBEjbBean;
    
    
    
    import javax.ejb.ActivationConfigProperty;
    
    import javax.ejb.MessageDriven;
    
    import javax.jms.JMSException;
    
    import javax.jms.Message;
    
    import javax.jms.MessageListener;
    
    import javax.jms.TextMessage;
    
    
    
    /**
    
    
    
     /*
    
     * A Message-Driven Bean processing TextMessages sent to MyServletQueue.
    
     */
    
    @MessageDriven(mappedName = "MyServletQueue", activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
    
    public class MDBBean implements MessageListener {
    
       /**
    
        * Receives a single JMSMessage from the specified Queue.
    
        * @param msg
    
        *  the message received from MyServletQueue
    
        */
    
       public void onMessage(Message msg) {
    
          try {
    
             // expecting only TextMessages
    
             if (msg instanceof TextMessage) {
    
                TextMessage textMsg = (TextMessage) msg;
    
                //To see the received messages in the logs, you need to 
    
                // increase the severity of System.out to debug
    
                System.out.println("MDBBean: Message received: "
    
                      + textMsg.getText());
    
             } else {
    
                System.out.println("MDBBean: Message of wrong type: "
    
                      + msg.getClass().getName());
    
             }
    
          } catch (JMSException jmsexc) {
    
             System.out.println("MDBBean: Message processing failed. " + jmsexc);
    
          }
    
       }
    
    }
    
End of the code.
Configuring the JMS resources

To configure the JMS resources, you have to add the following source code in the jms-resources.xml:

Syntax Syntax

  1. <?xml version="1.0" encoding="UTF-8"?>
    
    <jms-resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
       xsi:noNamespaceSchemaLocation="jms-resources.xsd">
    
       <connection-factory>
    
          <name>MyServletQueueConnectionFactory</name>
    
          <sap-local-factory-type>
    
             <type>javax.jms.QueueConnectionFactory</type>
    
             <virtual-provider>default</virtual-provider>
    
          </sap-local-factory-type>
    
       </connection-factory>
    
       <destination>
    
          <name>MyServletQueue</name>
    
          <type>javax.jms.Queue</type>
    
          <sap-local-destination-type>
    
             <virtual-provider>default</virtual-provider>
    
          </sap-local-destination-type>
    
       </destination>
    
    </jms-resources>
    
End of the code.
Deploying and Running the Example

To deploy the example, use the SAP NetWeaver Developer Studio. In the context menu of the Server view, choose Add and Remove Projects... and select the EAR you want to deploy. Add it and choose Finish.

To run the example, enter the following URL in a Web browser:

http://<host>:<http_port>/jmsProjectWeb/SendingServlet