KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > perfs > MsgListener


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
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): Frederic Maistre (INRIA)
22  * Contributor(s):
23  */

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

31 public class MsgListener implements MessageListener
32 {
33   private int max;
34   private java.io.FileWriter JavaDoc writer;
35   private int overall = 10;
36   private int counter = 0;
37   private double travelT = 0;
38
39   public MsgListener(int max) throws Exception JavaDoc
40   {
41     this.max = max;
42     writer = new java.io.FileWriter JavaDoc("PerfsFile");
43   }
44
45   public void onMessage(Message msg)
46   {
47     counter++;
48     try {
49       travelT = travelT +
50                 (System.currentTimeMillis() -
51                  msg.getLongProperty("time")) / 1000;
52
53       if (counter == 10) {
54         counter = 0;
55         System.out.println("Overall counter: " + overall);
56         writer.write("" + overall + " Mean travel time (s): "
57                      + (travelT / 10) + " \n");
58
59         //msg.acknowledge();
60

61
62         travelT = 0;
63         overall = overall + 10;
64
65         if (overall == max + 10) {
66           writer.close();
67           System.out.println("Writer closed");
68           synchronized(this) {
69             this.notify();
70           }
71         }
72       }
73     }
74     catch (Exception JavaDoc exc) {}
75   }
76 }
77
Popular Tags