Search in sources :

Example 31 with DeliverCallback

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();
            }
        }
    }
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) RabbitMQUtils(com.quan.framework.amqp.util.RabbitMQUtils) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) AMQP(com.rabbitmq.client.AMQP) Channel(com.rabbitmq.client.Channel) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 32 with DeliverCallback

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 -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) RabbitMQUtils(com.quan.framework.amqp.util.RabbitMQUtils) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) Channel(com.rabbitmq.client.Channel) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 33 with DeliverCallback

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 -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) Message(ProducerDummy.Messages.Message) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) AggregateConsumerClient(ConsumerDummy.AggregateConsumerClient) Connection(com.rabbitmq.client.Connection) StandardCharsets(java.nio.charset.StandardCharsets) AggregateConsumerClient.deserializeMessage(ConsumerDummy.AggregateConsumerClient.deserializeMessage) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Vector(java.util.Vector) Gson(com.google.gson.Gson) Map(java.util.Map) AggregateMessage(ProducerDummy.Messages.AggregateMessage) AbstractClient(ProducerDummy.Client.AbstractClient) Blockchain(BlockchainImplementation.Blockchain.Blockchain) Hmac_Message(ProducerDummy.Messages.Hmac_Message) Channel(com.rabbitmq.client.Channel) Message(ProducerDummy.Messages.Message) AggregateConsumerClient.deserializeMessage(ConsumerDummy.AggregateConsumerClient.deserializeMessage) AggregateMessage(ProducerDummy.Messages.AggregateMessage) Hmac_Message(ProducerDummy.Messages.Hmac_Message) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AggregateMessage(ProducerDummy.Messages.AggregateMessage) DeliverCallback(com.rabbitmq.client.DeliverCallback) Hmac_Message(ProducerDummy.Messages.Hmac_Message) Vector(java.util.Vector)

Example 34 with DeliverCallback

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 -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) TimeoutException(java.util.concurrent.TimeoutException) AbstractClient(ProducerDummy.Client.AbstractClient) IOException(java.io.IOException) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 35 with DeliverCallback

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 -> {
    });
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) MQConsumer(com.mendmix.amqp.MQConsumer) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Aggregations

DeliverCallback (com.rabbitmq.client.DeliverCallback)59 Channel (com.rabbitmq.client.Channel)58 Connection (com.rabbitmq.client.Connection)53 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)50 StandardCharsets (java.nio.charset.StandardCharsets)21 IOException (java.io.IOException)18 TimeoutException (java.util.concurrent.TimeoutException)12 ConnectionFactoryUtils (com.hyf.testDemo.mq.ConnectionFactoryUtils)11 AMQP (com.rabbitmq.client.AMQP)9 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 Date (java.util.Date)5 RabbitMQUtils (com.quan.framework.amqp.util.RabbitMQUtils)4 CancelCallback (com.rabbitmq.client.CancelCallback)4 AbstractClient (ProducerDummy.Client.AbstractClient)3 AggregateMessage (ProducerDummy.Messages.AggregateMessage)2 Message (ProducerDummy.Messages.Message)2 ThreadUtil (cn.hutool.core.thread.ThreadUtil)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DeclareOk (com.rabbitmq.client.AMQP.Exchange.DeclareOk)2