java – 如何从Activemq异步拉取消息

我想编写用于从Activemq中提取消息的代码.我不想一次从Activemq中提取所有消息,因为我的要求是每当我的Java应用程序从Activemq收到1条消息时,基于消息体我将找到相应的HTTP链接并转发到该链接.对于这整个逻辑,我写了2个.java文件名

MessageConsumer.java

MyListener.java

MessageConsumer.java文件仅用于连接建立.相应的代码如下.

 package PackageName;
 import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.jms.*;
 import org.apache.activemq.ActiveMQConnectionFactory;
 public class MessageConsumer extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
    throws ServletException, IOException {
    try {
      //creating connectionfactory object for way
      ConnectionFactory connectionFactory=new        
      ActiveMQConnectionFactory("admin","admin","tcp://localhost:61617");
     //establishing the connection b/w this Application and Activemq
     Connection connection=connectionFactory.createConnection();
     Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
     Queue queue=session.createQueue("MessageTesing");
     javax.jms.MessageConsumer consumer=session.createConsumer(queue);
     //fetching queues from Activemq
     MessageListener listener = new MyListener();
    consumer.setMessageListener(listener);
    connection.start();
    System.out.println("Press a key to terminate");
    }
  catch (Exception e) {
    // TODO: handle exception
}
 }
}

MyListener.java文件用于触发相应的Applications.code,如下所示

package PackageName;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class MyListener implements MessageListener {
public void onMessage(Message msg) {
    try {
        TextMessage msg1=(TextMessage)msg;
        //just for your understanding I mention dummy code
        System.out.println(msg1.getText());
        if (msg1.getText()=="Google") {
            System.out.println("Forwarding http link to Google");
        }
        else {
            System.out.println("Forwarding http link to Facebook");
        }
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

在我的帖子中,我正在触发Google和Facebook链接.但是就我的要求而言,我将调用我自己的Applications.each应用程序花费超过20分钟.所以我想逐个拉消息.之前的消息处理完成然后只有它将收到来自Activemq的另一条消息.

但是知道我一次收到所有的消息.我怎么能解决这个问题.我看过Activemq-Hellowworld计划.我不明白.

对不起,我是Java技术的新手.任何人都可以帮助我.

谢谢.

最佳答案
如果您正在使用MessageListener,那么您实际上是异步接收消息(在另一个线程中).

您可能正在寻找同步消息接收,因此请在主线程中尝试:

final QueueReceiver queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();

while (true) {
  Message message = queueReceiver.receive();
  // Process your message: insert the code from MyListener.onMessage here

  // Possibly add an explit message.acknowledge() here, 
  // if you want to make sure that in case of an exception no message is lost
  // (requires Session.CLIENT_ACKNOWLEDGE, when you create the queue session)

  // Possibly terminate program, if a certain condition/situation arises
}

没有MessageListener.

receive()阻塞直到消息可用,因此您的主线程(以及您的程序)在receive方法中等待.如果消息到达,它将接收并处理它.

更新

如果你使用

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

那你应该打电话

message.acknowledge()

消息完全处理完毕后.

而在Session.AUTO_ACKNOWLEDGE的情况下,消息立即从队列中删除(因此如果程序终止处理消息,则丢失).

转载注明原文:java – 如何从Activemq异步拉取消息 - 代码日志