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:net.lshift.accent.ConnectionSmokeTest.java

License:Apache License

@Test
public void shouldConsumeEvenIfConnectionIsntCreatedUntilLater() throws IOException, InterruptedException {
    AccentConnection directConn = new AccentConnection(directFactory(),
            AccentTestUtils.printingFailureLogger());
    AccentConnection managedConn = new AccentConnection(controlledFactory(5673),
            AccentTestUtils.smallPrintingFailureLogger());
    try {/*w  w w .  j  av  a2  s .c  o m*/
        AccentChannel directCh = directConn.createChannel();
        AccentChannel managedCh = managedConn.createChannel();

        // Add a setup listener to create the queue
        directCh.addChannelSetupListener(new ChannelListenerAdapter() {
            @Override
            public void channelCreated(Channel c) throws IOException {
                c.queueDeclare("accent.testq", false, false, true, null);
            }
        });

        // Consumer on the managed channel which we'll allow to work later
        QueueingConsumer consumer = new QueueingConsumer(null);
        AccentConsumer aConsumer = new AccentConsumer(managedCh, "accent.testq", consumer);

        // Publish the message on a direct channel
        AccentConfirmPublisher publisher = new AccentConfirmPublisher(directCh);
        publisher.reliablePublish("", "accent.testq", new AMQP.BasicProperties(), new byte[0]);

        // Try to consume. We shouldn't get anything because we haven't allowed the connection through
        QueueingConsumer.Delivery d = consumer.nextDelivery(2000);
        assertNull(d);

        // Start the tracer to give us a valid connection
        ControlledConnectionProxy proxy = createProxy(5673);
        try {
            QueueingConsumer.Delivery d2 = consumer.nextDelivery(2000);
            assertNotNull(d2);
            assertEquals(0, d2.getBody().length);

            aConsumer.reliableAck(d2.getEnvelope().getDeliveryTag(), false);

            closeQuietly(aConsumer);
        } finally {
            closeQuietly(proxy);
        }
    } finally {
        closeQuietly(managedConn);
        closeQuietly(directConn);
    }
}

From source file:net.lshift.accent.ConnectionSmokeTest.java

License:Apache License

@Test
public void shouldRecoverConsumeIfConnectionIsLost() throws IOException, InterruptedException {
    AccentConnection conn = new AccentConnection(controlledFactory(5675),
            AccentTestUtils.printingFailureLogger());
    ControlledConnectionProxy proxy = createProxy(5675);
    try {/*from   ww  w  .ja  v a 2  s  .c o m*/
        AccentChannel ch = conn.createChannel();

        // Add a setup listener to create the queue
        ch.addChannelSetupListener(new ChannelListenerAdapter() {
            @Override
            public void channelCreated(Channel c) throws IOException {
                c.queueDeclare("accent.testq", false, false, true, null);
            }
        });

        QueueingConsumer consumer = new QueueingConsumer(null);
        AccentConsumer aConsumer = new AccentConsumer(ch, "accent.testq", consumer);

        // Ensure that our consumer works
        AccentConfirmPublisher publisher = new AccentConfirmPublisher(ch);
        publisher.reliablePublish("", "accent.testq", new AMQP.BasicProperties(), new byte[0]);
        QueueingConsumer.Delivery d = consumer.nextDelivery(10000);
        assertNotNull(d);
        aConsumer.reliableAck(d.getEnvelope().getDeliveryTag(), false);

        // Terminate the connection and recreate it
        proxy.close();
        proxy.start();

        // Send another message through, and ensure that we see it
        publisher.reliablePublish("", "accent.testq", new AMQP.BasicProperties(), new byte[0]);
        QueueingConsumer.Delivery d2 = consumer.nextDelivery(10000);
        assertNotNull(d);
        aConsumer.reliableAck(d2.getEnvelope().getDeliveryTag(), false);

        closeQuietly(aConsumer);
    } finally {
        closeQuietly(proxy);
        closeQuietly(conn);
    }
}

