use of com.rabbitmq.client.DeliverCallback in project study-by-myself by Howinfun.
the class Receive2 method main.
public static void main(String[] args) throws Exception {
ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queueName, false, false, false, null);
channel.basicQos(1);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
ThreadUtil.sleep(4, TimeUnit.SECONDS);
System.out.println(message);
// 是否批量提交
boolean multiple = false;
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), multiple);
};
boolean autoAck = false;
channel.basicConsume(queueName, autoAck, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class WorkConsumerAckImmediately 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.queueDeclare(queueName, true, false, false, null);
channel.basicQos(1);
// 由于消息推送是异步的,需要定义回调
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(message + " Done");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
try {
Thread.sleep(70000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
channel.basicConsume(queueName, false, deliverCallback, (consumerTag, sig) -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class SubscribeConsumerTemporaryQueue2 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 WorkConsumer1 method main.
/**
* By default, RabbitMQ will send each message to the next consumer, in sequence.
* On average every consumer will get the same number of messages.
* This way of distributing messages is called round-robin
* 默认情况下,多个consumer会按顺序消费队列消息,模式称为:round-robin
* consumer之间是对等的公平的
* @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