KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > newsamplemdb > MdbBean


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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.1 2004/05/04 14:37:23 fmaistre Exp $
26  * --------------------------------------------------------------------------
27  */

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

32 package newsamplemdb;
33
34 import javax.ejb.MessageDrivenBean;
35 import javax.ejb.MessageDrivenContext;
36 import javax.jms.Message;
37 import javax.jms.MessageListener;
38 import javax.jms.TextMessage;
39 import javax.jms.JMSException;
40
41
42
43 /**
44  * Example of MessageDrivenBean on a Topic. The transactions are container managed (Required)
45  */

46 public class MdbBean implements MessageDrivenBean, MessageListener {
47
48     private transient MessageDrivenContext mdbContext;
49
50
51     
52     // ------------------------------------------------------------------
53
// MessageDrivenBean implementation
54
// ------------------------------------------------------------------
55

56     /**
57      * Default constructor
58      *
59      */

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

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

89     public void ejbRemove() {
90     System.out.println( "MdbBean ejbRemove");
91     }
92
93     /**
94      * The Message driven bean must define an ejbCreate methods with no args.
95      *
96      */

97     public void ejbCreate() {
98     System.out.println( "MdbBean ejbCreate");
99     }
100
101     /**
102      * onMessage method
103      */

104     public void onMessage(Message message) {
105     System.out.println( "MdbBean onMessage");
106     try{
107         TextMessage mess = (TextMessage)message;
108         System.out.println( "Message received: "+mess.getText());
109     }catch(JMSException ex){
110         System.err.println("Exception caught: "+ex);
111     }
112     }
113
114     /**
115      * onMessage1 method
116      */

117     public void onMessage1(Message message) {
118     System.out.println( "MdbBean onMessage1");
119     try{
120         TextMessage mess = (TextMessage)message;
121         System.out.println( "Message received: "+mess.getText());
122     }catch(JMSException ex){
123         System.err.println("Exception caught: "+ex);
124     }
125     }
126 }
127
Popular Tags