Example usage for com.rabbitmq.client Channel exchangeDeclare

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

Introduction

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

Prototype

Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable,
        boolean autoDelete, boolean internal, Map<String, Object> arguments) throws IOException;

Source Link

Document

Declare an exchange, via an interface that allows the complete set of arguments.

Usage

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumer.java

License:Apache License

RabbitMQMessageConsumer(String consumerName) {
    try {/*from   ww w.  j  a va 2  s  .  c  o  m*/
        this.consumerName = consumerName;
        Configuration subset = configuration.subset(consumerName);
        queueName = subset.getString("queue.name");

        String filter = subset.getString("queue.filter", "");
        boolean isAutoDelete = subset.getBoolean("queue.isAutoDelete", false);
        boolean isDurable = subset.getBoolean("queue.isDurable", true);
        boolean isSubscription = subset.getBoolean("queue.isSubscription", false);
        long expiration = subset.getLong("queue.expiration", 1800000);
        long maxLength = subset.getLong("queue.maxLength", -1);
        boolean deadLetterIsEnabled = subset.getBoolean("queue.deadLetterIsEnabled", true);
        String deadLetterExchangeName = subset.getString("queue.deadLetterExchangeName", DLQ);
        String subscribedTo = isSubscription ? subset.getString("queue.subscribedTo", "") : queueName;
        String exchangeType = subset.getString("queue.exchangeType", isSubscription ? "topic" : "direct");
        boolean isExclusive = subset.getBoolean("queue.isExclusive", false);
        try {
            RabbitMQMessagingFactory.INIT_LATCH.await();
        } catch (InterruptedException e) {
            LOGGER.error("error waiting for init to finish: " + e);
        }
        Channel channel = RabbitMQMessagingFactory.getChannel();
        channel.exchangeDeclare(subscribedTo, exchangeType, isDurable, false, false, null);

        Map<String, Object> args = new HashMap<String, Object>();

        if (maxLength > 0) {
            args.put("x-max-length", maxLength);
        }

        if (expiration > 0) {
            args.put("x-message-ttl", expiration);
        }

        if (deadLetterIsEnabled) {
            channel.exchangeDeclare(deadLetterExchangeName, exchangeType, isDurable, false, false, null);
            args.put("x-dead-letter-exchange", deadLetterExchangeName);
        }

        String queue = channel.queueDeclare(queueName, isDurable, isExclusive, isAutoDelete, args).getQueue();

        Map<String, String> filters = ConfigUtil.parseSimpleArrayAsMap(consumerName + ".queue.filters");
        if (filters != null && !filters.isEmpty()) {
            for (String routingKey : filters.values()) {
                channel.queueBind(queue, subscribedTo, routingKey);
            }
        } else {
            channel.queueBind(queue, subscribedTo, "#");
        }

        consumer = new QueueingConsumer(channel);
        //            channel.basicConsume(queueName, true, consumer);
        LOGGER.info("created rabbitmq consumer: {} on exchange: {}, queue-name: {}", consumerName, subscribedTo,
                queueName);
    } catch (IOException e) {
        throw new QueueException("Can't create consumer: " + e, e);
    }

}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumerWithoutTL.java

License:Apache License

