KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > foedeployer > ejb > message > MessageTraderBean


1 /*
2 * JBoss, the OpenSource EJB server
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7
8 package org.jboss.test.foedeployer.ejb.message;
9
10 import java.rmi.RemoteException JavaDoc;
11
12 import javax.ejb.CreateException JavaDoc;
13 import javax.ejb.MessageDrivenBean JavaDoc;
14 import javax.ejb.MessageDrivenContext JavaDoc;
15
16 import javax.jms.JMSException JavaDoc;
17 import javax.jms.Message JavaDoc;
18 import javax.jms.MessageListener JavaDoc;
19 import javax.jms.ObjectMessage JavaDoc;
20 import javax.jms.TextMessage JavaDoc;
21 import javax.jms.QueueConnectionFactory JavaDoc;
22 import javax.jms.QueueConnection JavaDoc;
23 import javax.jms.QueueSession JavaDoc;
24 import javax.jms.Session JavaDoc;
25 import javax.jms.QueueSender JavaDoc;
26 import javax.jms.Queue JavaDoc;
27
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import org.apache.log4j.Category;
32
33 /**
34  * A simple Message Driven Bean
35  *
36  * @ejb.bean
37  * name="MessageTrader"
38  * generate="true"
39  * jndi-name="MessageTraderBean"
40  * transaction-type="Container"
41  * acknowledge-mode="Auto-acknowledge"
42  * destination-type="javax.jms.Topic"
43  * subscription-durability="NonDurable"
44  *
45  * @jboss.destination-jndi-name name="topic/testTopic"
46  * @weblogic.message-driven destination-jndi-name="topic/testTopic"
47  *
48  * @author <a HREF="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
49  */

50 public class MessageTraderBean
51    implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
52 {
53    // Constants -------------------------------------------------
54
private static final String JavaDoc QUEUE_CONNECTION_FACTORY = "ConnectionFactory";
55    private static final String JavaDoc QUEUE = "queue/testQueue";
56
57    // Attributes ------------------------------------------------
58
protected Category log;
59    private MessageDrivenContext JavaDoc mdc;
60    private transient QueueConnection JavaDoc queueConnection;
61    private transient Queue JavaDoc queue;
62
63    // MessageDrivenBean implementation --------------------------
64
/**
65     * Sets the session context.
66     *
67     * @param ctx MessageDrivenContext Context for session
68     */

69    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
70    {
71       mdc = ctx;
72    }
73
74    // Implementation of MessageListener -----------------------------------
75
/**
76     * Handle the message.
77     */

78    public void onMessage(Message JavaDoc msg)
79    {
80       getLog().debug("received message of type: " + msg.getClass().getName());
81
82       if( !( msg instanceof ObjectMessage JavaDoc ) )
83          getLog().error( "message isn't of type ObjectMessage" );
84
85       try
86       {
87          QuoteMessage qm = (QuoteMessage) ( (ObjectMessage JavaDoc)msg ).getObject();
88
89          getLog().debug( "received new quote: " + qm.getQuote() );
90
91          send( msg );
92       }
93       catch(Exception JavaDoc ex)
94       {
95          getLog().error("ERROR: ", ex);
96       }
97    }
98
99    public void ejbCreate() { }
100
101    public void ejbRemove()
102    {
103       if( queueConnection != null )
104       {
105          getLog().debug( "closing connection" );
106          try
107          {
108             queueConnection.close();
109          }
110          catch( JMSException JavaDoc jmse )
111          {
112             getLog().debug( "Exception while closing queue connection: ", jmse );
113          }
114       }
115       else
116       {
117          // it could be null because not all MDBs in pool might be used
118
getLog().debug( "queue connection is null" );
119       }
120    }
121
122    // Private --------------------------------------------------------
123
private void send( Message JavaDoc msg )
124       throws Exception JavaDoc
125    {
126       QueueSession JavaDoc queueSession = getQueueSession();
127       queue = getQueue();
128
129       getLog().debug( "creating sender" );
130       QueueSender JavaDoc queueSender = queueSession.createSender( queue );
131
132       ObjectMessage JavaDoc objMsg = (ObjectMessage JavaDoc)msg;
133       QuoteMessage qm = (QuoteMessage)objMsg.getObject();
134       getLog().debug( "resending the message: " + qm.getQuote() );
135       queueSender.send( msg );
136    }
137
138    private QueueSession JavaDoc getQueueSession()
139       throws Exception JavaDoc
140    {
141       if(queueConnection == null)
142       {
143          getLog().debug("looking for queue connection factory: "
144             + QUEUE_CONNECTION_FACTORY );
145
146          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
147          QueueConnectionFactory JavaDoc queueFactory =
148             (QueueConnectionFactory JavaDoc) ctx.lookup(QUEUE_CONNECTION_FACTORY);
149          queueConnection = queueFactory.createQueueConnection();
150       }
151
152       getLog().debug( "creating queue connection" );
153       return queueConnection.createQueueSession(false,
154                 Session.AUTO_ACKNOWLEDGE);
155    }
156
157    private Queue JavaDoc getQueue()
158       throws Exception JavaDoc
159    {
160       if(queue == null)
161       {
162          getLog().debug( "looking for queue: " + QUEUE );
163
164          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
165          queue = (Queue JavaDoc) ctx.lookup( QUEUE );
166       }
167
168       return queue;
169    }
170
171    private Category getLog()
172    {
173       if( log != null ) return log;
174       log = Category.getInstance( this.getClass() );
175       return log;
176    }
177 }
178
Popular Tags