KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cluster > queue > MsgListener


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package cluster.queue;
24
25 import javax.jms.*;
26
27 /**
28  * Implements the <code>javax.jms.MessageListener</code> interface.
29  */

30 public class MsgListener implements MessageListener
31 {
32   String JavaDoc ident = null;
33   int nbMsg = 0;
34   long startTime = -1;
35   int sleep;
36   int nbMsgSleep;
37
38   public MsgListener() {}
39
40   public MsgListener(String JavaDoc ident) {
41     this.ident = ident;
42     int sleep = Integer.getInteger("sleep", 0).intValue();
43     int nbMsgSleep = Integer.getInteger("nbMsgSleep", 10).intValue();
44     System.out.println("sleep = " + sleep + ", nbMsgSleep=" + nbMsgSleep);
45   }
46
47   public void onMessage(Message msg) {
48     try {
49       nbMsg++;
50       if (nbMsg == 1)
51         startTime = System.currentTimeMillis();
52       long time = System.currentTimeMillis();
53       System.out.println("LastTime = " + time);
54       time = time - startTime;
55       System.out.println("time = " + time + " nbMsg=" + nbMsg);
56
57       if (msg instanceof TextMessage) {
58         if (ident == null)
59           System.out.println(((TextMessage) msg).getText());
60         else
61           System.out.println(ident + ": " + ((TextMessage) msg).getText());
62       } else if (msg instanceof ObjectMessage) {
63         if (ident == null)
64           System.out.println(((ObjectMessage) msg).getObject());
65         else
66           System.out.println(ident + ": " + ((ObjectMessage) msg).getObject());
67       }
68
69       if (sleep > 0 && (nbMsg % nbMsgSleep) == 0) {
70         try {
71           Thread.sleep(sleep);
72         } catch (Exception JavaDoc e) {}
73       }
74
75     } catch (JMSException jE) {
76       System.err.println("Exception in listener: " + jE);
77     }
78   }
79 }
80
Popular Tags