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)
        throws IOException;

Source Link

Document

Actively declare a non-autodelete exchange with no extra arguments

Usage

From source file:io.qdb.server.input.RabbitMQInputHandler.java

License:Apache License

protected void initChannel(Channel channel) throws IOException {
    channel.queueDeclare(queue, queueDurable, false, false, null);
    if (exchange != null) {
        channel.exchangeDeclare(exchange, exchangeType, exchangeDurable);
        channel.queueBind(queue, exchange, routingKey);
    }/*from ww w  .  jav  a2  s. c o m*/
}

From source file:io.qdb.server.output.RabbitMQOutputHandler.java

License:Apache License

protected void initChannel(Channel channel) throws IOException {
    channel.exchangeDeclare(exchange, exchangeType, exchangeDurable);
    if (queues != null) {
        for (int i = 0; i < queues.length; i++) {
            String q = queues[i];
            channel.queueDeclare(q, queueDurable[i], false, false, null);
            channel.queueBind(q, exchange, "");
        }//from  w ww  .j av a  2s  . c  o  m
    }
}

From source file:it.av.fac.messaging.rabbitmq.RabbitMQChannelPool.java

public static synchronized Channel createChannel(Connection conn, String queueIn, String queueOut,
        String routingKeyIn, String routingKeyOut) throws IOException {
    String UID = obtainUniqueId(queueIn, queueOut, routingKeyIn, routingKeyOut);

    if (!POOL.containsKey(UID) || !POOL.get(UID).isOpen()) {
        Channel channel = conn.createChannel();
        channel.exchangeDeclare(RabbitMQConstants.EXCHANGE, "direct", true);
        if (queueIn != null) {
            channel.queueDeclare(queueIn, false, false, true, null);
            channel.queueBind(queueIn, RabbitMQConstants.EXCHANGE, routingKeyIn);
        }//from w w  w  .  j  a  v a 2 s.  com
        if (queueOut != null) {
            channel.queueDeclare(queueOut, false, false, true, null);
            channel.queueBind(queueOut, RabbitMQConstants.EXCHANGE, routingKeyOut);
        }

        POOL.put(UID, channel);
    }

    return POOL.get(UID);
}

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

License:Apache License

/**   
 * Creates a new exchange//from w  w w  . j ava2  s.  c o  m
 * @throws IOException 
 */
public void creates(Channel channel, String exchange) throws IOException {
    channel.exchangeDeclare(exchange, "topic", true);

}

From source file:itinno.example.ExampleSocialMediaStormDeclarator.java

/**
 * Main RabbitMQ declaration method. Will use RabbitMQ channel reference.
 * /*from ww w .  j  av  a2 s .  com*/
 * 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:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  ?./* ww  w .  j av a 2 s  .c o  m*/
 * <p>
 * exchange = fanout
 * ??? topic ?
 *
 * @param topic   
 * @param message ?
 * @param confirm ??
 * @return ???rabbit confirm ?????
 */
public boolean doPublish(String topic, String message, boolean confirm) {
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, true);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0,
                null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke(topic, "", properties);
        channel.basicPublish(topic, "", properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit publish error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit publish error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit publish error.", e);
        }
        connection.close();
    }
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  .//from   w  w w.j a  v a  2 s .c  om
 * <p>
 * exchange = fanout
 * ??
 *
 * @param topic    
 * @param consumer ?
 */
@Override
protected void doSubscribe(String topic, Consumer<String> consumer) {
    Channel channel = rabbitAdapter.getConnection().createChannel(false);
    try {
        channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, true);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, topic, "");
        channel.basicQos(1);
        channel.basicConsume(queueName, false,
                getDefaultConsumer(channel, topic, topic, "", queueName, consumer));
    } catch (IOException e) {
        logger.error("[MQ] Rabbit response error.", e);
    }
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  ?./*from   w  ww  . jav a2  s  . c  o  m*/
 * <p>
 * exchange = topic
 *
 * @param topic      
 * @param routingKey Key
 * @param queueName  ??
 * @param message    ?
 * @param confirm    ??
 * @return ???rabbit confirm ?????
 */
public boolean publishWithTopic(String topic, String routingKey, String queueName, String message,
        boolean confirm) {
    logger.trace("[MQ] publishWithTopic {}:{}", topic, message);
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.queueDeclare(queueName, true, false, false, null);
        channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0,
                null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke(topic, routingKey, properties);
        channel.basicPublish(topic, routingKey, properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit publishWithTopic error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit publishWithTopic error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit publishWithTopic error.", e);
        }
        connection.close();
    }
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  .//w w w  . j a  v a  2 s.c om
 * <p>
 * exchange = topic
 * ??
 *
 * @param topic      
 * @param routingKey Key
 * @param queueName  ??
 * @param consumer   ?
 */
public void subscribeWithTopic(String topic, String routingKey, String queueName, Consumer<String> consumer) {
    Channel channel = rabbitAdapter.getConnection().createChannel(false);
    try {
        channel.queueDeclare(queueName, true, false, false, null);
        channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true);
        channel.queueBind(queueName, topic, routingKey);
        channel.basicQos(1);
        channel.basicConsume(queueName, false,
                getDefaultConsumer(channel, topic, topic, routingKey, queueName, consumer));
    } catch (IOException e) {
        logger.error("[MQ] Rabbit subscribeWithTopic error.", e);
    }
}

From source file:mx.bigdata.utils.amqp.AMQPClientHelperImpl.java

License:Apache License

public Channel declareChannel(ConnectionFactory factory, String key) throws Exception {
    Connection conn = factory.newConnection();
    Channel channel = conn.createChannel();
    Integer basicQos = conf.getInteger("channel_basic_qos");
    if (basicQos != null) {
        channel.basicQos(basicQos);//from  w  w  w.  ja  va 2 s. c  o m
    } else {
        channel.basicQos(DEFAULT_QOS);
    }
    channel.exchangeDeclare(getExchangeName(key), getExchangeType(key), true);
    return channel;
}