use of com.rabbitmq.client.DeliverCallback in project java-study by q920447939.
the class Consumer method consumer.
// @PostConstruct
public void consumer() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 第二个参数 顺序消费
channel.queueDeclare(QUEUE_NAME, true, true, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received 共产党说:'" + message + "'");
};
System.out.println(2123);
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
System.out.println("end...");
}
use of com.rabbitmq.client.DeliverCallback in project spring-parent by mingyang66.
the class Consumer method main.
public static void main(String[] args) throws Exception {
/**
* {@link Connection}的工厂类
*/
ConnectionFactory factory = new ConnectionFactory();
/**
* 设置vhost
*/
factory.setVirtualHost(ConnectionFactory.DEFAULT_VHOST);
/**
* 设置连接的主机
*/
factory.setHost("127.0.0.1");
/**
* 设置端口号
*/
factory.setPort(5673);
/**
* 用户名
*/
factory.setUsername("admin");
/**
* 密码
*/
factory.setPassword("admin");
/**
* 创建新的代理连接
*/
Connection connection = factory.newConnection();
/**
* 使用内部分配的通道号创建一个新的频道
*/
Channel channel = connection.createChannel();
/**
* prefetchCount:服务端每次分派给消费者的消息数量
*/
channel.basicQos(30);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
/**
* 当一个消息被发送过来时,将会被回调的接口
* consumerTag:与消费者相关的消费者标签
* delivery:发送过来的消息
*/
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("消费者优先级为9的消费者标识:" + consumerTag);
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
}
};
Map<String, Object> arguments = Maps.newHashMap();
/**
* 设置消息优先级
*/
arguments.put("x-priority", 9);
/**
* String basicConsume(String queue, boolean autoAck, DeliverCallback deliverCallback, CancelCallback cancelCallback)
* 启动一个消费者,并返回服务端生成的消费者标识
* queue:队列名
* autoAck:true 接收到传递过来的消息后acknowledged(应答服务器),false 接收到消息后不应答服务器
* deliverCallback: 当一个消息发送过来后的回调接口
* cancelCallback:当一个消费者取消订阅时的回调接口;取消消费者订阅队列时除了使用{@link Channel#basicCancel}之外的所有方式都会调用该回调方法
* @return 服务端生成的消费者标识
*/
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
System.out.println("消费者优先级为9的消费者标识:" + consumerTag);
});
}
use of com.rabbitmq.client.DeliverCallback in project middleware-benchmarks by Nastel.
the class RabbitConsumer method concurrentConsume.
public void concurrentConsume(String QUEUE_NAME) {
try {
myChannel.queueDeclare(QUEUE_NAME, true, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
delivery.getBody();
};
myChannel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.rabbitmq.client.DeliverCallback in project hummer-framework by hummer-team.
the class RabbitMqConsumer method start.
public void start() throws IOException, NoSuchAlgorithmException, KeyManagementException, URISyntaxException, TimeoutException {
createConnection();
channel = connection.createChannel();
channel.queueDeclare(queueConfig.getQueueName(), queueConfig.isDurable(), queueConfig.isExclusive(), queueConfig.isAutoDelete(), queueConfig.getArguments());
if (StringUtils.isNotEmpty(queueConfig.getRouteKey())) {
channel.queueBind(queueConfig.getQueueName(), queueConfig.getExchange(), queueConfig.getRouteKey());
}
channel.addShutdownListener(new ShutdownListener() {
@Override
public void shutdownCompleted(ShutdownSignalException e) {
if (!exitSignal) {
log.warn("mq shutdown completed, reason is ", e);
}
}
});
channel.addReturnListener(new ReturnListener() {
@Override
public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
log.info("consumer return {}", replyText);
}
});
channel.basicQos(queueConfig.getQos());
MqDeserializer mqDeserializer = DeserializerFactory.deserializer(queueConfig.getSerializerType());
consumerTag = channel.basicConsume(queueConfig.getQueueName(), queueConfig.isAutoAck(), new DeliverCallback() {
@Override
public void handle(String consumerTag, Delivery message) throws IOException {
T data;
try {
data = mqDeserializer.deserialize(message.getBody(), handler.messageType());
} catch (Exception e) {
log.error("deserialize message failed, topic is {} message type is {}", topic, handler.messageType(), e);
return;
}
MessageData<T> messageData = new MessageData<>();
messageData.setData(data);
messageData.setQueueOffset(message.getEnvelope().getDeliveryTag());
messageData.setProperties(Optional.ofNullable(message.getProperties().getHeaders()).orElse(Collections.emptyMap()).entrySet().stream().collect(Collectors.toConcurrentMap(Map.Entry::getKey, e -> String.valueOf(e.getValue()))));
ListenableFuture<Long> future = ((ListeningExecutorService) executorService).submit(() -> handler.handlerAndReturnOffset(messageData));
Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(@Nullable Long offset) {
ack(offset);
}
@Override
public void onFailure(Throwable throwable) {
log.warn("business handler message failed {}", topic, throwable);
}
}, executorService);
}
}, new CancelCallback() {
@Override
public void handle(String consumerTag) throws IOException {
log.warn("consumer cancel {}", consumerTag);
}
}, new ConsumerShutdownSignalCallback() {
@Override
public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) {
log.warn("consumer shutdown {} ", consumerTag, sig);
}
});
log.info("consumer {} start ok,queueConfig is {}", consumerTag, queueConfig);
}
use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.
the class MessageConsumer 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);
System.out.println("消费者订阅到消息内容:'" + message + "'");
/**
* 验证 消息没有被消费完成,mq的消息是否还能看到
* 没有任何策略的情况下,消息还没有消费结束,mq队列中的消息就已经没有了
*/
// while (true){
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// }
// }
};
channel.basicConsume(queueName, true, deliverCallback, (consumerTag, sig) -> {
});
}
Aggregations