RabbitMQMessageConsumerWithoutTL(String consumerName) {
    try {/*ww w  . j  a  v a  2  s  .c o m*/
        this.consumerName = consumerName;
        Configuration subset = configuration.subset(consumerName);
        queueName = subset.getString("queue.name");

        String filter = subset.getString("queue.filter", "");
        boolean isAutoDelete = subset.getBoolean("queue.isAutoDelete", false);
        boolean isDurable = subset.getBoolean("queue.isDurable", true);
        boolean isSubscription = subset.getBoolean("queue.isSubscription", false);
        long expiration = subset.getLong("queue.expiration", 1800000);
        long maxLength = subset.getLong("queue.maxLength", -1);
        boolean deadLetterIsEnabled = subset.getBoolean("queue.deadLetterIsEnabled", true);
        String deadLetterExchangeName = subset.getString("queue.deadLetterExchangeName", DLQ);
        String subscribedTo = isSubscription ? subset.getString("queue.subscribedTo", "") : queueName;
        String exchangeType = subset.getString("queue.exchangeType", isSubscription ? "topic" : "direct");
        boolean isExclusive = subset.getBoolean("queue.isExclusive", false);
        try {
            RabbitMQMessagingFactory.INIT_LATCH.await();
        } catch (InterruptedException e) {
            LOGGER.error("error waiting for init to finish: " + e);
        }
        Channel channel = RabbitMQMessagingFactory.createChannelWithoutTL();
        channel.exchangeDeclare(subscribedTo, exchangeType, isDurable, false, false, null);

        Map<String, Object> args = new HashMap<String, Object>();

        if (maxLength > 0) {
            args.put("x-max-length", maxLength);
        }

        if (expiration > 0) {
            args.put("x-message-ttl", expiration);
        }

        if (deadLetterIsEnabled) {
            channel.exchangeDeclare(deadLetterExchangeName, exchangeType, isDurable, false, false, null);
            args.put("x-dead-letter-exchange", deadLetterExchangeName);
        }

        String queue = channel.queueDeclare(queueName, isDurable, isExclusive, isAutoDelete, args).getQueue();

        Map<String, String> filters = ConfigUtil.parseSimpleArrayAsMap(consumerName + ".queue.filters");
        if (filters != null && !filters.isEmpty()) {
            for (String routingKey : filters.values()) {
                channel.queueBind(queue, subscribedTo, routingKey);
            }
        } else {
            channel.queueBind(queue, subscribedTo, "#");
        }

        consumer = new QueueingConsumer(channel);
        //            channel.basicConsume(queueName, true, consumer);
        LOGGER.info("created rabbitmq consumer: {} on exchange: {}, queue-name: {} channel number {}",
                consumerName, subscribedTo, queueName, channel.getChannelNumber());
    } catch (IOException e) {
        throw new QueueException("Can't create consumer: " + e, e);
    }

}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageProducer.java

License:Apache License

RabbitMQMessageProducer(String producerName) {
    super(producerName);
    Configuration subset = configuration.subset(producerName);
    queueName = subset.getString("queue.name");

    if (StringUtils.isBlank(queueName)) {
        throw new QueueException(
                "Check Configuration - missing required queue name for producer: " + producerName);
    }//from  ww w  . ja va2 s . c  o  m

    //update expiration
    expiration = subset.getLong("queue.expiration", 1800000);
    groupId = subset.getString("queue.groupId", "");
    exchangeType = subset.getString("queue.exchangeType", "topic");
    isDurable = subset.getBoolean("queue.isDurable", true);
    isPersistent = subset.getBoolean("queue.isPersistent", true);

    try {
        Channel channel = RabbitMQMessagingFactory.getChannel();
        channel.exchangeDeclare(queueName, exchangeType, isDurable, false, false, null);
        isInitialized.set(true);
    } catch (QueueException e) {
        LOGGER.debug("can't init producer as its underlying connection is not ready");
    } catch (IOException e) {
        throw new QueueException("Can't create producer: " + e, e);
    }

    LOGGER.info("created rabbitmq producer: {} on exchange: {}", producerName, queueName);

}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageProducer.java

License:Apache License

@Override
public void sendMessage(byte[] message, Map<String, Object> messageHeaders) {

    if (!RabbitMQMessagingFactory.IS_BLOCKED.get()) {
        messageHeaders.put(QueueConstants.FLOW_CONTEXT_HEADER, FlowContextFactory.serializeNativeFlowContext());

        if (isInitialized.get()) {
            sendMessageInternal(message, messageHeaders);
        } else {/*w ww  .ja  v a  2  s  .c  o m*/
            try {
                Channel channel = RabbitMQMessagingFactory.getChannel();
                channel.exchangeDeclare(queueName, "topic", true, false, false, null);
                isInitialized.set(true);
                sendMessageInternal(message, messageHeaders);
            } catch (Exception e) {
                String errorMsg = "can't init producer as it is underlying connection is not ready";
                LOGGER.warn(errorMsg);
                throw new QueueException(errorMsg, e);
            }
        }
    } else {
        throw new QueueException("RabbitMQ connection is blocked");
    }

}

