use of com.rabbitmq.client.DeliverCallback in project rabbitmq-tutorials by rabbitmq.
the class ReceiveLogsTopic method main.
public static void main(String[] argv) 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 (argv.length < 1) {
System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
System.exit(1);
}
for (String bindingKey : argv) {
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 rabbitmq-tutorials by rabbitmq.
the class Worker method main.
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
channel.basicQos(1);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
try {
doWork(message);
} finally {
System.out.println(" [x] Done");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
};
channel.basicConsume(TASK_QUEUE_NAME, false, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project quarkus-quickstarts by quarkusio.
the class QuoteProcessorTest method testProcessor.
@Test
void testProcessor() throws Exception {
String quoteId = UUID.randomUUID().toString();
Channel channel = getChannel();
channel.exchangeDeclare("quotes", TOPIC, true, false, Map.of());
String queue = channel.queueDeclare("quotes", true, false, false, Map.of()).getQueue();
channel.queueBind(queue, "quotes", "#");
AtomicReference<Quote> receivedQuote = new AtomicReference<>(null);
DeliverCallback deliverCallback = (consumerTag, message) -> {
Quote quote = objectMapper.readValue(message.getBody(), Quote.class);
if (!Objects.equals(quote.id, quoteId)) {
return;
}
receivedQuote.set(quote);
};
String consumerTag = channel.basicConsume(queue, true, deliverCallback, tag -> {
});
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().contentType("text/plain").build();
channel.basicPublish("quote-requests", quoteId, props, quoteId.getBytes(UTF_8));
await().atMost(3, SECONDS).untilAtomic(receivedQuote, notNullValue());
channel.basicCancel(consumerTag);
}
use of com.rabbitmq.client.DeliverCallback in project mt-auth by publicdevop2019.
the class RabbitMQEventStreamService method subscribe.
@Override
public void subscribe(String subscribedApplicationName, boolean internal, @Nullable String fixedQueueName, Consumer<StoredEvent> consumer, String... topics) {
String routingKeyWithoutTopic = subscribedApplicationName + "." + (internal ? "internal" : "external") + ".";
String queueName;
if (fixedQueueName != null) {
queueName = fixedQueueName;
} else {
long id = CommonDomainRegistry.getUniqueIdGeneratorService().id();
queueName = Long.toString(id, 36);
}
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
log.trace("mq message received");
String s = new String(delivery.getBody(), StandardCharsets.UTF_8);
StoredEvent event = CommonDomainRegistry.getCustomObjectSerializer().deserialize(s, StoredEvent.class);
log.debug("handling {} with id {}", ClassUtility.getShortName(event.getName()), event.getId());
try {
consumer.accept(event);
} catch (Exception ex) {
log.error("error during consume, catch error to maintain connection", ex);
}
log.trace("mq message consumed");
};
try {
Channel channel = connectionSub.createChannel();
channel.queueDeclare(queueName, true, false, false, null);
checkExchange(channel);
for (String topic : topics) {
channel.queueBind(queueName, EXCHANGE_NAME, routingKeyWithoutTopic + topic);
}
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
});
} catch (IOException e) {
log.error("unable create queue for {} with routing key {} and queue name {}", subscribedApplicationName, routingKeyWithoutTopic, queueName, e);
}
}
use of com.rabbitmq.client.DeliverCallback in project Iot-now-start by Dongboy111.
the class Work01 method main.
public static void main(String[] args) throws IOException, TimeoutException {
Channel channel = RabbitMqUtils.getChannel();
// 声明接受消息
DeliverCallback deliverCallback = (consumerTag, message) -> {
System.out.println("接受到的消息:" + new String(message.getBody()));
};
// 取消消息消费
CancelCallback cancelCallback = cousumerTag -> {
System.out.println("消费被中断");
};
System.out.println("C2等到接收消息,,,,");
channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
// 不关闭的话一直等待
}
Aggregations