在穿越 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}]}]}].