简体   繁体   English

StompConnection的ActiveMQ寄存器侦听器

[英]ActiveMQ register listener to StompConnection

I'm using a variation of the example at http://svn.apache.org/repos/asf/activemq/trunk/assembly/src/release/example/src/StompExample.java to receive message from a queue. 我在http://svn.apache.org/repos/asf/activemq/trunk/assembly/src/release/example/src/StompExample.java使用示例的变体来接收队列中的消息。 What I'm trying to do is to keep listening to a queue and perform some action upon reception of a new message. 我正在尝试做的是继续侦听队列,并在收到新消息时执行一些操作。 The problem is that I couldn't find a way to register a listener to any of the related objects. 问题是我找不到将侦听器注册到任何相关对象的方法。 I've tried something like: 我已经尝试过类似的东西:

public static void main(String args[]) throws Exception {
    StompConnection connection = null;
    try {
        connection = new StompConnection();
        connection.open("localhost", 61613);
        connection.connect("admin", "activemq");
        connection.subscribe("/queue/worker", Subscribe.AckModeValues.AUTO);
        while (true) {
            StompFrame message = connection.receive();
            System.out.println(message.getBody());
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

but this doesn't work as a time out occurs after a few seconds ( java.net.SocketTimeoutException: Read timed out ). 但这不起作用,因为几秒钟后发生了java.net.SocketTimeoutException: Read timed outjava.net.SocketTimeoutException: Read timed out )。 Is there anything I can do to indefinitely listen to this queue? 有什么我可以无限期收听此队列的方法吗?

ActiveMQ's StompConnection class is a relatively primitive STOMP client. ActiveMQ的StompConnection类是一个相对原始的STOMP客户端。 Its not capable of async callbacks on Message or for indefinite waits. 它不能在Message上进行异步回调或无限期等待。 You can pass a timeout to receive but depending on whether you are using STOMP v1.1 it could still timeout early if a heart-beat isn't received in time. 您可以传递一个超时来接收,但是取决于是否使用STOMP v1.1,如果没有及时接收到心跳,它仍可能会提前超时。 You can of course always catch the timeout exception and try again. 您当然可以随时捕获超时异常,然后重试。

For STOMP via Java you're better off using StompJMS or the like which behaves like a real JMS client and allows for async Message receipt. 对于通过Java的STOMP,最好使用StompJMS或类似的工具,其行为类似于真实的JMS客户端,并允许异步消息接收。

@Tim Bish: I tried StompJMS, but couldn't find any example that I could use (maybe you can provide a link). @Tim Bish:我尝试了StompJMS,但是找不到我可以使用的任何示例(也许您可以提供链接)。 I 'fixed' the problem by setting the timeout to 0 which seems to be blocking. 我通过将超时设置为0(似乎正在阻止)来“解决”该问题。

even i was facing the same issue.. you can fix this by adding time out to your receive() method. 即使我也面临同样的问题..您可以通过将超时添加到您的receive()方法来解决此问题。

Declare a long type variable. 声明一个长型变量。

long waitTimeOut = 5000;   //this is 5 seconds

now modify your receive function like below. 现在,如下所示修改您的接收功能。

StompFrame message = connection.receive(waitTimeOut);

This will definitely work. 这肯定会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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