From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignaler.java

License:Apache License

private void ensureQueue(final Channel channel, final String queueName) throws IOException {
    if (!queuesCreated.getOrDefault(queueName, false)) {
        channel.queueDeclare(queueName, true, false, false, null);
        queuesCreated.put(queueName, true);
    }//w w w .j  a  v  a2 s. c om
}

From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java

License:Apache License

@Test
public void preDeleteHappyCase() throws Exception {

    Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg",
            "a", amq_default_address, primaryTableNameString, "true", "");
    setupHBase(kvs);/*from  www .  j  a  v  a2 s . c  o m*/
    com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
    factory.setUri(amq_default_address);
    com.rabbitmq.client.Connection conn = factory.newConnection();
    com.rabbitmq.client.Channel channel = conn.createChannel();

    //simulate population of secondary index for a put on the downstreamTable
    Put idxPut = new Put("EFG1".getBytes());
    idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes());
    secondaryIdxTable.put(idxPut);

    //simulate a data rippling performed by the data_rippler service consuming off of the rabbitmq queue
    Put tablePut = new Put("EFB1".getBytes());
    tablePut.addColumn("eg".getBytes(), "some_key".getBytes(), "some_value".getBytes());
    downstreamTable.put(tablePut);

    // since we made no active put to the queue from the prePut, we need to declare it explicitly here
    channel.queueDeclare(primaryTableNameString, true, false, false, null);

    // finished with the setup, we now issue a delete which should be caught by the rabbitmq
    Delete d = new Delete("EFG1".getBytes());
    primaryTable.delete(d);

    //check that values made it to the queue
    while (true) {
        GetResponse response = channel.basicGet(primaryTableNameString, false);
        if (response == null)//busy-wait until the message has made it through the MQ
        {
            continue;
        }
        String routingKey = response.getEnvelope().getRoutingKey();
        Assert.assertEquals("Routing key should be rowkey", "genome", routingKey);

        String contentType = response.getProps().getContentType();
        Assert.assertEquals("Content type should be preserved", "application/json", contentType);

        Map<String, Object> headers = response.getProps().getHeaders();
        Assert.assertEquals("An action should be set on the message", "delete",
                headers.get("action").toString());

        byte[] body = response.getBody();
        JSONObject jo = new JSONObject(new String(body));

        String column_qualifier = (String) jo.get("column_qualifier");
        Assert.assertEquals("Column qualifier should be empty, signalling a row delete", "", column_qualifier);

        long deliveryTag = response.getEnvelope().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
        break;
    }
}

From source file:net.orzo.queue.AmqpConnection.java

License:Apache License

@Override
public Channel createChannel() throws IOException {
    Channel ch = this.connection.createChannel();
    ch.queueDeclare(this.conf.queue, true, false, false, null);
    return ch;//from w w  w .  j a  v a  2  s. c om
}

From source file:net.orzo.queue.AmqpResponse.java

License:Apache License

@Override
public void response(String message) {
    try {/*ww  w . j  a v  a  2  s .c  o  m*/
        Channel ch = this.channelProvider.createChannel();
        ch.queueDeclare(this.conf.queue, true, false, false, null);
        ch.basicPublish("", this.conf.queue, null, message.getBytes());

    } catch (IOException e) {
        LoggerFactory.getLogger(AmqpResponse.class)
                .error(String.format("Failed to send response: %s", e.getMessage()));
    }
}

From source file:nl.uva.sne.drip.api.service.DRIPLogService.java

License:Apache License

