KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > stats > ejb > EjbStatisticsMessageSender


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
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 (at your option) any later version.
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 USA
18  */

19 package za.org.coefficient.util.stats.ejb;
20
21 import za.org.coefficient.core.Constants;
22 import za.org.coefficient.interfaces.StatisticsMessageSender;
23
24 import javax.jms.MapMessage JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.QueueConnection JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.QueueSender JavaDoc;
29 import javax.jms.QueueSession JavaDoc;
30 import javax.jms.Session JavaDoc;
31
32 import javax.naming.InitialContext JavaDoc;
33
34 /**
35  * This is the Ejb implementation of the statisticMessageSender. This implementation
36  * relies on a JMS provider and a configured statistics message queue.
37  */

38 public class EjbStatisticsMessageSender implements StatisticsMessageSender {
39     //~ Static fields/initializers =============================================
40

41     private InitialContext JavaDoc ctx = null;
42     private QueueConnection JavaDoc queueConnection = null;
43     private QueueSession JavaDoc queueSession = null;
44     private QueueSender JavaDoc queueSender = null;
45     private boolean connected = true;
46
47     //~ Methods ================================================================
48

49     public EjbStatisticsMessageSender() {
50         this.init();
51     }
52
53     public void reportStatistic(Long JavaDoc projectId, String JavaDoc moduleName, String JavaDoc action) {
54         if ((projectId == null) || (action == null)) {
55             throw new IllegalArgumentException JavaDoc("project and action must not be null to send a message");
56         } else if(!connected) {
57             init();
58             if(!connected) {
59                 System.err.println("<< The statistics queue is unavailable");
60             }
61         } else {
62             try {
63                 MapMessage JavaDoc mm = queueSession.createMapMessage();
64                 if (projectId != null) {
65                     mm.setLong("project", projectId.longValue());
66                 }
67                 if (moduleName != null) {
68                     mm.setString("name", moduleName);
69                 }
70                 mm.setString("action", action);
71                 
72                 queueSender.send(mm);
73             } catch (Exception JavaDoc e) {
74                 System.out.println("MessageSender: initialize - " + e);
75                 e.printStackTrace();
76             }
77         }
78     }
79     
80     private void init() {
81         try {
82             // Locate the JNDI context root
83
ctx = new InitialContext JavaDoc();
84
85             // Get the connection factory
86
QueueConnectionFactory JavaDoc connectionFactory =
87                 (QueueConnectionFactory JavaDoc) ctx.lookup(Constants.JMS_CONNECTION_FACTORY);
88
89             // Create the connection
90
queueConnection = connectionFactory.createQueueConnection();
91
92             // Create the session
93
queueSession =
94                 queueConnection.createQueueSession( // No transaction
95
false,
96                 // Auto ack
97
Session.AUTO_ACKNOWLEDGE);
98
99             // Look up the destination
100
Queue JavaDoc queue = (Queue JavaDoc) ctx.lookup(Constants.JMS_STATISTICS_QUEUE);
101
102             // Create a publisher
103
queueSender = queueSession.createSender(queue);
104         } catch (Exception JavaDoc e) {
105             // render this usless
106
connected = false;
107             e.printStackTrace();
108         } finally {
109             try {
110                 if (ctx != null) {
111                     ctx.close();
112                 }
113             } catch (Exception JavaDoc e) {
114                 e.printStackTrace();
115             }
116         }
117     }
118 }
119
Popular Tags