2015-10-05 178 views
1

如何配置我的J2EE应用程序,以便可以与Tomcat服务器一起调用ActiveMQ服务?我知道关于嵌入式代理,这里询问如何启动ActiveMQ,只要我启动tomcat如何在tomcat启动时启动ActiveMQ?

当前代码(正常工作): 现在我想删除main()方法并使用代码在tomcat运行时运行。

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); 
}} 

这里是我的消费:

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); 
} 

}

+0

您是否尝试从您的客户中删除'connection.close()'? –

+0

感谢您的时间。请检查我的更新 –

+0

由于它是java程序,它应该在jvm(java.exe)中运行。可能性是运行你的客户端在像tomcat这样的服务器上运行,或者作为主线程运行。 –

回答

1

@Tim Bish说的是正确的。你或者需要有一个计时器,例如接收器应该监听1小时 - 或者在程序终止之前使其可用。你需要这两种情况下一次启动消费计划:

更改receiveJMS方法如下:

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 
    } 

} 

上述程序将听取高达1小时。如果您希望只要程序运行,请删除finally块。但推荐的方法是以某种方式关闭它。由于您的应用程序似乎是独立的,因此您可以检查java runtime shutdown hook,您可以在程序终止时指定如何释放这些资源。

如果您的消费者是一个Web应用程序,您可以在ServletContextlistner关闭它。

1

你不给消费者应用程序的任何时间以实际收到一条消息,您创建它,那么你将其关闭。您需要使用定时接收调用来同步接收来自队列的消息,或者您需要在主方法中添加某种等待(例如CountDownLatch等),以允许异步onMessage调用在处理完该消息已完成。

+0

您正在询问一个普通的java应用程序,因此需要一个主要方法,对于服务类型方案来看待像Tomcat这样的容器或某些类似的容器,JMS对容器或服务没有定义。 –