RabbitMQ-01-每一步都是坑
01:主机无法访问虚拟机MQ查询指定端口是否已开firewall-cmd --query-port=15672/tcp添加指定需要开放的端口:firewall-cmd --add-port=15672/tcp --permanent重载入添加的端口:firewall-cmd --reload查询指定端口是否开启成功:firewall-cmd --query-port=15672/tcp移除指定端口
·
01:主机无法访问虚拟机MQ
查询指定端口是否已开
firewall-cmd --query-port=15672/tcp
添加指定需要开放的端口:
firewall-cmd --add-port=15672/tcp --permanent
重载入添加的端口:
firewall-cmd --reload
查询指定端口是否开启成功:
firewall-cmd --query-port=15672/tcp
移除指定端口:
firewall-cmd --permanent --remove-port=15672/tcp
查询开放的防火墙列表
firewall-cmd --zone=public --list-ports
2:com.rabbitmq.client.ShutdownSignalException: connection error
在使用浏览器可以访问,但是用程序就访问不到了。是因为浏览器访问的端口是15672。
但是用java程序连接,端口就变成了5672。
所有需要在linux中,将5672端口开放。
开放 5672端口
firewall-cmd --add-port=5672/tcp --permanent
重载入添加的端口:
firewall-cmd --reload
03:第二次接受到发送的MQ 后,发现消费获取了两个值,队列中的两个没有删除。
原因是因为MQ默认的是手动应答机制,所以要使用 autoAck = true
下面的API 是 手动应答机制
/**
* Start a non-nolocal, non-exclusive consumer, with
* explicit acknowledgement and a server-generated consumerTag.
* Provide access only to <code>basic.deliver</code> and
* <code>basic.cancel</code> AMQP methods (which is sufficient
* for most cases). See methods with a {@link Consumer} argument
* to have access to all the application callbacks.
* @param queue the name of the queue
* @param deliverCallback callback when a message is delivered
* @param cancelCallback callback when the consumer is cancelled
* @return the consumerTag generated by the server
* @throws IOException if an error is encountered
* @see com.rabbitmq.client.AMQP.Basic.Consume
* @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
* @see #basicAck
* @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer)
* @since 5.0
*/
String basicConsume(String queue, DeliverCallback deliverCallback, CancelCallback cancelCallback) throws IOException;
应使用下面API 并将 autoAck = true
/**
* Start a non-nolocal, non-exclusive consumer, with
* a server-generated consumerTag.
* Provide access only to <code>basic.deliver</code> and
* <code>basic.cancel</code> AMQP methods (which is sufficient
* for most cases). See methods with a {@link Consumer} argument
* to have access to all the application callbacks.
* @param queue the name of the queue
* @param autoAck true if the server should consider messages
* acknowledged once delivered; false if the server should expect
* explicit acknowledgements
* @param deliverCallback callback when a message is delivered
* @param cancelCallback callback when the consumer is cancelled
* @return the consumerTag generated by the server
* @throws IOException if an error is encountered
* @see com.rabbitmq.client.AMQP.Basic.Consume
* @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
* @see #basicAck
* @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer)
* @since 5.0
*/
String basicConsume(String queue, boolean autoAck, DeliverCallback deliverCallback, CancelCallback cancelCallback) throws IOException;
修改代码
/**
* 消费者消费消息
* 1.消费哪个队列
* 2.消费成功之后是否要自动应答 true 代表自动应答 false 手动应答
* 3.消费者成功消费回的调
* 4.消费者未成功消费的回调
*/
channel.basicConsume(RabbitMQUtils.QUEUE_NAME, true, deliverCallback, cancelCallback);
作者:Darren
QQ:603026148
以上内容归Darren所有,如果有什么错误或者不足的地方请联系我,希望我们共同进步。
更多推荐
已为社区贡献1条内容
所有评论(0)