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.mule.transport.amqp.internal.client.AmqpDeclarer.java

License:Open Source License

private void declareQueueActively(Channel channel, String queueName, boolean queueDurable,
        boolean queueExclusive, boolean queueAutoDelete, Map<String, Object> arguments) throws IOException {
    channel.queueDeclare(queueName, queueDurable, queueExclusive, queueAutoDelete, arguments);
    logger.info("Declared queue: " + queueName + ", durable: " + queueDurable + ", exclusive: " + queueExclusive
            + ", autoDelete: " + queueAutoDelete + ", arguments: " + arguments);
}

From source file:org.muzza.rabbit.RabbitConfig.java

public static Channel getChannel(Connection connection) throws IOException {
    Channel channel = connection.createChannel();

    channel.queueDeclare(Constants.QUEUE, false, false, false, null);

    return channel;
}

From source file:org.ninjav.rabbitmq.SendTest.java

@Test
public void canSendMessageToQueue() throws IOException, TimeoutException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    for (int i = 0; i < 1000000; i++) {
        String message = "Hello world " + i;
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    }/*from   w w  w  .j  a v  a 2  s  .  c om*/

    channel.close();
    connection.close();
}

From source file:org.ninjav.rabbitmq.TestReceive.java

@Test
public void canReceiveMessageFromQueue() throws IOException, TimeoutException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    Consumer consumer;/*from   w  w w  .j a  v  a 2 s  . co  m*/
    consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            try {
                String message = new String(body, "UTF-8");
                System.out.println("Message Received: " + message);
            } finally {
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        }
    };
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    try {
        Thread.sleep(100000);
    } catch (InterruptedException ex) {
        Logger.getLogger(TestReceive.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.objectweb.joram.mom.dest.amqp.AmqpAcquisition.java

License:Open Source License

/**
 * Create a new AMQP consumer for each connection available.
 *//*from w ww . j a v a2  s. c  o  m*/
public void updateConnections(List<LiveServerConnection> connections) {
    for (LiveServerConnection connection : connections) {
        if (!channels.containsKey(connection.getName())) {
            if (connectionNames == null || connectionNames.contains(connection.getName())) {
                if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG, "Creating a new consumer on queue " + amqpQueue
                            + " for connection " + connection.getName());
                }
                try {
                    Channel channel = connection.getConnection().createChannel();
                    if (amqpQueuePassive) {
                        channel.queueDeclarePassive(amqpQueue);
                    } else {
                        channel.queueDeclare(amqpQueue, amqpQueueDurable, amqpQueueExclusive,
                                amqpQueueAutoDelete, null);
                    }
                    AmqpConsumer consumer = new AmqpConsumer(channel, connection.getName());
                    channel.basicConsume(amqpQueue, false, consumer);
                    channels.put(connection.getName(), channel);
                } catch (Exception e) {
                    logger.log(BasicLevel.ERROR,
                            "Error while starting consumer on connection: " + connection.getName(), e);
                }
            }
        }
    }
}

From source file:org.objectweb.joram.mom.dest.amqp.AmqpDistribution.java

License:Open Source License

public void distribute(Message message) throws Exception {

    List<String> connectionNames = this.connectionNames;

    // Convert message properties
    AMQP.BasicProperties props = new AMQP.BasicProperties();
    if (message.persistent) {
        props.setDeliveryMode(Integer.valueOf(2));
    } else {//  w w  w  . j ava2 s . c  o m
        props.setDeliveryMode(Integer.valueOf(1));
    }
    props.setCorrelationId(message.correlationId);
    props.setPriority(Integer.valueOf(message.priority));
    props.setTimestamp(new Date(message.timestamp));
    props.setMessageId(message.id);
    props.setType(String.valueOf(message.type));
    props.setExpiration(String.valueOf(message.expiration));

    if (message.properties != null) {
        Map<String, Object> headers = new HashMap<String, Object>();
        message.properties.copyInto(headers);
        props.setHeaders(headers);

        Object customRouting = message.properties.get(ROUTING_PROP);
        if (customRouting != null && customRouting instanceof String) {
            connectionNames = AmqpConnectionService.convertToList((String) customRouting);
        }
    }

    // Update channels if necessary
    long now = System.currentTimeMillis();
    if (now - lastUpdate > updatePeriod) {
        if (logger.isLoggable(BasicLevel.DEBUG)) {
            logger.log(BasicLevel.DEBUG, "Updating channels.");
        }
        List<LiveServerConnection> connections = AmqpConnectionService.getInstance().getConnections();
        for (LiveServerConnection connection : connections) {
            if (!channels.containsKey(connection.getName())) {
                if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG,
                            connection.getName() + ": New channel available for distribution.");
                }
                try {
                    Channel channel = connection.getConnection().createChannel();
                    if (amqpQueuePassive) {
                        channel.queueDeclarePassive(amqpQueue);
                    } else {
                        channel.queueDeclare(amqpQueue, amqpQueueDurable, amqpQueueExclusive,
                                amqpQueueAutoDelete, null);
                    }
                    channels.put(connection.getName(), channel);
                } catch (IOException exc) {
                    if (logger.isLoggable(BasicLevel.DEBUG)) {
                        logger.log(BasicLevel.DEBUG, "Channel is not usable.", exc);
                    }
                }
            }
        }
        lastUpdate = now;
    }

    // Send the message
    Iterator<Map.Entry<String, Channel>> iter = channels.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, Channel> entry = iter.next();
        try {
            Channel chan = entry.getValue();
            String cnxName = entry.getKey();
            if (!chan.isOpen()) {
                iter.remove();
                continue;
            }
            if (connectionNames != null && !connectionNames.contains(cnxName)) {
                continue;
            }
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "Sending message on " + cnxName);
            }
            chan.basicPublish("", amqpQueue, props, message.getBody());
            channels.get(cnxName); // Access the used connection to update the LRU map
            return;
        } catch (IOException exc) {
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "Channel is not usable, remove from table.", exc);
            }
            iter.remove();
        } catch (AlreadyClosedException exc) {
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "Channel is not usable, remove from table.", exc);
            }
            iter.remove();
        }
    }

    throw new Exception("Message could not be sent, no usable channel found.");
}

