use of com.rabbitmq.client.DeliverCallback in project force-oneself by Force-oneself.
the class RPCServer method main.
public static void main(String[] argv) throws Exception {
Channel channel = RabbitMQUtils.getChannel();
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
channel.queuePurge(RPC_QUEUE_NAME);
channel.basicQos(1);
System.out.println(" [x] Awaiting RPC requests");
Object monitor = new Object();
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder().correlationId(delivery.getProperties().getCorrelationId()).build();
String response = "";
try {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
int n = Integer.parseInt(message);
System.out.println(" [.] fib(" + message + ")");
response += fib(n);
} catch (RuntimeException e) {
System.out.println(" [.] " + e.toString());
} finally {
channel.basicPublish("", delivery.getProperties().getReplyTo(), replyProps, response.getBytes(StandardCharsets.UTF_8));
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
// RabbitMq consumer worker thread notifies the RPC server owner thread
synchronized (monitor) {
monitor.notify();
}
}
};
channel.basicConsume(RPC_QUEUE_NAME, false, deliverCallback, (consumerTag -> {
}));
// Wait and be prepared to consume the message from RPC client.
while (true) {
synchronized (monitor) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
use of com.rabbitmq.client.DeliverCallback in project force-oneself by Force-oneself.
the class ReceiveLogsTopics method main.
public static void main(String[] argv) throws Exception {
Channel channel = RabbitMQUtils.getChannel();
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(), StandardCharsets.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 amos2022ss02-audit-chain by amosproj.
the class ConsumerClientBlockchain method start.
/**
* Start receiving Messages from the RabbitMQ Server.
* @throws IOException if an I/O error occurs
* @throws TimeoutException if the timeout expires
*/
public void start() throws IOException, TimeoutException {
System.out.println("Starting to receive Messages.");
Connection connection = this.factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages.");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
AggregateMessage message;
try {
message = (AggregateMessage) AggregateConsumerClient.deserializeMessage(delivery.getBody());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Vector<Message> messages = message.getMessages();
Integer[] seq_numbers = new Integer[messages.size()];
String[] transactions = new String[messages.size()];
int iterator = 0;
for (Message m : messages) {
// if you use instanceOf you could accept both Messages and HmacMessages
m = (Hmac_Message) m;
seq_numbers[iterator] = m.getSequence_number();
transactions[iterator] = m.getMessage();
iterator++;
}
blockchain.addABlock(seq_numbers, transactions);
// blockchain.printBlockchain();
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project amos2022ss02-audit-chain by amosproj.
the class ConsumerClient method start.
/**
* Start receiving Messages from the RabbitMQ Server.
* @throws IOException if an I/O error occurs
* @throws TimeoutException if the timeout expires
*/
public void start() throws IOException, TimeoutException {
System.out.println("Starting to receive Messages.");
Connection connection = this.factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages.");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
use of com.rabbitmq.client.DeliverCallback in project jeesuite-libs by vakinge.
the class RabbitmqConsumerAdapter method start.
@Override
public void start() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("");
factory.setPort(101);
factory.setUsername("");
factory.setPassword("");
Connection connect = factory.newConnection();
Channel channel = connect.createChannel();
// 声明exchange
DeclareOk declareOk = channel.exchangeDeclare("my.fanout3", "fanout", true);
channel.exchangeDeclare("test", "fanout");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, "test", "");
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 -> {
});
}
Aggregations