001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): ____________________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: StockHandlerBean.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package sampleappli;
028:
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.ejb.MessageDrivenBean;
032: import javax.ejb.MessageDrivenContext;
033: import javax.ejb.EJBException;
034: import javax.jms.*;
035: import javax.rmi.PortableRemoteObject;
036:
037: /**
038: * StockHandlerBean is a Message driven bean which is listening to a topic It
039: * receives MapMessages that contains a customer identification, a product
040: * identification and a quantity for the product When the StockHandlerBean
041: * receives a message it builds a message corresponding to an Order and sends it
042: * to a Queue destination then it updates the Stock database.
043: */
044: public class StockHandlerBean implements MessageDrivenBean,
045: MessageListener {
046:
047: private transient MessageDrivenContext mdbContext;
048:
049: StockHome sh = null;
050:
051: QueueConnectionFactory queueConnectionFactory = null;
052:
053: QueueConnection queueConnection = null;
054:
055: Queue queue = null;
056:
057: Context initialContext = null;
058:
059: // ------------------------------------------------------------------
060: // MessageDrivenBean implementation
061: // ------------------------------------------------------------------
062:
063: /**
064: * Default constructor
065: */
066: public StockHandlerBean() {
067: }
068:
069: /**
070: * Set the associated context. The container call this method after the
071: * instance creation. The enterprise Bean instance should store the
072: * reference to the context object in an instance variable. This method is
073: * called with no transaction context.
074: * @param MessageDrivenContext A MessageDrivenContext interface for the
075: * instance.
076: * @throws EJBException Thrown by the method to indicate a failure caused by
077: * a system-level error.
078: */
079:
080: public void setMessageDrivenContext(MessageDrivenContext ctx) {
081: mdbContext = ctx;
082: }
083:
084: /**
085: * A container invokes this method before it ends the life of the
086: * message-driven object. This happens when a container decides to terminate
087: * the message-driven object. This method is called with no transaction
088: * context.
089: * @throws EJBException Thrown by the method to indicate a failure caused by
090: * a system-level error.
091: */
092: public void ejbRemove() {
093: }
094:
095: /**
096: * The Message driven bean must define an ejbCreate methods with no args.
097: */
098: public void ejbCreate() {
099: try {
100: initialContext = new InitialContext();
101: sh = (StockHome) PortableRemoteObject
102: .narrow(initialContext
103: .lookup("java:comp/env/ejb/Stock"),
104: StockHome.class);
105: queue = (Queue) initialContext
106: .lookup("java:comp/env/jms/Orders");
107: queueConnectionFactory = (QueueConnectionFactory) initialContext
108: .lookup("java:comp/env/jms/QueueConnectionFactory");
109: queueConnection = queueConnectionFactory
110: .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,
152: Session.AUTO_ACKNOWLEDGE);
153: qs = session.createSender(queue);
154: stock = sh.findByPrimaryKey(pid);
155: System.out.println("StockHandlerBean findByPrimaryKey("
156: + pid + ")");
157: } catch (Exception ex) {
158: System.err.println(ex.toString());
159: }
160: try {
161: TextMessage tm = session.createTextMessage();
162: String m = "For CustomerId = " + cid + " ProductId= " + pid
163: + " Quantity= " + qty;
164: tm.setText(m);
165: qs.send(tm);
166: System.out.println("StockHandlerBean message sent: " + m);
167: stock.decreaseQuantity(qty);
168: } catch (Exception ex) {
169: // on negative Stock -> rollback the transaction
170: mdbContext.setRollbackOnly();
171: } finally {
172: if (session != null) {
173: try {
174: session.close();
175: System.out
176: .println("StockHandlerBean session closed");
177: } catch (Exception e) {
178: e.printStackTrace();
179: }
180: }
181: }
182: }
183:
184: }
|