赞
踩
接收消息的正确代码
-
- /**
- * 接收消息
- */
- override fun receiveMessage(queue: String, onMessageReceiveListener: OnMessageReceiveListener) {
- if (!ensureChannelOpen()) return
- try {
- val consumer = object : DefaultConsumer(mChannel) {
- override fun handleDelivery(consumerTag: String?, envelope: Envelope?, properties: AMQP.BasicProperties?, body: ByteArray?) {
- if (body != null) {
- onMessageReceiveListener.receivedMessage(String(body, Charsets.UTF_8))
- mChannel?.basicAck(envelope?.deliveryTag ?: 0L, false)
- }
- }
- }
- mChannel?.basicConsume(queue, false,"xtsf_"+DataHelper.getUserId(), consumer)
- } catch (e: Exception) {
- Log.d(TAG, Log.getStackTraceString(e))
- }
- }
这里之前一直是收到消息但是basicAck调用之后没有让服务器删消息 原因就是 basicConsume,方法的第三个参数必须设值
而且最好是每个消费者不同,否则,就算调用basicAck也没用,服务器不会删消息
参考官方文档,谷歌翻译一波绝对受益匪浅https://www.rabbitmq.com/api-guide.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。