From source file:edu.kit.dama.util.test.RabbitMQTest.java

License:Apache License

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, false, null);
    //channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    /*String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");*/

    String message = "Hello!";

    channel.basicPublish(EXCHANGE_NAME, "", MessageProperties.MINIMAL_PERSISTENT_BASIC, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();/*from  w  ww .  j  a v  a  2s  . c om*/
    connection.close();

}

From source file:io.bootique.rabbitmq.client.channel.ExchangeConfig.java

License:Apache License

public void exchangeDeclare(Channel channel, String exchangeName) throws IOException {
    channel.exchangeDeclare(exchangeName, type, durable, autoDelete, internal, arguments);
}

From source file:user.Client.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("rabbitmq.akhfa.me");
    factory.setUsername(username);//from www .  j  a  v  a2s. c o  m
    factory.setPassword(password);
    factory.setVirtualHost("pat");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    ArrayList<String> daftarNick = getAllQueues();

    String nick = "";

    while (true) {
        Scanner in = new Scanner(System.in);

        System.out.print("Please enter your command: ");
        String command = in.nextLine();

        String[] com = command.split(" ", 2);
        try {
            switch (com[0]) {
            case "/NICK":
                if (!nick.equals("")) {
                    System.out.println("You have registered with nickname: " + nick);
                } else {
                    if (!daftarNick.contains(com[1])) {
                        channel.queueDeclare(com[1], false, false, true, null);
                        nick = com[1];
                        System.out.println("Your nickname is " + nick);

                        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

                        Consumer consumer = new DefaultConsumer(channel) {
                            @Override
                            public void handleDelivery(String consumerTag, Envelope envelope,
                                    AMQP.BasicProperties properties, byte[] body) throws IOException {
                                String message = new String(body, "UTF-8");
                                filterChannel(message);
                            }
                        };
                        channel.basicConsume(nick, true, consumer);
                    } else {
                        System.out.println("Nickname exists.");
                    }
                }
                break;
            case "/JOIN":
                channel.exchangeDeclare(com[1], "fanout", false, false, false, null);
                channel.queueBind(nick, com[1], "");
                System.out.println("You have successfully join " + com[1]);
                break;
            case "/LEAVE":
                channel.queueUnbind(nick, com[1], "");
                System.out.println("Leave " + com[1]);
                break;
            case "/EXIT":
                System.out.println("bye bye...  :D");
                System.exit(0);
            default:
                String message = nick + ' ' + command;
                channel.basicPublish(com[0].substring(1), "", null, message.getBytes("UTF-8"));
                break;
            }
        } catch (Exception e) {
            if (command.compareTo("/NICK") == 0) {
                //random nick
                String random;

                if (!nick.equals("")) {
                    System.out.println("You have registered with nickname: " + nick);
                } else {
                    do {
                        random = randomNick();
                    } while (daftarNick.contains(random));

                    nick = random;

                    channel.queueDeclare(random, false, false, true, null);
                    System.out.println("Your nickname is " + random);

                    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

                    Consumer consumer = new DefaultConsumer(channel) {
                        @Override
                        public void handleDelivery(String consumerTag, Envelope envelope,
                                AMQP.BasicProperties properties, byte[] body) throws IOException {
                            String message = new String(body, "UTF-8");
                            filterChannel(message);
                        }
                    };
                    channel.basicConsume(nick, true, consumer);
                }

            } else if ((command.compareTo("/JOIN") == 0) || (command.compareTo("/LEAVE") == 0)) {
                //error
                System.out.println("Please enter channel name!");
            } else if (command.charAt(0) == '@') {
                System.out.println("Please enter your command for the channel.");
            } else {
                System.out.println("Invalid command.");
            }
        }
    }
}