我在http://svn.apache.org/repos/asf/activemq/trunk/assembly/src/release/example/src/StompExample.java使用示例的变体从队列接收消息。我正在尝试做的是继续收听队列并在收到新消息后执行一些操作。问题是我找不到将侦听器注册到任何相关对象的方法。我试过类似的东西:
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();
}
}
}
但这不起作用,因为几秒钟后会发生超时(java.net.SocketTimeoutException: Read timed out
)。我能做些什么来无限期地听这个队列吗?