KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samplemdb > MdbBean


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: MdbBean.java,v 1.3 2004/04/19 06:39:30 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // MdbBean.java
30
// Message Driven bean
31
package samplemdb;
32
33 import javax.ejb.MessageDrivenBean;
34 import javax.ejb.MessageDrivenContext;
35 import javax.jms.Message;
36 import javax.jms.MessageListener;
37 import javax.jms.TextMessage;
38 import javax.jms.JMSException;
39
40 /**
41  * Example of MessageDrivenBean on a Topic. The transactions are container
42  * managed (Required)
43  */

44 public class MdbBean implements MessageDrivenBean, MessageListener {
45
46     private transient MessageDrivenContext mdbContext;
47
48     // ------------------------------------------------------------------
49
// MessageDrivenBean implementation
50
// ------------------------------------------------------------------
51

52     /**
53      * Default constructor
54      */

55     public MdbBean() {
56     }
57
58     /**
59      * Set the associated context. The container call this method after the
60      * instance creation. The enterprise Bean instance should store the
61      * reference to the context object in an instance variable. This method is
62      * called with no transaction context.
63      * @param MessageDrivenContext A MessageDrivenContext interface for the
64      * instance.
65      * @throws EJBException Thrown by the method to indicate a failure caused by
66      * a system-level error.
67      */

68
69     public void setMessageDrivenContext(MessageDrivenContext ctx) {
70         System.out.println("MdbBean setMessageDrivenContext");
71         mdbContext = ctx;
72     }
73
74     /**
75      * A container invokes this method before it ends the life of the
76      * message-driven object. This happens when a container decides to terminate
77      * the message-driven object. This method is called with no transaction
78      * context.
79      * @throws EJBException Thrown by the method to indicate a failure caused by
80      * a system-level error.
81      */

82     public void ejbRemove() {
83         System.out.println("MdbBean ejbRemove");
84     }
85
86     /**
87      * The Message driven bean must define an ejbCreate methods with no args.
88      */

89     public void ejbCreate() {
90         System.out.println("MdbBean ejbCreate");
91     }
92
93     /**
94      * onMessage method
95      */

96     public void onMessage(Message message) {
97         System.out.println("MdbBean onMessage");
98         try {
99             TextMessage mess = (TextMessage) message;
100             System.out.println("Message received: " + mess.getText());
101         } catch (JMSException ex) {
102             System.err.println("Exception caught: " + ex);
103         }
104     }
105 }
Popular Tags