In this recipe, we will use a TextMessage
to send an order to an MDB. The TextMessage
interface uses a String
object to hold the order. We will use the java.util.Scanner
class to parse the order information from this string.
The essential structure of a servlet used to generate a message was introduced in the introduction. Here we will address the unique elements of creating and using a TextMessage
which include:
Create a Queue named jms/TextQueue
and a QueueConnectionFactory
named jms/TextFactoryPool
as described in the introduction. Next, create a Java EE application called TextMessageApplication
. In the EJB module, add a package called packt
with an MDB called TextBean
. In the WAR module add a package called servlet
and a servlet called TextServlet
.
Create the TextServlet
as follows. The doGet
and doPost
methods are not listed here.
public class TextServlet extends HttpServlet { @Resource(mappedName="jms/TextFactoryPool") private QueueConnectionFactory queueConnectionFactory; @Resource(mappedName="jms/TextQueue") private Queue queue; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Connection connection; try { connection = queueConnectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = (MessageProducer) session.createProducer(queue); TextMessage textMessage = session.createTextMessage(); // Part number 12345 // Weight 12.5f // Quantity - 50 String message = "12345 12.5 50"; textMessage.setText(message); messageProducer.send(textMessage); System.out.println("---> Text Message Sent"); } catch (JMSException ex) { Logger.getLogger(TextServlet.class.getName()). log(Level.SEVERE, null, ex); } out.println("<html>"); out.println("<head>"); out.println("<title>Servlet TextServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet TextServlet at " + request.getContextPath () + "</h1>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } }
Next, let's look at the TextBean
. Use the @MessageDriven annotation to designate the EJB as an MDB. Be sure to implement the MessageListener
interface and add the onMessage
method.
@MessageDriven(mappedName = "jms/TextQueue", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class TextBean implements MessageListener { public TextBean() { } public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { Scanner scanner = new Scanner(textMessage.getText()); System.out.println("---> Part Number: " + scanner.nextInt()); System.out.println("---> Weight: " + scanner.nextFloat()); System.out.println("---> Quantity: " + scanner.nextInt()); System.out.println("---> TextMessage Received"); } catch (JMSException ex) { Logger.getLogger(TextBean.class.getName()).log(Level.SEVERE, null, ex); } } }
Execute the servlet. The server's log will display the output of the println
methods.
INFO: ---> Text Message Sent
INFO: ---> Part Number: 12345
INFO: ---> Weight: 12.5
INFO: ---> Quantity: 50
INFO: ---> TextMessage Received
In the TextServlet
, the @Resource annotation injected a QueueConnectionFactory
and a Queue
. The general details of connecting to a Queue
are detailed in the introduction. Notice that we used the jms/TextQueue
in this example.
In the processRequest
method, the TextMessage
was created using the session's createTextMessage
method. A string was declared to hold the parts information (part number, weight, and quantity) and was then assigned to the TextMessage
with the setText
method. Next, the MessageProducer
object's send
method transferred the message to the queue.
The TextBean
started with the @MessageDriven annotation and used the mappedName attribute to specify the queue used. Two @ActivationConfigProperty elements were used to specify the acknowledgement mode and that the destination is a queue.
The Message
received in the onMessage
method was cast to a TextMessage
. A Scanner
object was created to retrieve the individual elements of the order. Using the nextInt
and nextFloat
methods, the elements of the message were easily extracted and displayed.
Once the TextMessage
object has been created in the servlet, writing to and reading from TextMessage
is permitted. When it is received by the MDB, it is in a read-only mode. Attempts to write to the message will result in a javax.jms.MessageNotWriteableException
. However, the clearBody
method in the MDB can be used to permit subsequent writing and reading of the message. Normally, this is not necessary.
This example illustrates a simple use of the TextMessage
. Using an ObjectMessage
may be the best approach for handling orders or similar object-oriented requests. However, the TextMessage
is well suited for XML type messages.
18.119.160.181