Search in sources :

Example 31 with Envelope

use of com.rabbitmq.client.Envelope in project tech by ffyyhh995511.

the class MQConsumer method doGSMWork.

public void doGSMWork() throws Exception {
    final Connection connection = MQServer.getConnection();
    final Channel channel = connection.createChannel();
    // 消息持久化
    boolean durable = true;
    channel.queueDeclare(MQServer.GSM_QUEUE, durable, false, false, null);
    // 指定该线程同时只接收一条消息
    channel.basicQos(1);
    final Consumer consumer = new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            Object gsmMessage = JSON.parseObject(message, Object.class);
            log.info(" [Consumer] Received : " + message);
            Map<String, String> map = null;
            // 只有服务端返回非失败才发ack
            if ("1".equals(map.get("status"))) {
                // envelope.getDeliveryTag() 获取消息ID
                // 接口返回消息除非网络异常和接口异常,其他(参数错误、已处理等)都做确认消息动作
                // 返回接收到消息的确认信息
                channel.basicAck(envelope.getDeliveryTag(), false);
            } else if ("2".equals(map.get("status"))) {
                // 如果nack的requeue = false,mq把消息直接丢弃
                // message requeue = true
                channel.basicNack(envelope.getDeliveryTag(), false, true);
            } else if ("3".equals(map.get("status"))) {
                // 参数不合法直接丢弃
                channel.basicNack(envelope.getDeliveryTag(), false, false);
            }
            log.info(" [Consumer] Done :" + message);
        }
    };
    // 打开消息应答机制
    channel.basicConsume(MQServer.GSM_QUEUE, false, consumer);
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) Envelope(com.rabbitmq.client.Envelope)

Aggregations

Envelope (com.rabbitmq.client.Envelope)31 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)20 AMQP (com.rabbitmq.client.AMQP)18 IOException (java.io.IOException)17 Channel (com.rabbitmq.client.Channel)16 Test (org.junit.Test)12 Connection (com.rabbitmq.client.Connection)11 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)10 TimeoutException (java.util.concurrent.TimeoutException)9 GetResponse (com.rabbitmq.client.GetResponse)8 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)6 LongString (com.rabbitmq.client.LongString)5 Consumer (com.rabbitmq.client.Consumer)4 BasicProperties (de.gessnerfl.rabbitmq.queue.management.model.BasicProperties)4 Message (de.gessnerfl.rabbitmq.queue.management.model.Message)4 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UUID (java.util.UUID)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Path (javax.ws.rs.Path)3