Advertisement
kdelemme

ActiveMQ Producer

May 29th, 2012
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. package com.simpleapplication.Profiler;
  2.  
  3. import javax.jms.*;
  4.  
  5. import org.apache.activemq.ActiveMQConnection;
  6. import org.apache.activemq.ActiveMQConnectionFactory;
  7.  
  8. public class ActiveMQProducer {
  9.     // = localhost
  10.     private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
  11.     // Name of the queue
  12.     private static String subject = "Profiler";
  13.    
  14.     // Singleton
  15.     private static final ActiveMQProducer instance = new ActiveMQProducer();
  16.     private static ConnectionFactory connectionFactory;
  17.     private static Connection connection;
  18.     private static Session session;
  19.     private static Destination destination;
  20.     private static MessageProducer producer;
  21.    
  22.     private ActiveMQProducer() {
  23.         connectionFactory = new ActiveMQConnectionFactory(url);
  24.     }
  25.    
  26.     public void configure() {
  27.         try {
  28.             connection = connectionFactory.createConnection();
  29.             connection.start();
  30.             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  31.             destination = session.createQueue(subject);
  32.         } catch (JMSException e) {
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.    
  37.     public static ActiveMQProducer getInstance() {
  38.         return instance;
  39.     }
  40.    
  41.    
  42.     public void addToQueue(String stacktrace) {
  43.         try {
  44.             producer = session.createProducer(destination);
  45.             TextMessage message = session.createTextMessage(stacktrace);
  46.             producer.send(message);
  47.         } catch (JMSException e) {
  48.             e.printStackTrace();
  49.         }
  50.     }
  51.    
  52.     public void close() {
  53.         try {
  54.             connection.close();
  55.         } catch (JMSException e) {
  56.             e.printStackTrace();
  57.         }
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement