KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sampleappli > StockHandlerBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: StockHandlerBean.java,v 1.6 2004/04/19 06:39:30 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // StockHandlerBean.java
30
// Message Driven bean
31

32 package sampleappli;
33
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.ejb.MessageDrivenBean;
37 import javax.ejb.MessageDrivenContext;
38 import javax.ejb.EJBException;
39 import javax.jms.*;
40 import javax.rmi.PortableRemoteObject;
41
42 /**
43  * StockHandlerBean is a Message driven bean which is listening to a topic It
44  * receives MapMessages that contains a customer identification, a product
45  * identification and a quantity for the product When the StockHandlerBean
46  * receives a message it builds a message corresponding to an Order and sends it
47  * to a Queue destination then it updates the Stock database.
48  */

49 public class StockHandlerBean implements MessageDrivenBean, MessageListener {
50
51     private transient MessageDrivenContext mdbContext;
52
53     StockHome sh = null;
54
55     QueueConnectionFactory queueConnectionFactory = null;
56
57     QueueConnection queueConnection = null;
58
59     Queue queue = null;
60
61     Context initialContext = null;
62
63     // ------------------------------------------------------------------
64
// MessageDrivenBean implementation
65
// ------------------------------------------------------------------
66

67     /**
68      * Default constructor
69      */

70     public StockHandlerBean() {
71     }
72
73     /**
74      * Set the associated context. The container call this method after the
75      * instance creation. The enterprise Bean instance should store the
76      * reference to the context object in an instance variable. This method is
77      * called with no transaction context.
78      * @param MessageDrivenContext A MessageDrivenContext interface for the
79      * instance.
80      * @throws EJBException Thrown by the method to indicate a failure caused by
81      * a system-level error.
82      */

83
84     public void setMessageDrivenContext(MessageDrivenContext ctx) {
85         mdbContext = ctx;
86     }
87
88     /**
89      * A container invokes this method before it ends the life of the
90      * message-driven object. This happens when a container decides to terminate
91      * the message-driven object. This method is called with no transaction
92      * context.
93      * @throws EJBException Thrown by the method to indicate a failure caused by
94      * a system-level error.
95      */

96     public void ejbRemove() {
97     }
98
99     /**
100      * The Message driven bean must define an ejbCreate methods with no args.
101      */

102     public void ejbCreate() {
103         try {
104             initialContext = new InitialContext();
105             sh = (StockHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Stock"),
106                     StockHome.class);
107             queue = (Queue) initialContext.lookup("java:comp/env/jms/Orders");
108             queueConnectionFactory = (QueueConnectionFactory) initialContext
109                     .lookup("java:comp/env/jms/QueueConnectionFactory");
110             queueConnection = queueConnectionFactory.createQueueConnection();
111         } catch (Exception e) {
112             System.err.println("StockHandlerBean ejbCreate : " + e);
113         }
114     }
115
116     /**
117      * onMessage method Map Messages are receive with the following format:
118      * "CustomerId" String "ProductId" String "Quantity" int the Message driven
119      * bean will construct a string for an Order that will be sent to the Queue
120      * Orders and decrease the stock quantity for the product identified by
121      * ProductId this method run in the scope of a transaction that the
122      * container started immediately before dispatching the onMessage method the
123      * sending message to the Order queue and the updating of the Stock table is
124      * made in the same global transaction this transaction may be rolled back
125      * if the stock quantity became negative
126      */

127
128     public void onMessage(Message message) {
129         QueueSession session = null;
130         QueueSender qs = null;
131         int code;
132         String pid = null;
133         ;
134         int qty = 0;
135         String cid = null;
136         ;
137         MapMessage msg = (MapMessage) message;
138         Stock stock = null;
139         try {
140             if (message.getJMSRedelivered()) {
141                 System.out.println("Ok, that's it!");
142                 return;
143             }
144         } catch (Exception ex) {
145             System.err.println(ex.toString());
146         }
147         try {
148             pid = msg.getString("ProductId");
149             qty = msg.getInt("Quantity");
150             cid = msg.getString("CustomerId");
151             session = queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
152             qs = session.createSender(queue);
153             stock = sh.findByPrimaryKey(pid);
154             System.out.println("StockHandlerBean findByPrimaryKey(" + pid + ")");
155         } catch (Exception ex) {
156             System.err.println(ex.toString());
157         }
158         try {
159             TextMessage tm = session.createTextMessage();
160             String m = "For CustomerId = " + cid + " ProductId= " + pid + " Quantity= " + qty;
161             tm.setText(m);
162             qs.send(tm);
163             System.out.println("StockHandlerBean message sent: " + m);
164             stock.decreaseQuantity(qty);
165         } catch (Exception ex) {
166             // on negative Stock -> rollback the transaction
167
mdbContext.setRollbackOnly();
168         } finally {
169             if (session != null) {
170                 try {
171                     session.close();
172                     System.out.println("StockHandlerBean session closed");
173                 } catch (Exception e) {
174                     e.printStackTrace();
175                 }
176             }
177         }
178     }
179
180 }
Popular Tags