KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Date JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.MessageDrivenBean JavaDoc;
28 import javax.ejb.MessageDrivenContext JavaDoc;
29 import javax.ejb.TimedObject JavaDoc;
30 import javax.ejb.Timer JavaDoc;
31 import javax.ejb.TimerService JavaDoc;
32 import javax.jms.DeliveryMode JavaDoc;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.Message JavaDoc;
35 import javax.jms.MessageListener JavaDoc;
36 import javax.jms.Queue JavaDoc;
37 import javax.jms.QueueConnection JavaDoc;
38 import javax.jms.QueueConnectionFactory JavaDoc;
39 import javax.jms.QueueSender JavaDoc;
40 import javax.jms.QueueSession JavaDoc;
41 import javax.jms.Session JavaDoc;
42 import javax.jms.TextMessage JavaDoc;
43 import javax.naming.InitialContext JavaDoc;
44
45 import org.jboss.logging.Logger;
46
47 /** An MDB that obtains the TimerService during the ejbCreate callback. A
48  * timer is created in onMessage using this TimerService. It delays for 1
49  * seconds and continues once a second for 10 seconds.
50  *
51  * @ejb.bean name="OnCreateTimerMDB"
52  * description="OnCreateTimerMDB unit test bean"
53  * destination-type="javax.jms.Queue"
54  * acknowledge-mode="Auto-acknowledge"
55  * @ejb.resource-ref res-ref-name="jms/QCF" res-type="javax.jms.QueueConnectionFactory" res-auth="Container"
56  * @jboss.destination-jndi-name name="queue/C"
57  * @jboss.resource-ref res-ref-name="jms/QCF" jndi-name="ConnectionFactory"
58  *
59  * @author Scott.Stark@jboss.org
60  * @version $Revision: 37406 $
61  */

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