ActiveMQ初体验

实例搭建参考:https://blog.csdn.net/zsg88/article/details/76359618

搭建完成之后,执行MQProducerTest中的main方法发送两条消息(一条默认队列,三条其他队列)之后,再执行MQConsumerTest中的main方法获取到生产者发送的信息,第一次执行,发送消息成功,接受消息失败:

producter:



customer:


在上一步的基础上,因为看到context上有黄色警告,鼠标放上去显示


作为一个有洁癖的程序员,不能容忍这种现象,于是把context提出去,在下面加了一个finally块:


这下没有警告了,再次执行下customer:


诶!我的消息呢?反应了半天才醒悟:消息已经被消费了,还哪来的消息!重新执行 producter-->customer:


消息生产者正常运行,继续执行消息消费测试:


消息呢?还没消费呢哪去了?仔细一看,有警告!什么鬼?

错误信息:

Rejecting received message because of the listener container having been stopped in the meantime: ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:DESKTOP-HCAROEL-51395-1530667650235-1:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:DESKTOP-HCAROEL-51395-1530667650235-1:1:1:1, destination = queue://default_queue, transactionId = null, expiration = 0, timestamp = 1530667650638, arrival = 0, brokerInTime = 1530667648634, brokerOutTime = 1530668220704, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@1e7895b1, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = oo activeMq Test!}

字面意思是由于监听器已经停止了导致消息被拒绝接受,但是为什么会出现这种现象呢?

不会怎么办,找度娘呗!于是,让我找到了这个:

详情请转到:https://stackoverflow.com/questions/8120866/spring-defaultmessagelistenercontainer-listener-not-reading-messages-on-websph

The issue is resolved. The test class was terminating after the listener got hold of the message but before it could show the message as output. So the first message (highest priority one) was getting lost from the queue.

Later as I had included a Transaction Manager, the listener was putting the message back on the queue (showing a warning as Rejecting received message because of the listener container having been stopped in the meantime). As this was a warning and my logger was at a Debug level, I missed this earlier.

Putting a thread.sleep in the test class made sure that it is running for a longer time and the listener could read all the messages in the queue in the order of priority :)

cheers,

意思就是说在消息传输过去的时候监听器已经停止工作或者正在停止过程中了,尽管消息已经送到消费者端了,但是由于监听器已经停止工作导致消费者无法接收到消息,这种情况下还会导致优先级最高的一条消息丢失!可以通过使用Thread.sleep(times),来确保运行更长的时间再close(),可以解决这样的问题。

说的很明白了,使用Thread.sleep(),接着走:



运行:


可以看出,最上面的两条消息已经丢失(返回两条reject日志消息,每条丢失一条数据)。

重新执行producter-->customer测试:

producter(没问题):


customer(正常):



上面的操作是给加了close了,每次执行完producter之后还得重新执行下customer才能接收到消息,如果不加close,会不会自动接收消息了?于是我注掉close,又打开一个新的编辑器窗口,导入同一个项目,一个窗口发送消息,一个窗口接收消息:


执行生产消息producter:


(第一次)执行消费消息,开启消费:


这里看到两条hello!world是由于我执行了两遍生产消息protucter没有消费的缘故。

修改消息内容,再次发送消息protucter:



可以看到消费者端自动获取消息:



总结:消息队列activeMQ的原理(或者说消息队列)就是消息生产者与消息消费者中间多了个消息‘’存放仓库‘’,生产者将生产的消息放到仓库中,消费者从消息仓库获取消息‘’消费‘’,‘’消费‘’后的消息即从仓库中移除,未‘’消费‘’的消息会继续存储,消息‘’消费‘’监听器未关闭的前提下,会处理完当前消息后继续从仓库中提取消息进行‘’消费‘’,直到消息仓库中的消息全部被‘消费’。

至于消息仓库中的消息存储方式,还需进一步研究。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值