gpt4 book ai didi

java - RabbitMQ 订阅者超时

转载 作者:行者123 更新时间:2023-11-29 09:13:01 24 4
gpt4 key购买 nike

我在穿越 CISCO ASA 5505 防火墙时订阅 RabbitMQ 消息时遇到问题。防火墙中似乎有某种超时会关闭空闲连接并导致我的 RabbitMQ 订阅被静默删除。结果是我的订阅者没有抛出/显示任何异常,但没有收到已发布的消息。

public class RabbitMqSubscriber<T extends Serializable> implements Subscriber<T> {

private QueueingConsumer consumer;
private MessageListener<T> listener;
private String exchange;
private String topic;
public RabbitMqSubscriber(String host,String exchange,String topic) throws IOException {
this.exchange=exchange;
this.topic=topic;
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setRequestedHeartbeat(10);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(exchange, "topic");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, exchange, topic);
consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
}

public void run() {

while (true) {
QueueingConsumer.Delivery delivery;
try {
delivery = consumer.nextDelivery();
Object o=SerializationUtils.deserialize(delivery.getBody());
listener.receive((T)o);
} catch (ShutdownSignalException | ConsumerCancelledException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}

@Override
public void setListener(MessageListener<T> listener) {
this.listener=listener;
}

}

我也尝试过将 keepalive 添加到服务器配置中,但这也没有帮助:

[        {rabbit, [{tcp_listen_options, [binary, 
{packet, raw},
{reuseaddr, true},
{backlog, 128},
{nodelay, true},
{exit_on_close, false},
{keepalive, true}]}]}].

最佳答案

尝试使用您的连接工厂设置连接超时:

factory.setConnectionTimeout(timeout);

关于java - RabbitMQ 订阅者超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11245626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com