简体   繁体   中英

How to start ActiveMQ when tomcat starts?

How do I configure my J2EE application so that I can invoke ActiveMQ service along with tomcat server? I am aware about embedded broker, here asking how to start the ActiveMQ whenever I start tomcat

Current Code (works fine) : Now I want to remove main() method and use the code to run when tomcat runs.

public class JMSService {


public void produceJMS() throws NamingException, JMSException {

    ConnectionFactory connFactory =  new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);

    Connection conn = connFactory.createConnection();

    conn.start();

    Session session = conn.createSession(false,Session.AUTO_ACKNOWLEDGE);

    Destination destination = session.createQueue("testQueue");

    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);

    TextMessage message = session.createTextMessage("Test Message ");

    // send the message
    producer.send(message);

    System.out.println("sent: " + message);
}}

Here is my consumer :

 public class JMSReceiver implements MessageListener,ExceptionListener {

public static void main(String args[]) throws Exception {

    JMSReceiver re = new JMSReceiver();
    re.receiveJMS();    
  }

 public void receiveJMS() throws NamingException, JMSException {

     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
     Connection connection = connectionFactory.createConnection();
     connection.start();


     Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

     // Getting the queue 'testQueue'
     Destination destination = session.createQueue("testQueue");


     MessageConsumer consumer = session.createConsumer(destination);         

    // set an asynchronous message listener
    JMSReceiver asyncReceiver = new JMSReceiver();
    consumer.setMessageListener(asyncReceiver);

     connection.setExceptionListener(asyncReceiver);

}

@Override
public void onMessage(Message message) {

    System.out.println("Received message : " +message);
}

}

You aren't giving the consumer application any time to actually receive a message, you create it, then you close it. You either need to use a timed receive call to do an sync receive of the message from the Queue or you need to add some sort of wait in the main method such as a CountDownLatch etc to allow the async onMessage call to trigger shutdown once processing of the message is complete.

What @Tim Bish said is correct. You either need to have a timer say for example receiver should listen for 1 hour- or make it available until program terminate. Either case you need to start your consumer program once:

Change your receiveJMS method as follows:

 public void receiveJMS() throws NamingException, JMSException {
try{
     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
     Connection connection = connectionFactory.createConnection();
     connection.start(); // it's the start point


     Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

     // Getting the queue 'testQueue'
     Destination destination = session.createQueue("testQueue");


     MessageConsumer consumer = session.createConsumer(destination);         

    // set an asynchronous message listener
   // JMSReceiver asyncReceiver = new JMSReceiver(); 
   //no need to create another object
    consumer.setMessageListener(this);

     connection.setExceptionListener(this);

     // connection.close(); once this is closed consumer no longer active

    Thread.sleep(60 *60 * 1000);             // receive messages for 1 hour
   }finally{
      connection.close();// after 1 hour close it
   }

}

The above program will listen upto 1 hour. If you want it as long as the program run, remove the finally block. But the recommended way is to close it somehow. since your application seems to be standalone ,you can check the java runtime shutdown hook , where you can specify how to release such resources while program terminates.

If your consumer is a web application you can close it in a ServletContextlistner .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM