Example usage for com.rabbitmq.client Channel queueDeclare

List of usage examples for com.rabbitmq.client Channel queueDeclare

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel queueDeclare.

Prototype

Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
        Map<String, Object> arguments) throws IOException;

Source Link

Document

Declare a queue

Usage

From source file:it.jugtorino.one.msvc.way.rabbit.reference.webapp.GeoIPResource.java

License:Open Source License

public GeoIPResource() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    Connection connection = factory.newConnection(Collections.singletonList(new Address("localhost")));
    Channel channel = connection.createChannel();
    channel.queueDeclare("geoip_service_v1", false, false, false, null);

    client = new RpcClient(channel, "geoip_service_v1");
    client.startConsuming();/*from w w w  . jav  a 2s  .  c  om*/
}

From source file:it.txt.ens.authorisationService.amqp.AMQPQueueHelper.java

License:Apache License

public static void creates(Channel channel, String queue, boolean durable, boolean exclusive,
        boolean autoDelete) throws IOException {
    channel.queueDeclare(queue, durable, exclusive, autoDelete, null);
}

From source file:itinno.example.ExampleSocialMediaStormDeclarator.java

/**
 * Main RabbitMQ declaration method. Will use RabbitMQ channel reference.
 * /*from  w ww  .ja  v  a2  s  .  c o  m*/
 * Rabbit MQ Channel API: https://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1.5/rabbitmq-java-client-javadoc-3.1.5/ (search for "Channel")
 * 
 * @param channel  rabbitmq channel
 */
@Override
public void execute(Channel channel) {
    try {
        // Storm any possible arguments that could be passed 
        Map<String, Object> args = new HashMap<>();

        /* Declare the queue 
         * 
         * API: http://www.rabbitmq.com/releases/rabbitmq-java-client/v1.7.0/rabbitmq-java-client-javadoc-1.7.0/com/rabbitmq/client/Channel.html#queueDeclare(java.lang.String, boolean, boolean, boolean, boolean, java.util.Map))
          * 
          */
        channel.queueDeclare(this.strQueueName, true, false, false, args);

        /* Declare the exchange
         * 
         * API: http://www.rabbitmq.com/releases/rabbitmq-java-client/v1.7.0/rabbitmq-java-client-javadoc-1.7.0/com/rabbitmq/client/Channel.html#exchangeDeclare(java.lang.String, java.lang.String, boolean)
         */
        channel.exchangeDeclare(this.strExchange, this.strExchangeType, true);

        /*
         * Bind the queue
         * 
         * API: http://www.rabbitmq.com/releases/rabbitmq-java-client/v1.7.0/rabbitmq-java-client-javadoc-1.7.0/com/rabbitmq/client/Channel.html#queueBind(java.lang.String, java.lang.String, java.lang.String)
         */
        channel.queueBind(this.strQueueName, this.strExchange, this.strRoutingKey);

        // Handle Exception and allow to continue
    } catch (Exception e) {
        System.err.println("Failed to execute RabbitMQ declarations. Details: " + e.getMessage());
        e.printStackTrace();
    }
}

From source file:javarpc_server.JavaRPC_Server.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/*from  ww  w . j  a va 2  s.com*/
 * @throws java.lang.InterruptedException
 */
public static void main(String[] args) throws IOException, InterruptedException {
    // TODO code application logic here

    ConnectionFactory factory = new ConnectionFactory();
    System.out.println(factory.getUsername() + " " + factory.getPassword());
    factory.setHost("localhost");

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(RPC_QUEUE_NAME, false, consumer);

    System.out.println(" [x] Awaiting RPC requests");

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();

        BasicProperties props = delivery.getProperties();
        BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                .build();

        String message = new String(delivery.getBody());

        System.out.println(" [.] convert(" + message + ")");
        String response = "" + convert(message);

        channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:jenkins.plugins.logstash.persistence.RabbitMqDao.java

License:Open Source License

@Override
public void push(String data) throws IOException {
    Connection connection = null;
    Channel channel = null;
    try {/* w ww. j a  v a  2 s.  c o  m*/
        connection = pool.newConnection();
        channel = connection.createChannel();

        // Ensure the queue exists
        try {
            channel.queueDeclarePassive(key);
        } catch (IOException e) {
            // The queue does not exist and the channel has been closed
            finalizeChannel(channel);

            // Create the queue
            channel = connection.createChannel();
            channel.queueDeclare(key, true, false, false, null);
        }

        channel.basicPublish("", key, null, data.getBytes());
    } finally {
        finalizeChannel(channel);
        finalizeConnection(connection);
    }
}

From source file:joram.amqp.PersistenceKillTest.java

License:Open Source License

public void killingTest() throws Exception {
    senderConnection = new LiveServerConnection("sender", "localhost", 5672, null, null);
    receiverConnection = new LiveServerConnection("receiver", "localhost", 5672, null, null);

    senderConnection.startLiveConnection();
    receiverConnection.startLiveConnection();

    Channel senderChannel = senderConnection.getConnection().createChannel();
    senderChannel.txSelect();/*from   w w  w.  ja v a 2  s  .co  m*/

    DeclareOk declareOk = senderChannel.queueDeclare("testQueue", true, false, false, null);

    new Thread(new Runnable() {

        int received;
        long totalReceived;

        Channel consumerChannel;
        QueueingConsumer consumer;

        // Consumer thread
        public void run() {

            try {
                consumerChannel = receiverConnection.getConnection().createChannel();
                consumerChannel.txSelect();
                consumer = new QueueingConsumer(consumerChannel);
                consumerChannel.basicConsume("testQueue", false, consumer);
            } catch (Exception exc) {
                exc.printStackTrace();
            }

            while (true) {
                QueueingConsumer.Delivery delivery;
                try {
                    delivery = consumer.nextDelivery();
                    long receivedNb = Long.parseLong(new String(delivery.getBody()));
                    consumer.getChannel().basicAck(delivery.getEnvelope().getDeliveryTag(), false);

                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException exc1) {
                    }

                    if (receivedNb < totalReceived) {
                        System.out.println("Duplicate received: " + receivedNb);
                        continue;
                    }

                    // We can receive duplicates but can't miss one message
                    // One duplicate if the channel is transacted, multiple if it is not
                    assertEquals(totalReceived, receivedNb);

                    totalReceived++;
                    received++;

                    consumerChannel.txCommit();

                    if (received == nbMsgRound) {
                        received = 0;
                        synchronized (lock) {
                            lock.notify();
                        }
                    }

                    if (totalReceived == nbMsgRound * nbRounds) {
                        consumer.getChannel().close();
                        return;
                    }

                } catch (Exception ie) {
                    if (totalReceived == nbRounds * nbMsgRound) {
                        return;
                    }
                    System.out.println("Consumer connection broken. Reconnect.");
                    while (!receiverConnection.isConnectionOpen()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException exc) {
                            exc.printStackTrace();
                        }
                    }
                    try {
                        consumerChannel = receiverConnection.getConnection().createChannel();
                        consumerChannel.txSelect();
                        consumer = new QueueingConsumer(consumerChannel);
                        consumerChannel.basicConsume("testQueue", false, consumer);
                    } catch (IOException exc) {
                        exc.printStackTrace();
                    }
                    System.out.println("Consumer Reconnected --- totalReceived = " + totalReceived);
                    continue;
                }
            }
        }
    }).start();

    long start = System.nanoTime();

    // Killer thread
    new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(5000);
                System.out.println("Kill server");
                killAgentServer((short) 0);

                Thread.sleep(5000);
                startAgentServer((short) 0);
                System.out.println("server restarted.");
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    }).start();

    // Sender
    for (int i = 0; i < nbRounds; i++) {
        if (i % 20 == 0) {
            long delta = System.nanoTime() - start;
            System.out.println("Round " + i + " " + ((i * nbMsgRound * 1000000000L) / delta) + " msg/s");
        }
        try {
            for (int j = 0; j < nbMsgRound; j++) {
                senderChannel.basicPublish("", declareOk.getQueue(), MessageProperties.PERSISTENT_BASIC,
                        new Long(i * nbMsgRound + j).toString().getBytes());
            }

            synchronized (lock) {
                senderChannel.txCommit();
                lock.wait();
            }
        } catch (Exception exc) {
            i--;
            System.out.println("Sender connection broken. Reconnect.");
            while (!senderConnection.isConnectionOpen()) {
                Thread.sleep(100);
            }
            senderChannel = senderConnection.getConnection().createChannel();
            senderChannel.txSelect();
            System.out.println("Sender Reconnected");
            Thread.sleep(1000);
            System.out.println("Restart Sender");
        }
    }

    long delta = System.nanoTime() - start;
    System.out.println(delta / 1000000L + " ms");
    System.out.println(((nbRounds * nbMsgRound * 1000000000L) / delta) + " msg/s");

    senderChannel.queueDelete(declareOk.getQueue());

    senderChannel.close();

    senderConnection.stopLiveConnection();
    receiverConnection.stopLiveConnection();

}

From source file:joram.amqp.PersistenceSimpleTest.java

License:Open Source License

public void recover1() throws Exception {
    ConnectionFactory cnxFactory = new ConnectionFactory();
    Connection connection = cnxFactory.newConnection();

    Channel channel = connection.createChannel();
    DeclareOk declareOk = channel.queueDeclare("testqueue", true, false, false, null);

    channel.txSelect();/*w w w .  j a v a2 s.  c  o m*/
    for (int i = 0; i < 5; i++) {
        channel.basicPublish("", declareOk.getQueue(), MessageProperties.PERSISTENT_BASIC,
                "this is a test message !!!".getBytes());
    }
    channel.txCommit();

    killAgentServer((short) 0);

    startAgentServer((short) 0);

    connection = cnxFactory.newConnection();
    channel = connection.createChannel();
    declareOk = channel.queueDeclarePassive("testqueue");

    for (int i = 0; i < 5; i++) {
        GetResponse msg = channel.basicGet("testqueue", true);
        assertNotNull(msg);
        assertEquals("this is a test message !!!", new String(msg.getBody()));
    }

    GetResponse msg = channel.basicGet("testqueue", true);
    assertNull(msg);

    channel.queueDelete(declareOk.getQueue());
}

From source file:joram.amqp.PersistenceSimpleTest.java

License:Open Source License

public void recover2() throws Exception {
    ConnectionFactory cnxFactory = new ConnectionFactory();
    Connection connection = cnxFactory.newConnection();

    Channel channel = connection.createChannel();
    DeclareOk declareOk = channel.queueDeclare("testqueue", true, false, false, null);

    channel.txSelect();/*  www .j  a v  a 2s  . c o m*/
    for (int i = 0; i < 5; i++) {
        channel.basicPublish("", declareOk.getQueue(), MessageProperties.PERSISTENT_BASIC,
                "this is a test message !!!".getBytes());
    }
    channel.txCommit();

    killAgentServer((short) 0);

    startAgentServer((short) 0);
    Thread.sleep(500);

    connection = cnxFactory.newConnection();
    channel = connection.createChannel();
    declareOk = channel.queueDeclarePassive("testqueue");

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(declareOk.getQueue(), true, consumer);

    for (int i = 0; i < 5; i++) {
        Delivery msg = consumer.nextDelivery(1000);
        assertNotNull(msg);
        assertEquals("this is a test message !!!", new String(msg.getBody()));
    }

    Delivery msg = consumer.nextDelivery(1000);
    assertNull(msg);

    channel.queueDelete(declareOk.getQueue());

}

From source file:joram.amqp.QueueTest.java

License:Open Source License

public void queueDeclareName() throws Exception {
    ConnectionFactory cnxFactory = new ConnectionFactory();
    Connection connection = cnxFactory.newConnection();

    Channel channel = connection.createChannel();
    DeclareOk declareOk = channel.queueDeclare("testqueue", true, false, false, null);

    assertEquals("testqueue", declareOk.getQueue());
}

From source file:loanbroker.normalizer.NormalizerTeachersJsonBank.java

public static void main(String[] args) {
    try {/*from   ww  w  .ja  v  a 2 s  . c  o m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");
        factory.setPort(5672);
        factory.setUsername("student");
        factory.setPassword("cph");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        //String queueName = channel.queueDeclare().getQueue();
        //channel.queueBind(queueName, EXCHANGE_NAME, "");   
        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(RPC_QUEUE_NAME, true, consumer);
        //producer 
        Channel channelOutput = connection.createChannel();
        channelOutput.exchangeDeclare(ExchangeName.GLOBAL, "direct");
        String queueName = channelOutput.queueDeclare().getQueue();
        channelOutput.queueBind(queueName, ExchangeName.GLOBAL, "normalizerToAggregator");

        LoanResponse loanResponse;
        while (true) {
            System.out.println("Reading");
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId());

            String message = new String(delivery.getBody());

            JSONObject jsonObj = new JSONObject(message);

            loanResponse = new LoanResponse(jsonObj.getInt("ssn"), jsonObj.getDouble("interestRate"),
                    "Teachers Json Bank", delivery.getProperties().getCorrelationId());
            System.out.println("renter: " + loanResponse.getInterestRate());
            System.out.println("ssn: " + loanResponse.getSsn());
            System.out.println("bank : " + loanResponse.getBank());
            //             System.out.println(" [x] Received '" + message + "'");
            System.out.println("JSON:" + loanResponse);
            System.out.println("TOstring:" + jsonObj.toString());
            Gson g = new Gson();
            String fm = g.toJson(loanResponse);
            channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null, fm.getBytes());

        }

    } catch (IOException | TimeoutException | InterruptedException e) {
        e.printStackTrace();
    }
}