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:org.opendaylight.federationmessagequeue.impl.RabbitMessageBus.java

License:Open Source License

private void createQueueIfNeeded(String queueName, MessageBusConnectionData messageBusConnectionData,
        Channel channel) {
    try {/*  www  .  ja v a  2 s .co  m*/
        channel.queueDeclare(queueName, false, false, false, null);
    } catch (IOException e) {
        LOG.warn("Failed to declare queue {} on broker {}", queueName, messageBusConnectionData.brokerIp, e);
    }
}

From source file:org.pushtrigger.mule.agent.CreateQueueAgent.java

License:Apache License

@Override
public void start() throws MuleException {
    System.out.println("Create Queue Agent is running...");

    Connection connection = null;
    Channel channel = null;
    try {/*from  w  w  w  .j  a  va2  s  .  c  om*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World! I am creating a queue...";

        Map<String, Object> props = new HashMap<String, Object>();
        props.put("path", "queue");
        props.put("branch", "create");

        AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
        AMQP.BasicProperties basicProps = bob.headers(props).build();

        channel.basicPublish("", QUEUE_NAME, basicProps, message.getBytes());
        System.out.println("Agent has created the queue...");
    } catch (IOException e) {
        System.out.println("Something wrong " + e);
    } finally {
        try {
            if (channel != null)
                channel.close();
        } catch (IOException e) {
        }
        try {
            if (connection != null)
                connection.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.sdw.scheduler.PeriodicUpdater.java

License:Apache License

/**
 * Pushes messages to the shared queue on execution of triggers
 * @param JobExecutionContext Job execution context
 * @throws JobExecutionException //from   w  ww.jav  a 2s . c om
 */
@Override
public void execute(JobExecutionContext jobExecutionContext) {
    try {
        Connection connection = PeriodicScheduler.factory.newConnection();
        Channel channel = connection.createChannel();
        String queueName = "work-queue-1";
        Map<String, Object> params = new HashMap<>();
        params.put("x-ha-policy", "all");
        channel.queueDeclare(queueName, true, false, false, params);
        String mesg = "Sent at: " + System.currentTimeMillis();
        byte[] body = mesg.getBytes("UTF-8");
        channel.basicPublish("", queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, body);
        LOG.info("Message sent: " + mesg);
        connection.close();
    } catch (Exception ex) {
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:org.sdw.scheduler.QueueProcessor.java

License:Apache License

/**
 * Implementation of the Runnable interface's run method
 * Polls for messages in the shared queue and logs the results on arrival of message
 *///ww w.  ja  v  a 2s  .  c  om
@Override
public void run() {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri(System.getenv("CLOUDAMQP_URL"));
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String queueName = "work-queue-1";
        Map<String, Object> params = new HashMap<>();
        params.put("x-ha-policy", "all");
        channel.queueDeclare(queueName, true, false, false, params);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, false, consumer);

        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            if (delivery != null) {
                String msg = new String(delivery.getBody(), "UTF-8");
                LOG.info("Message Received: " + msg);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    } catch (IOException | KeyManagementException | NoSuchAlgorithmException | URISyntaxException
            | ShutdownSignalException | ConsumerCancelledException | InterruptedException ex) {
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:org.smartdeveloperhub.harvesters.it.notification.CollectorControllerTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//from   w w  w  .  j  a  v  a 2s. c o m
public void testConnect$shouldCleanUpOnQueueDeclarationFailure(@Mocked final Channel channel) throws Exception {
    new MockUp<ConnectionManager>() {
        private boolean connected = false;

        @Mock(invocations = 1)
        void connect() {
            this.connected = true;
        }

        @Mock(invocations = 1)
        void disconnect() {
        }

        @Mock
        boolean isConnected() {
            return this.connected;
        }

        @Mock
        Channel channel() {
            return channel;
        }
    };
    final CollectorConfiguration defaultCollector = defaultCollector();
    new Expectations() {
        {
            channel.queueDeclare((String) this.any, true, true, true, (Map<String, Object>) this.any);
            this.result = new IOException("Failure");
        }
    };
    final CollectorController sut = collectorController(defaultCollector);
    try {
        sut.connect();
        fail("Should not connect on failure");
    } catch (final ControllerException e) {
        assertThat(e.getMessage(), equalTo("Could not create queue named 'name'"));
        assertThat(e.getCause(), instanceOf(IOException.class));
        assertThat(e.getCause().getMessage(), equalTo("Failure"));
    }
}

From source file:org.smartdeveloperhub.harvesters.it.notification.CollectorControllerTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*from   ww  w .j ava  2s.com*/
public void testConnect$shouldCleanUpOnQueueBindingFailure(@Mocked final Channel channel,
        @Mocked final Queue.DeclareOk ok) throws Exception {
    new MockUp<ConnectionManager>() {
        private boolean connected = false;

        @Mock(invocations = 1)
        void connect() {
            this.connected = true;
        }

        @Mock(invocations = 1)
        void disconnect() {
        }

        @Mock
        boolean isConnected() {
            return this.connected;
        }

        @Mock
        Channel channel() {
            return channel;
        }
    };
    final CollectorConfiguration defaultCollector = defaultCollector();
    new Expectations() {
        {
            channel.queueDeclare((String) this.any, true, true, true, (Map<String, Object>) this.any);
            this.result = ok;
            ok.getQueue();
            this.result = "actualQueueName";
            channel.queueBind("actualQueueName", defaultCollector.getExchangeName(),
                    Notifications.ROUTING_KEY_PATTERN);
            this.result = new IOException("Failure");
        }
    };
    final CollectorController sut = collectorController(defaultCollector);
    try {
        sut.connect();
        fail("Should not connect on failure");
    } catch (final ControllerException e) {
        assertThat(e.getMessage(),
                equalTo("Could not bind queue 'actualQueueName' to exchange '"
                        + defaultCollector.getExchangeName() + "' using routing key '"
                        + Notifications.ROUTING_KEY_PATTERN + "'"));
        assertThat(e.getCause(), instanceOf(IOException.class));
        assertThat(e.getCause().getMessage(), equalTo("Failure"));
    }
}

From source file:org.smartdeveloperhub.harvesters.it.notification.CollectorControllerTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*from  www.ja  v a 2  s .c  o m*/
public void testConnect$shouldCleanUpOnConsumerRegistrationFailure(@Mocked final Channel channel,
        @Mocked final Queue.DeclareOk ok) throws Exception {
    new MockUp<ConnectionManager>() {
        private boolean connected = false;

        @Mock(invocations = 1)
        void connect() {
            this.connected = true;
        }

        @Mock(invocations = 1)
        void disconnect() {
        }

        @Mock
        boolean isConnected() {
            return this.connected;
        }

        @Mock
        Channel channel() {
            return channel;
        }
    };
    final CollectorConfiguration defaultCollector = defaultCollector();
    new Expectations() {
        {
            channel.queueDeclare((String) this.any, true, true, true, (Map<String, Object>) this.any);
            this.result = ok;
            ok.getQueue();
            this.result = "actualQueueName";
            channel.basicConsume("actualQueueName", false, (Consumer) this.any);
            this.result = new IOException("Failure");
        }
    };
    final CollectorController sut = collectorController(defaultCollector);
    try {
        sut.connect();
        fail("Should not connect on failure");
    } catch (final ControllerException e) {
        assertThat(e.getMessage(), equalTo("Could not register consumer for queue 'actualQueueName'"));
        assertThat(e.getCause(), instanceOf(IOException.class));
        assertThat(e.getCause().getMessage(), equalTo("Failure"));
    }
}

From source file:org.smartdeveloperhub.harvesters.it.notification.CollectorControllerTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//w w  w.ja v  a2  s .com
public void testDisconnect$ignoreCleanerFailures(@Mocked final Channel channel,
        @Mocked final Queue.DeclareOk ok) throws Exception {
    new MockUp<ConnectionManager>() {
        private boolean connected = false;

        @Mock(invocations = 1)
        void connect() {
            this.connected = true;
        }

        @Mock(invocations = 1)
        void disconnect() {
        }

        @Mock
        boolean isConnected() {
            return this.connected;
        }

        @Mock
        Channel channel() {
            return channel;
        }
    };
    final CollectorConfiguration defaultCollector = defaultCollector();
    final CollectorController sut = collectorController(defaultCollector);
    new Expectations() {
        {
            channel.queueDeclare((String) this.any, true, true, true, (Map<String, Object>) this.any);
            this.result = ok;
            ok.getQueue();
            this.result = "actualQueueName";
            channel.basicConsume("actualQueueName", false, (Consumer) this.any);
            channel.queueUnbind("actualQueueName", defaultCollector.getExchangeName(), (String) this.any);
            this.result = new IOException("failure");
        }
    };
    sut.connect();
    sut.disconnect();
}

From source file:org.smartdeveloperhub.harvesters.scm.backend.notification.CollectorControllerTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*from  w ww  .j a  v  a 2s  .co m*/
public void testConnect$shouldCleanUpOnQueueDeclarationFailure(@Mocked final Channel channel) throws Exception {
    new MockUp<ConnectionManager>() {
        private boolean connected = false;

        @Mock(invocations = 1)
        void connect() {
            this.connected = true;
        }

        @Mock(invocations = 1)
        void disconnect() {
        }

        @Mock
        boolean isConnected() {
            return this.connected;
        }

        @Mock
        Channel channel() {
            return channel;
        }
    };
    final Collector defaultCollector = defaultCollector();
    new Expectations() {
        {
            channel.queueDeclare((String) this.any, true, true, true, (Map<String, Object>) this.any);
            this.result = new IOException("Failure");
        }
    };
    final CollectorController sut = collectorController(defaultCollector);
    try {
        sut.connect();
        fail("Should not connect on failure");
    } catch (final ControllerException e) {
        assertThat(e.getMessage(), equalTo("Could not create queue named 'name'"));
        assertThat(e.getCause(), instanceOf(IOException.class));
        assertThat(e.getCause().getMessage(), equalTo("Failure"));
    }
}

From source file:org.smartdeveloperhub.harvesters.scm.backend.notification.CollectorControllerTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*from  www .ja  v a2s.  c  o  m*/
public void testConnect$shouldCleanUpOnQueueBindingFailure(@Mocked final Channel channel,
        @Mocked final Queue.DeclareOk ok) throws Exception {
    new MockUp<ConnectionManager>() {
        private boolean connected = false;

        @Mock(invocations = 1)
        void connect() {
            this.connected = true;
        }

        @Mock(invocations = 1)
        void disconnect() {
        }

        @Mock
        boolean isConnected() {
            return this.connected;
        }

        @Mock
        Channel channel() {
            return channel;
        }
    };
    final Collector defaultCollector = defaultCollector();
    new Expectations() {
        {
            channel.queueDeclare((String) this.any, true, true, true, (Map<String, Object>) this.any);
            this.result = ok;
            ok.getQueue();
            this.result = "actualQueueName";
            channel.queueBind("actualQueueName", defaultCollector.getExchangeName(),
                    Notifications.ROUTING_KEY_PATTERN);
            this.result = new IOException("Failure");
        }
    };
    final CollectorController sut = collectorController(defaultCollector);
    try {
        sut.connect();
        fail("Should not connect on failure");
    } catch (final ControllerException e) {
        assertThat(e.getMessage(),
                equalTo("Could not bind queue 'actualQueueName' to exchange '"
                        + defaultCollector.getExchangeName() + "' using routing key '"
                        + Notifications.ROUTING_KEY_PATTERN + "'"));
        assertThat(e.getCause(), instanceOf(IOException.class));
        assertThat(e.getCause().getMessage(), equalTo("Failure"));
    }
}