From source file:org.objectweb.proactive.extensions.amqp.federation.AMQPFederationRemoteObjectServer.java

License:Open Source License

@Override
protected void createObjectQueue(Channel channel, String queueName) throws IOException {
    boolean autoDelete = true;
    boolean durable = false;
    boolean exclusive = false;
    Map<String, Object> arguments = null;

    channel.queueDeclare(queueName, durable, exclusive, autoDelete, arguments);
    channel.queueBind(queueName, AMQPFederationConfig.PA_AMQP_FEDERATION_DISCOVER_EXCHANGE_NAME.getValue(), "");
    channel.queueBind(queueName, AMQPFederationConfig.PA_AMQP_FEDERATION_RPC_EXCHANGE_NAME.getValue(),
            queueName);/*from  w w w  . j a v a 2s  . c om*/
}

From source file:org.objectweb.proactive.extensions.amqp.federation.FederationFindQueuesRPCClient.java

License:Open Source License

@Override
protected String createReplyQueue(Channel channel) throws IOException {
    String replyQueueName = AMQPFederationUtils.uniqueQueueName("reply_discover");
    channel.queueDeclare(replyQueueName, false, true, true, null);
    channel.queueBind(replyQueueName,/*from w  w  w.  j  a v  a  2  s.c  o  m*/
            AMQPFederationConfig.PA_AMQP_FEDERATION_RPC_REPLY_EXCHANGE_NAME.getValue(), replyQueueName);
    return replyQueueName;
}

From source file:org.objectweb.proactive.extensions.amqp.remoteobject.AMQPRemoteObjectServer.java

License:Open Source License

@Override
protected void createObjectQueue(Channel channel, String queueName) throws IOException {
    boolean autoDelete = true;
    boolean durable = false;
    boolean exclusive = false;
    Map<String, Object> arguments = null;

    channel.queueDeclare(queueName, durable, exclusive, autoDelete, arguments);
    channel.queueBind(queueName, AMQPConfig.PA_AMQP_DISCOVER_EXCHANGE_NAME.getValue(), "");
    channel.queueBind(queueName, AMQPConfig.PA_AMQP_RPC_EXCHANGE_NAME.getValue(), queueName);
}

From source file:org.opendaylight.federationmessagequeue.impl.RabbitMessageBus.java

License:Open Source License

@Override
public boolean createQueue(String queueName, String mqBrokerIp, int mqPortNumber, String mqUser,
        String mqUserPwd) {/*from ww  w .  j av a2 s .c o  m*/
    LOG.info("Creating connection for queue {} on broker {}", queueName, mqBrokerIp);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(mqBrokerIp);
    factory.setPort(mqPortNumber);
    factory.setUsername(mqUser);
    factory.setPassword(mqUserPwd);
    factory.setAutomaticRecoveryEnabled(true);

    try {
        Connection connection = factory.newConnection();
        LOG.info("Created connection to broker {}:{} for user {} ", mqBrokerIp, mqPortNumber, mqUser);
        Channel channel = connection.createChannel();
        channel.queueDeclare(queueName, false, false, false, null);
        LOG.info("Declared queue {} on broker {}", queueName, mqBrokerIp);
        MessageBusConnectionData mbcd = new MessageBusConnectionData(mqBrokerIp, connection, channel);
        queueNameToConnectionData.put(queueName, mbcd);
        return true;
    } catch (IOException | TimeoutException e) {
        LOG.warn("Failed creating queue {} on broker {}:{} for user {} because: {}", queueName, mqBrokerIp,
                mqPortNumber, mqUser, e.getMessage());
        return false;
    }
}