KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > timer > ejb > TimerMessageBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.timer.ejb;
23
24 import java.io.Serializable JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.MessageDrivenBean JavaDoc;
27 import javax.ejb.MessageDrivenContext JavaDoc;
28 import javax.ejb.TimedObject JavaDoc;
29 import javax.ejb.Timer JavaDoc;
30 import javax.ejb.TimerService JavaDoc;
31 import javax.jms.DeliveryMode JavaDoc;
32 import javax.jms.JMSException JavaDoc;
33 import javax.jms.Message JavaDoc;
34 import javax.jms.MessageListener JavaDoc;
35 import javax.jms.Queue JavaDoc;
36 import javax.jms.QueueConnection JavaDoc;
37 import javax.jms.QueueConnectionFactory JavaDoc;
38 import javax.jms.QueueSender JavaDoc;
39 import javax.jms.QueueSession JavaDoc;
40 import javax.jms.Session JavaDoc;
41 import javax.jms.TextMessage JavaDoc;
42 import javax.naming.InitialContext JavaDoc;
43
44 import org.jboss.logging.Logger;
45
46 /** An MDB that schedules an ejb timer for every onMessage receipt. The timer
47  * is a single event timer that expires after 10 seconds.
48  *
49  * @ejb.bean name="TimerMDB"
50  * description="TimerMDB unit test bean"
51  * destination-type="javax.jms.Queue"
52  * acknowledge-mode="Auto-acknowledge"
53  * @ejb.resource-ref res-ref-name="jms/QCF" res-type="javax.jms.QueueConnectionFactory" res-auth="Container"
54  * @jboss.destination-jndi-name name="queue/A"
55  * @jboss.resource-ref res-ref-name="jms/QCF" jndi-name="ConnectionFactory"
56  *
57  * @author Scott.Stark@jboss.org
58  * @version $Revision: 37406 $
59  */

60 public class TimerMessageBean implements MessageDrivenBean JavaDoc, MessageListener JavaDoc, TimedObject JavaDoc
61 {
62    private static Logger log = Logger.getLogger(TimerMessageBean.class);
63    private MessageDrivenContext JavaDoc messageContext = null;
64    private QueueConnection JavaDoc qc = null;
65    private InitialContext JavaDoc ctx = null;
66    private long timerTimeout = 10000;
67
68    static class ReplyInfo implements Serializable JavaDoc
69    {
70       private int msgID;
71       private Queue JavaDoc replyTo;
72       ReplyInfo(int msgID, Queue JavaDoc replyTo)
73       {
74          this.msgID = msgID;
75          this.replyTo = replyTo;
76       }
77    }
78
79    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
80       throws EJBException JavaDoc
81    {
82       messageContext = ctx;
83    }
84
85    public void ejbCreate()
86    {
87       try
88       {
89          ctx = new InitialContext JavaDoc();
90          QueueConnectionFactory JavaDoc qcf = (QueueConnectionFactory JavaDoc) ctx.lookup("java:comp/env/jms/QCF");
91          qc = qcf.createQueueConnection();
92       }
93       catch (Exception JavaDoc e)
94       {
95          throw new EJBException JavaDoc("ejbCreate failed", e);
96       }
97    }
98
99    public void ejbTimeout(Timer JavaDoc timer)
100    {
101       log.info("ejbTimeout(), timer: " + timer);
102       ReplyInfo info = (ReplyInfo) timer.getInfo();
103       try
104       {
105          sendReply("ejbTimeout", info.msgID, info.replyTo);
106       }
107       catch(Exception JavaDoc e)
108       {
109          log.error("Failed to send timer msg", e);
110       }
111    }
112
113    public void ejbRemove() throws EJBException JavaDoc
114    {
115       try
116       {
117          qc.close();
118          log.info("QueueConnection is closed.");
119       }
120       catch (JMSException JavaDoc e)
121       {
122          log.error("Failed to close connection", e);
123       }
124    }
125
126    public void onMessage(Message JavaDoc message)
127    {
128       try
129       {
130          TextMessage JavaDoc msg = (TextMessage JavaDoc) message;
131          log.info("onMessage() called, msg="+msg);
132          int msgID = msg.getIntProperty("UNIQUE_ID");
133          Queue JavaDoc replyTo = (Queue JavaDoc) message.getJMSReplyTo();
134          initTimer(msgID, replyTo);
135          sendReply("onMessage", msgID, replyTo);
136       }
137       catch (Exception JavaDoc e)
138       {
139          log.error("onMessage failure", e);
140       }
141    }
142
143    public void initTimer(int msgID, Queue JavaDoc replyTo)
144    {
145       try
146       {
147          TimerService JavaDoc ts = messageContext.getTimerService();
148          ReplyInfo info = new ReplyInfo(msgID, replyTo);
149          Timer JavaDoc timer = ts.createTimer(timerTimeout, info);
150          log.info("Timer created with a timeout: " + timerTimeout
151             + " and with info: " + msgID
152             + ", handle: "+timer.getHandle());
153       }
154       catch (Exception JavaDoc e)
155       {
156          log.info("Failed to init timer", e);
157       }
158       return;
159
160    }
161
162    private void sendReply(String JavaDoc msg, int msgID, Queue JavaDoc dest)
163       throws JMSException JavaDoc
164    {
165       QueueSession JavaDoc qs = null;
166       try
167       {
168          qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
169          QueueSender JavaDoc sender = qs.createSender(dest);
170          TextMessage JavaDoc reply = qs.createTextMessage();
171          reply.setText(msg + " : " + msgID);
172          reply.setIntProperty("UNIQUE_ID", msgID);
173          sender.send(reply, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, 180000);
174          log.info("Message sent");
175       }
176       finally
177       {
178          try
179          {
180             qs.close();
181             log.info("JBossMQ QueueSession Closed");
182          }
183          catch (JMSException JavaDoc e)
184          {
185             log.error("Failed to close queue session", e);
186          }
187       }
188    }
189 }
190
Popular Tags