01: package com.mockrunner.example.jms;
02:
03: import java.io.IOException;
04:
05: import javax.jms.JMSException;
06: import javax.jms.Queue;
07: import javax.jms.QueueConnection;
08: import javax.jms.QueueConnectionFactory;
09: import javax.jms.QueueReceiver;
10: import javax.jms.QueueSender;
11: import javax.jms.QueueSession;
12: import javax.jms.Session;
13: import javax.jms.TextMessage;
14: import javax.naming.InitialContext;
15: import javax.servlet.ServletException;
16: import javax.servlet.http.HttpServlet;
17: import javax.servlet.http.HttpServletRequest;
18: import javax.servlet.http.HttpServletResponse;
19:
20: /**
21: * This example servlet registers a listener for a
22: * test queue. On each request a customer id is
23: * extracted an sent as a text message to the queue.
24: * The example simulates an online bank. The user
25: * can request a printout of his account data.
26: * This is done in an asynchronous manner, so
27: * the user gets an immediate response.
28: */
29: public class PrintMessageServlet extends HttpServlet {
30: public void init() throws ServletException {
31: try {
32: InitialContext initialContext = new InitialContext();
33: QueueConnectionFactory queueFactory = (QueueConnectionFactory) initialContext
34: .lookup("java:/ConnectionFactory");
35: QueueConnection queueConnection = queueFactory
36: .createQueueConnection();
37: QueueSession queueSession = queueConnection
38: .createQueueSession(false,
39: Session.CLIENT_ACKNOWLEDGE);
40: Queue queue = (Queue) initialContext
41: .lookup("queue/testQueue");
42: QueueReceiver receiver = queueSession.createReceiver(queue);
43: receiver.setMessageListener(new PrintMessageListener());
44: queueConnection.start();
45: } catch (Exception exc) {
46: exc.printStackTrace();
47: }
48: }
49:
50: public void doGet(HttpServletRequest request,
51: HttpServletResponse response) throws ServletException,
52: IOException {
53: doPost(request, response);
54: }
55:
56: public void doPost(HttpServletRequest request,
57: HttpServletResponse response) throws ServletException,
58: IOException {
59: String customerId = request.getParameter("customerId");
60: QueueConnection queueConnection = null;
61: QueueSession queueSession = null;
62: QueueSender queueSender = null;
63: try {
64: InitialContext initialContext = new InitialContext();
65: QueueConnectionFactory queueFactory = (QueueConnectionFactory) initialContext
66: .lookup("java:/ConnectionFactory");
67: queueConnection = queueFactory.createQueueConnection();
68: queueSession = queueConnection.createQueueSession(false,
69: Session.CLIENT_ACKNOWLEDGE);
70: Queue queue = (Queue) initialContext
71: .lookup("queue/testQueue");
72: TextMessage message = queueSession
73: .createTextMessage(customerId);
74: queueSender = queueSession.createSender(queue);
75: queueSender.send(message);
76: response.getWriter().write(
77: "Print request for " + customerId
78: + " successfully sent");
79: } catch (Exception exc) {
80: response.getWriter().write(
81: "Error sending print request for " + customerId);
82: } finally {
83: try {
84: if (null != queueSender)
85: queueSender.close();
86: if (null != queueSession)
87: queueSession.close();
88: if (null != queueConnection)
89: queueConnection.close();
90: } catch (JMSException exc) {
91: exc.printStackTrace();
92: }
93: }
94: }
95: }
|