1 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 ; 25 import javax.jms.Queue ; 26 import javax.jms.QueueConnection ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.QueueSender ; 29 import javax.jms.QueueSession ; 30 import javax.jms.Session ; 31 32 import javax.naming.InitialContext ; 33 34 38 public class EjbStatisticsMessageSender implements StatisticsMessageSender { 39 41 private InitialContext ctx = null; 42 private QueueConnection queueConnection = null; 43 private QueueSession queueSession = null; 44 private QueueSender queueSender = null; 45 private boolean connected = true; 46 47 49 public EjbStatisticsMessageSender() { 50 this.init(); 51 } 52 53 public void reportStatistic(Long projectId, String moduleName, String action) { 54 if ((projectId == null) || (action == null)) { 55 throw new IllegalArgumentException ("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 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 e) { 74 System.out.println("MessageSender: initialize - " + e); 75 e.printStackTrace(); 76 } 77 } 78 } 79 80 private void init() { 81 try { 82 ctx = new InitialContext (); 84 85 QueueConnectionFactory connectionFactory = 87 (QueueConnectionFactory ) ctx.lookup(Constants.JMS_CONNECTION_FACTORY); 88 89 queueConnection = connectionFactory.createQueueConnection(); 91 92 queueSession = 94 queueConnection.createQueueSession( false, 96 Session.AUTO_ACKNOWLEDGE); 98 99 Queue queue = (Queue ) ctx.lookup(Constants.JMS_STATISTICS_QUEUE); 101 102 queueSender = queueSession.createSender(queue); 104 } catch (Exception e) { 105 connected = false; 107 e.printStackTrace(); 108 } finally { 109 try { 110 if (ctx != null) { 111 ctx.close(); 112 } 113 } catch (Exception e) { 114 e.printStackTrace(); 115 } 116 } 117 } 118 } 119 | Popular Tags |