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: MdbBean.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package samplemdb;
028:
029: import javax.ejb.MessageDrivenBean;
030: import javax.ejb.MessageDrivenContext;
031: import javax.jms.Message;
032: import javax.jms.MessageListener;
033: import javax.jms.TextMessage;
034: import javax.jms.JMSException;
035:
036: /**
037: * Example of MessageDrivenBean on a Topic. The transactions are container
038: * managed (Required)
039: */
040: public class MdbBean implements MessageDrivenBean, MessageListener {
041:
042: private transient MessageDrivenContext mdbContext;
043:
044: // ------------------------------------------------------------------
045: // MessageDrivenBean implementation
046: // ------------------------------------------------------------------
047:
048: /**
049: * Default constructor
050: */
051: public MdbBean() {
052: }
053:
054: /**
055: * Set the associated context. The container call this method after the
056: * instance creation. The enterprise Bean instance should store the
057: * reference to the context object in an instance variable. This method is
058: * called with no transaction context.
059: * @param MessageDrivenContext A MessageDrivenContext interface for the
060: * instance.
061: * @throws EJBException Thrown by the method to indicate a failure caused by
062: * a system-level error.
063: */
064:
065: public void setMessageDrivenContext(MessageDrivenContext ctx) {
066: System.out.println("MdbBean setMessageDrivenContext");
067: mdbContext = ctx;
068: }
069:
070: /**
071: * A container invokes this method before it ends the life of the
072: * message-driven object. This happens when a container decides to terminate
073: * the message-driven object. This method is called with no transaction
074: * context.
075: * @throws EJBException Thrown by the method to indicate a failure caused by
076: * a system-level error.
077: */
078: public void ejbRemove() {
079: System.out.println("MdbBean ejbRemove");
080: }
081:
082: /**
083: * The Message driven bean must define an ejbCreate methods with no args.
084: */
085: public void ejbCreate() {
086: System.out.println("MdbBean ejbCreate");
087: }
088:
089: /**
090: * onMessage method
091: */
092: public void onMessage(Message message) {
093: System.out.println("MdbBean onMessage");
094: try {
095: TextMessage mess = (TextMessage) message;
096: System.out.println("Message received: " + mess.getText());
097: } catch (JMSException ex) {
098: System.err.println("Exception caught: " + ex);
099: }
100: }
101: }
|