Connection in RabbitMQ server auto lost after 600s

佐手、 提交于 2019-12-22 04:39:09

问题


I'm using rabbitMQ server with amq.

I am having a difficult problem. After leaving the server alone for about 10 min, the connection is lost.

What could be causing this?


回答1:


The default connection timeout for the RabbitMQ connection factory is 600 seconds (at least in the Java client API), hence your 10 minutes. You can change this by specifying to the connection factory your timeout of choice.

It is good practice to ensure your connection is release and recreated after a specific amount of time, to prevent eventual leaks and excessive resournces. Your code should ensure that it seeks a valid connection that is not close to be timed-out, and re-establish a new connection on the ones that did time-out. Overall, adopt a connection-pooling approach.

- Java example:

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.serverName);
    factory.setPort(this.serverPort);
    factory.setUsername(this.userName);
    factory.setPassword(this.userPassword);
    factory.setConnectionTimeout( YOUR-TIMEOUT-IN-SECONDS ); 

    Connection = factory.newConnection();



回答2:


If you look at the Erlang client documentation http://www.rabbitmq.com/erlang-client-user-guide.html you will see a section titled Connecting To A Broker

This gives you a few different options that you can specify when setting up your connection to the RabbitMQ server, one of the options is the heartbeat, as you can see the default is 0 so no heartbeat is specified.

I don't know the exact Erlang notation, but you will need to do something like:

{ok, Connection} = amqp_connection:start(#amqp_params_network{heartbeat = 5})

The heartbeat timeout is specified in seconds. So this would cause your consumer to heartbeat back to the server every 5seconds.

Also take a look at this discussion: https://groups.google.com/forum/?fromgroups=#!topic/rabbitmq-discuss/u227xzvqOr8



来源:https://stackoverflow.com/questions/15150207/connection-in-rabbitmq-server-auto-lost-after-600s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!