use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class WorkConsumerAck method main.
/**
* A timeout (30 minutes by default) is enforced on consumer delivery acknowledgement.
* This helps detect buggy (stuck) consumers that never acknowledge deliveries.
* You can increase this timeout as described in Delivery Acknowledgement Timeout.
* 消费者超时30分钟后将强制发送ACK
* If a consumer does not ack its delivery for more than the timeout value (30 minutes by default),
* its channel will be closed with a PRECONDITION_FAILED channel exception
* 超过30分钟后,channel会被关闭 并抛出异常
*
* The timeout value is configurable in [rabbitmq.conf] (in milliseconds):
*
* # 30 minutes in milliseconds
* consumer_timeout = 1800000
* # one hour in milliseconds
* consumer_timeout = 3600000
* <p>
* <p>
* 是否开启ACK 通过 boolean autoAck 参数控制
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
// 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
// 所以如果队列存在,那么直接订阅即可
channel.queueDeclare(queueName, false, false, false, null);
// 由于消息推送是异步的,需要定义回调
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
try {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
doWork(message);
System.out.println(message + " Done");
} finally {
/**
* Acknowledgement must be sent on the same channel that received the delivery.
* Attempts to acknowledge using a different channel will result in a channel-level protocol exception.
* 如果不发送ACK确认消息,则MQ不会删除队列中的消息
* 消费者有消息没有确认ack的时候,当数量达到prefetch时,mq将不会再发送消息给该消费者,直到超时
*/
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
};
/**
* autoAck = false
*/
channel.basicConsume(queueName, false, deliverCallback, (consumerTag, sig) -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class WorkConsumerDispatch method main.
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
/**
* 设置消费数量
*/
channel.basicQos(1);
// 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
// 所以如果队列存在,那么直接订阅即可
channel.queueDeclare(queueName, false, false, false, null);
// 由于消息推送是异步的,需要定义回调
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
try {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
doWork(message);
System.out.println(message + " Done");
} finally {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
};
channel.basicConsume(queueName, false, deliverCallback, (consumerTag, sig) -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class WorkConsumerDispatch2 method main.
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
/**
* 设置消费数量
*/
channel.basicQos(1);
// 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
// 所以如果队列存在,那么直接订阅即可
channel.queueDeclare(queueName, false, false, false, null);
// 由于消息推送是异步的,需要定义回调
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
try {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
doWork(message);
System.out.println(message + " Done");
} finally {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
};
channel.basicConsume(queueName, false, deliverCallback, (consumerTag, sig) -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class SubscribeConsumerTemporaryQueue method main.
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
// 临时队列,连接断开,队列消失
// amq.gen-jnnX7acGeSY50L4Wi_DK4g
String queueRandom = channel.queueDeclare().getQueue();
channel.queueBind(queueRandom, exchangeName, "");
// 由于消息推送是异步的,需要定义回调
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
doWork(message);
System.out.println(message + " Done");
};
channel.basicConsume(queueRandom, true, deliverCallback, (consumerTag, sig) -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class WorkConsumer2 method main.
/**
* 启动后代码不会退出
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
// 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
// 所以如果队列存在,那么直接订阅即可
channel.queueDeclare(queueName, false, false, false, null);
// 由于消息推送是异步的,需要定义回调
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
doWork(message);
System.out.println(message + " Done");
};
channel.basicConsume(queueName, true, deliverCallback, (consumerTag, sig) -> {
});
}
Aggregations