问题描述
在 CentOS 机器上有一个 ActiveMQ 服务器.我可以使用 OpenWire JMS 客户端通过 TCP 和 HTTP 连接和使用消息.但是,当我尝试使用 ActiveMQ 测试 STOMP 客户端时,它会在 connection.receieve
;
There is an ActiveMQ server working on CentOS machine. I can connect and consume messages with TCP and HTTP using the OpenWire JMS client. However, When I tried with the ActiveMQ test STOMP client it throws this exception on connection.receieve
;
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.net.SocketInputStream.read(SocketInputStream.java:224)
at java.io.DataInputStream.readByte(DataInputStream.java:265)
at org.apache.activemq.transport.stomp.StompWireFormat.readHeaderLine(StompWireFormat.java:174)
at org.apache.activemq.transport.stomp.StompWireFormat.readLine(StompWireFormat.java:167)
at org.apache.activemq.transport.stomp.StompWireFormat.parseAction(StompWireFormat.java:200)
at org.apache.activemq.transport.stomp.StompWireFormat.unmarshal(StompWireFormat.java:112)
at org.apache.activemq.transport.stomp.StompConnection.receive(StompConnection.java:77)
at tr.com.estherial.stomplistener.StompListener.main(StompListener.java:25)
监听类
import org.apache.activemq.transport.stomp.Stomp;
import org.apache.activemq.transport.stomp.StompConnection;
import org.apache.activemq.transport.stomp.StompFrame;
public class StompListener {
public static void main(String[] args) {
StompConnection connection = new StompConnection();
try {
connection.open("host", 61613);
connection.connect("admin", "admin", "test");
connection.subscribe("TEST_TOPIC", Stomp.Headers.Subscribe.AckModeValues.CLIENT);
connection.begin("test");
while (true) {
try {
StompFrame message = connection.receive(10000);
System.out.println(String.format("%s - Receiver: received '%s'", new Date(), message.getBody()));
} catch (SocketTimeoutException e) {
// ignore
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是activemq.xml
中的连接器:
<transportConnectors>
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
</transportConnectors>
您之前是否遇到过类似的异常?
Did you get similar exception before?
推荐答案
java.net.SocketTimeoutException
当 STOMP 订阅者在指定的超时时间内没有收到任何消息时.一旦客户创建了他的订阅,您需要发送一条消息到该主题.此时客户端应该收到消息并通过您的 System.out.println
打印它.
The java.net.SocketTimeoutException
is expected when the STOMP subscriber receives no message within the specified timeout. Once the client creates his subscription you need to send a message to the topic. At that point the client should receive the message and print it via your System.out.println
.
此外,在 ActiveMQ 5.x 中,当从 STOMP 客户端订阅目的地时,您需要在目的地名称前加上 /queue/
或 /topic/
前缀.您没有在应用程序中执行此操作.尝试使用这个:
Also, in ActiveMQ 5.x when subscribing to a destination from a STOMP client you need to prefix the destination name with either /queue/
or /topic/
. You aren't doing this in your application. Try using this:
connection.subscribe("/topic/TEST_TOPIC", Stomp.Headers.Subscribe.AckModeValues.CLIENT);
最后,值得注意的是,您正在使用来自 ActiveMQ 代码库的 test STOMP 客户端.ActiveMQ 的内部测试套件使用此客户端来验证代理实现是否按预期工作.它不适合一般用途.此外,如果您使用的是 Java,则最好使用性能更好、功能更全面的客户端,例如 OpenWire JMS 客户端甚至 Qpid JMS 客户端.
Lastly, it's worth noting that you're using the test STOMP client from the ActiveMQ code-base. This client is used by ActiveMQ's internal test-suite to verify the broker implementation is working as expected. It's not meant for general use. Furthermore, if you're using Java you would be better off using a better performing and more full-featured client like the OpenWire JMS client or even the Qpid JMS client.
这篇关于ActiveMQ Java STOMP 客户端收到 SocketTimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!