public List<DRIPLogRecord> get() throws IOException, TimeoutException {
    Channel channel = null;

    if (factory == null) {
        this.factory = new ConnectionFactory();
        factory.setHost(messageBrokerHost);
        factory.setPort(AMQP.PROTOCOL.PORT);
    }//from w  ww .  ja va 2s  .c  om
    if (this.mapper == null) {
        this.mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    }

    try (Connection connection = factory.newConnection()) {
        channel = connection.createChannel();

        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String owner = user.getUsername();
        String qeueNameUser = qeueName + "_" + owner;
        channel.queueDeclare(qeueNameUser, true, false, false, null);

        GetResponse response = channel.basicGet(qeueNameUser, true);
        List<DRIPLogRecord> logs = new ArrayList<>();
        while (response != null) {
            String message = new String(response.getBody(), "UTF-8");
            response = channel.basicGet(qeueNameUser, true);
            logs.add(mapper.readValue(message, DRIPLogRecord.class));
        }
        return logs;

    }
}

From source file:nl.uva.sne.drip.commons.utils.DRIPLogHandler.java

License:Apache License

@Override
public void publish(LogRecord record) {

    try (Connection connection = factory.newConnection()) {
        Channel channel = connection.createChannel();
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        if (auth != null) {

            User user = (User) auth.getPrincipal();
            setOwner(user.getUsername());
        }/*from  w w  w . j  ava  2s  .  co m*/

        DRIPLogRecord dripLog = DRIPLogRecordFactory.create(record);
        dripLog.setOwner(getOwner());
        String jsonInString = mapper.writeValueAsString(dripLog);

        //            channel.basicPublish(qeueName, owner, null, jsonInString.getBytes());
        //            channel.basicPublish(qeueName, owner, MessageProperties.PERSISTENT_TEXT_PLAIN, jsonInString.getBytes("UTF-8"));
        String qeueNameUser = qeueName + "_" + getOwner();
        channel.queueDeclare(qeueNameUser, true, false, false, null);

        channel.basicPublish("", qeueNameUser, MessageProperties.PERSISTENT_TEXT_PLAIN,
                jsonInString.getBytes("UTF-8"));

    } catch (JsonProcessingException ex) {
        Logger.getLogger(DRIPLogHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | TimeoutException ex) {
        Logger.getLogger(DRIPLogHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:nl.uva.sne.drip.drip.component_example.RPCServer.java

License:Apache License

private static void start() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);/*from  ww w.j av a2  s  . c  o  m*/
    factory.setPassword("guest");
    factory.setUsername("guest");
    factory.setPort(AMQP.PROTOCOL.PORT);
    try (Connection connection = factory.newConnection()) {
        Channel channel = connection.createChannel();
        //We define the queue name 
        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
        //Set our own customized consummer 
        Consumer c = new Consumer(channel);
        //Start listening for messages 
        channel.basicConsume(RPC_QUEUE_NAME, false, c);

        //Block so we don't close the channel
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException _ignore) {
            }
        }

    } catch (IOException | TimeoutException ex) {
        Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:nl.uva.sne.drip.drip.provisioner.RPCServer.java

License:Apache License

private static void start() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(PropertyValues.HOST);
    factory.setPassword("guest");
    factory.setUsername("guest");
    factory.setPort(AMQP.PROTOCOL.PORT);
    Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, "Connected to: {0}", PropertyValues.HOST);
    try (Connection connection = factory.newConnection()) {
        Channel channel = connection.createChannel();
        //We define the queue name 
        channel.queueDeclare(PropertyValues.RPC_QUEUE_NAME, false, false, false, null);
        DefaultConsumer c;//  w  w  w.j  a v  a  2 s . c om
        if (PropertyValues.RPC_QUEUE_NAME.endsWith("v0")) {
            c = new nl.uva.sne.drip.drip.provisioner.v0.Consumer(channel);
        } else {
            c = new nl.uva.sne.drip.drip.provisioner.v1.Consumer(channel, PropertyValues.HOST);
        }

        //Start listening for messages 
        channel.basicConsume(PropertyValues.RPC_QUEUE_NAME, false, c);

        //Block so we don't close the channel
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException _ignore) {
            }
        }

    } catch (IOException | TimeoutException ex) {
        Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
    }

}