简体   繁体   English

ActiveMQ Java STOMP 客户端收到 SocketTimeoutException

[英]ActiveMQ Java STOMP client receives SocketTimeoutException

There is an ActiveMQ server working on CentOS machine. CentOS 机器上有一个 ActiveMQ 服务器。 I can connect and consume messages with TCP and HTTP using the OpenWire JMS client.我可以使用 OpenWire JMS 客户端与 TCP 和 HTTP 连接和使用消息。 However, When I tried with the ActiveMQ test STOMP client it throws this exception on connection.receieve ;但是,当我尝试使用 ActiveMQ 测试 STOMP 客户端时,它会在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)

Listener Class监听器 Class

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

This is the connector in activemq.xml :这是activemq.xml的连接器:

<transportConnectors>
    <transportConnector name="stomp" uri="stomp://localhost:61613"/>
</transportConnectors>

Did you get similar exception before?您之前是否遇到过类似的异常?

The java.net.SocketTimeoutException is expected when the STOMP subscriber receives no message within the specified timeout.当 STOMP 订阅者在指定的超时时间内未收到任何消息时,预计会出现java.net.SocketTimeoutException 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 .此时客户端应该收到消息并通过您的System.out.println打印它。

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/ .此外,在 ActiveMQ 5.x 中,当从 STOMP 客户端订阅目的地时,您需要在目的地名称前加上/queue//topic/前缀。 You aren't doing this in your application.您没有在您的应用程序中执行此操作。 Try using this:尝试使用这个:

connection.subscribe("/topic/TEST_TOPIC", Stomp.Headers.Subscribe.AckModeValues.CLIENT);

Lastly, it's worth noting that you're using the test STOMP client from the ActiveMQ code-base.最后,值得注意的是,您正在使用 ActiveMQ 代码库中的测试STOMP 客户端。 This client is used by ActiveMQ's internal test-suite to verify the broker implementation is working as expected. ActiveMQ 的内部测试套件使用此客户端来验证代理实现是否按预期工作。 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.此外,如果您使用的是 Java,您最好使用性能更好、功能更全的客户端,例如 OpenWire JMS 客户端甚至 Qpid JMS 客户端。

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

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