Heart-beating in STOMP client

obsessiveCookie picture obsessiveCookie · Mar 9, 2014 · Viewed 8.3k times · Source

The design of my current stomp client process is as follows:

  1. Open stomp connection (sending CONNECT frame)
  2. Subscribe to a feed (send a SUBSCRIBE frame)
  3. Do a loop to continually receive feed:
while (true) {
                connection.begin("txt1");

                    StompFrame message = connection.receive();
                    System.out.println("message get header"+message.toString());
                    LOG.info(message.getBody());

                    connection.ack(message, "txt1");

                connection.commit("txt1");

            }

My problem with this process is that I get

java.net.SocketTimeoutException: Read timed out
 at java.net.SocketInputStream.socketRead0(Native Method)...

and I think the cause of this is mostly because the feed I am subscribed to gives information slower on certain times (as I normally get this error when the weekend comes, holidays or evenings).

I have been reading up on this here and I think this would help with my problem. However, I'm not so sure how to incorporate it with the current layout of my stomp client. Would I have to send a CONNECT header within Step 3?

I am currently using activemq to create my stomp client if that helps.

In the stomp spec we have:

Regarding the heart-beats themselves, any new data received over the network connection is an indication that the remote end is alive. In a given direction, if heart-beats are expected every milliseconds:

  • the sender MUST send new data over the network connection at least every milliseconds
  • if the sender has no real STOMP frame to send, it MUST send a single newline byte (0x0A)
  • if, inside a time window of at least milliseconds, the receiver did not receive any new data, it CAN consider the connection as dead
  • because of timing inaccuracies, the receiver SHOULD be tolerant and take into account an error margin

Would that mean my client would need to send a newline bye every n seconds?

Answer

Rob Newton picture Rob Newton · Sep 15, 2014

The stomp server you are connected to has timed out your connection due to innactivity.

Providing the server supports Stomp version 1.1 or newer, the easiest solution for your client is to include a heart-beat instruction in the header of your CONNECT, such as "0,10000". This tells the server that you cannot send heart-beats, but you want it to send one every 10 seconds. This way you don't need to implement them, and the server will keep the connection active by sending them to you.

Of course the server will have its own requirements of the client. In your comment it responds to your request with "1000,0". This indicates that it will send a heart-beat every 1000 millisecs, and it expects you to send one every 0 millisecs, 0 indicating none at all. So your job will be minimal.