use of com.rabbitmq.client.DeliverCallback in project SpringBoot-Learning by kong0827.
the class consumer02 method main.
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("47.102.218.26");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
/**
* 注意
* Rabbitmq服务通道是持久通道,该queue 已经存在, 而且通道属性需要保持一致
*/
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// DefaultConsumer consumer = new DefaultConsumer(channel) {
// /**
// *
// * @param consumerTag 标识
// * @param envelope 获取的一些信息 包括交换机 路由
// * @param properties 配置信息
// * @param body 数据
// * @throws IOException
// */
// @Override
// public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
// System.out.println("consumerTag:" + consumerTag);
// System.out.println("envelope:" + envelope);
// System.out.println("properties:" + properties);
// System.out.println("message:" + new String(body));
// }
// };
// channel.basicConsume(QUEUE_NAME, true, consumer);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" message: " + message + "'");
};
/**
* 启动一个消费者,并返回服务端生成的消费者标识
* queue:队列名
* autoAck:true 接收到传递过来的消息后acknowledged(应答服务器),false 接收到消息后不应答服务器
* deliverCallback: 当一个消息发送过来后的回调接口
* cancelCallback:当一个消费者取消订阅时的回调接口;取消消费者订阅队列时除了使用{@link Channel#basicCancel}之外的所有方式都会调用该回调方法
* @return 服务端生成的消费者标识
*/
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project SpringBoot-Learning by kong0827.
the class consumer02 method main.
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("47.102.218.26");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(TOPIC_QUEUE_2, true, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" message: " + message + "'");
};
channel.basicConsume(TOPIC_QUEUE_2, true, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project mercury by yellow013.
the class ReceiveLogsTopic method main.
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String queueName = channel.queueDeclare().getQueue();
if (args.length < 1) {
System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
System.exit(1);
}
for (String bindingKey : args) {
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
}
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 '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
};
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project mercury by yellow013.
the class ReceiveLogs method main.
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
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 + "'");
};
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project skywalking-java by apache.
the class CaseController method rabbitmqCase.
@RequestMapping("/rabbitmq")
@ResponseBody
public String rabbitmqCase() {
Channel channel = null;
Connection connection = null;
try {
ConnectionFactory factory = new ConnectionFactory();
LOGGER.info("Using brokerUrl = " + brokerUrl);
factory.setHost(brokerUrl);
factory.setPort(PORT);
factory.setUsername(USERNAME);
factory.setPassword(PASSWORD);
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder();
LOGGER.info("Message being published -------------->" + MESSAGE);
channel.basicPublish("", QUEUE_NAME, propsBuilder.build(), MESSAGE.getBytes(StandardCharsets.UTF_8));
LOGGER.info("Message has been published-------------->" + MESSAGE);
final CountDownLatch waitForConsume = new CountDownLatch(1);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
LOGGER.info("Message received-------------->" + message);
waitForConsume.countDown();
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
waitForConsume.await(5000L, TimeUnit.MILLISECONDS);
LOGGER.info("Message Consumed-------------->");
} catch (Exception ex) {
LOGGER.error(ex.toString());
} finally {
if (channel != null) {
try {
channel.close();
} catch (Exception e) {
// ignore
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
// ignore
}
}
}
return "Success";
}
Aggregations