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:org.trpr.mule.transport.rabbitmq.EndpointUtils.java

License:Apache License

/**
 * Declares an Exchange on the specified Channel using the exchange details derived from the specified Endpoint
 *//* w w  w .j a v  a2s . com*/
public static String declareExchange(Channel channel, ImmutableEndpoint e) throws IOException {
    String exchange = EndpointUtils.getExchange(e);
    if (exchange != null) {
        channel.exchangeDeclare(exchange, EndpointUtils.getExchangeType(e), getDurable(e));
    }
    return exchange;
}

From source file:org.wso2.carbon.event.adapter.rabbitmq.internal.util.RabbitMQEventAdapterUtils.java

License:Open Source License

/**
 * @param connection      Connection to the RabbitMQ
 * @param exchangeName    Name of the exchange
 * @param exchangeType    Type of Exchange
 * @param exchangeDurable Whether durable or not
 * @throws IOException/*ww  w  . ja  v  a2  s.com*/
 */
public static void declareExchange(Connection connection, String exchangeName, String exchangeType,
        String exchangeDurable) throws IOException, TimeoutException {
    Boolean exchangeAvailable = false;
    Channel channel = connection.createChannel();
    try {
        // check availability of the named exchange.
        // The server will raise an IOException if the named exchange already exists.
        channel.exchangeDeclarePassive(exchangeName);
        exchangeAvailable = true;
    } catch (IOException e) {
        log.info("Exchange :" + exchangeName + " not found.Declaring exchange.");
    }
    if (!exchangeAvailable) {
        // Declare the named exchange if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }
        try {
            if (exchangeType != null && !exchangeType.equals("")) {
                if (exchangeDurable != null && !exchangeDurable.equals("")) {
                    channel.exchangeDeclare(exchangeName, exchangeType, Boolean.parseBoolean(exchangeDurable));
                } else {
                    channel.exchangeDeclare(exchangeName, exchangeType, true);
                }
            } else {
                channel.exchangeDeclare(exchangeName, RabbitMQInputEventAdapterConstants.DEFAULT_EXCHANGE_TYPE,
                        true);
            }
        } catch (IOException e) {
            handleException("Error occurred while declaring exchange.", e);
        }
    }
    channel.close();
}

From source file:org.wso2.carbon.extension.analytics.receiver.rabbitmq.internal.util.RabbitMQUtils.java

License:Open Source License

public static void declareExchange(Connection connection, String exchangeName, String exchangeType,
        String exchangeDurable) throws IOException {
    Boolean exchangeAvailable = false;
    Channel channel = connection.createChannel();
    try {//from  w  ww.  ja  va 2  s  . c o  m
        // check availability of the named exchange.
        // The server will raise an IOException
        // if the named exchange already exists.
        channel.exchangeDeclarePassive(exchangeName);
        exchangeAvailable = true;
    } catch (IOException e) {
        log.info("Exchange :" + exchangeName + " not found.Declaring exchange.");
    }

    if (!exchangeAvailable) {
        // Declare the named exchange if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }
        try {
            if (exchangeType != null && !exchangeType.equals("")) {
                if (exchangeDurable != null && !exchangeDurable.equals("")) {
                    channel.exchangeDeclare(exchangeName, exchangeType, Boolean.parseBoolean(exchangeDurable));
                } else {
                    channel.exchangeDeclare(exchangeName, exchangeType, true);
                }
            } else {
                channel.exchangeDeclare(exchangeName, "direct", true);
            }
        } catch (IOException e) {
            handleException("Error occurred while declaring exchange.", e);
        }
    }
    channel.close();
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQUtils.java

License:Open Source License

public static void declareExchange(Connection connection, String exchangeName,
        Hashtable<String, String> properties) throws IOException {
    Boolean exchangeAvailable = false;
    Channel channel = connection.createChannel();
    String exchangeType = properties.get(RabbitMQConstants.EXCHANGE_TYPE);
    String durable = properties.get(RabbitMQConstants.EXCHANGE_DURABLE);
    try {//w  w  w.j  a  v  a  2  s. com
        // check availability of the named exchange.
        // The server will raise an IOException
        // if the named exchange already exists.
        channel.exchangeDeclarePassive(exchangeName);
        exchangeAvailable = true;
    } catch (IOException e) {
        log.info("Exchange :" + exchangeName + " not found.Declaring exchange.");
    }

    if (!exchangeAvailable) {
        // Declare the named exchange if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }
        try {
            if (exchangeType != null && !exchangeType.equals("")) {
                if (durable != null && !durable.equals("")) {
                    channel.exchangeDeclare(exchangeName, exchangeType, Boolean.parseBoolean(durable));
                } else {
                    channel.exchangeDeclare(exchangeName, exchangeType, true);
                }
            } else {
                channel.exchangeDeclare(exchangeName, "direct", true);
            }
        } catch (IOException e) {
            handleException("Error occurred while declaring exchange.", e);
        }
    }
    try {
        channel.close();
    } catch (TimeoutException e) {
        log.error("Error occurred while closing connection.", e);
    }
}

From source file:ox.softeng.burst.service.message.RabbitMessageService.java

@Override
public void run() {
    for (int c = 0; c < consumerCount; c++) {
        try {/* w w w  . j  a v a  2  s .  co  m*/
            Channel channel = connection.createChannel();
            channel.basicQos(40);
            channel.exchangeDeclare(exchange, "topic", true);
            Consumer consumer = new MessageConsumer(channel, entityManagerFactory);
            channel.basicConsume(queue, true, consumer);
        } catch (IOException e) {
            logger.error("Error running consumer for queue '" + queue + "'", e);
        } catch (Exception e) {
            logger.error("Unhandled exception: " + e.getMessage(), e);
        }
    }
}

From source file:ox.softeng.burst.services.RabbitService.java

License:Open Source License

public void run() {
    try {//from  w ww  .j  a  v a2  s .com
        Channel channel = connection.createChannel();
        channel.basicQos(40);
        channel.exchangeDeclare(rabbitMQExchange, "topic", true);
        Consumer consumer = createConsumer(channel);
        logger.warn("Waiting for messages");
        channel.basicConsume(rabbitMQQueue, true, consumer);
    } catch (IOException e) {
        logger.error("Error running consumer for queue '" + rabbitMQQueue + "'", e);
    } catch (Exception e) {
        logger.error("Unhandled exception: " + e.getMessage(), e);
    }
}

From source file:ox.softeng.gel.filereceive.FileReceive.java

public void generateStartupMessage() throws IOException {

    try {//from  ww w. j a v a 2  s  .c o m
        StringWriter writer;
        MessageDTO message = new MessageDTO();
        message.setSource("file-receiver");
        message.setDetails("Burst Service starting\n" + version());
        message.setSeverity(SeverityEnum.INFORMATIONAL);
        message.setDateTimeCreated(OffsetDateTime.now(ZoneId.of("UTC")));
        message.setTitle("File Receiver Startup");
        message.addTopic("service");
        message.addTopic("startup");
        message.addTopic("file-receiver");
        message.addMetadata("gmc", "gel");
        message.addMetadata("file_receiver_service_version", version());

        writer = new StringWriter();
        getMarshaller().marshal(message, writer);

        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
        builder.deliveryMode(2);
        builder.contentType("text/xml");
        builder.timestamp(Date.from(OffsetDateTime.now(ZoneId.systemDefault()).toInstant()));

        Channel channel = factory.newConnection().createChannel();
        channel.exchangeDeclare(exchangeName, "topic", true);
        channel.basicPublish(exchangeName, burstQueue, builder.build(), writer.toString().getBytes());
        channel.close();

    } catch (JAXBException | TimeoutException ignored) {
    }
}

From source file:play.modules.rabbitmq.RabbitMQPlugin.java

License:Apache License

/**
 * Creates the channel./*from w  ww.j ava 2s  .c  o  m*/
 * 
 * @param queue
 *            the queue
 * @return the channel
 * @throws Exception
 *             the exception
 */
public Channel createChannel(String queue, String routingKey) throws Exception {
    // Counter that keeps track of number of retries
    int attempts = 0;

    // Get Plugin
    RabbitMQPlugin plugin = Play.application().plugin(RabbitMQPlugin.class);

    // Create Channel
    Channel channel = this.createChannel();

    // Basic Qos
    if (RabbitMQPlugin.isBasicQos()) {
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
    }

    // Start Daemon
    while (true) {
        // Add to the number of retries
        attempts++;

        // Log Debug
        Logger.debug("Retry " + attempts);

        // Get Next Delivery Message
        try {
            // http://www.rabbitmq.com/api-guide.html
            // channel.exchangeDeclare(exchangeName, "direct", true);
            // String queueName = channel.queueDeclare().getQueue();
            // channel.queueBind(queueName, exchangeName, routingKey);

            channel.exchangeDeclare(queue, plugin.getExchangeType(), true);
            channel.queueDeclare(queue, plugin.isDurable(), false, false, null);
            channel.queueBind(queue, queue, routingKey);

            // Log Debug
            Logger.info("RabbitMQ Task Channel Available: " + channel);

            // Return Channel
            return channel;

        } catch (Throwable t) {
            // Log Debug
            Logger.error("Error establishing a connection to RabbitMQ, will keep retrying - Exception: %s",
                    ExceptionUtil.getStackTrace(t));

            // Sleep a little while before retrying
            try {
                Thread.sleep(1000 * 10);
            } catch (InterruptedException ex) {
            }
        }
    }
}

From source file:play.modules.rabbitmq.RabbitMQPlugin.java

License:Apache License

/**
    * Creates the channel for a subscriber to consume messages from an exchange
    * (Exchange is created.  Subscribers queue and binding by routing key are created)
    * //from   www.  j av  a  2  s  .c  o  m
    * @param exchangeName
    *            the exchange name
    * @param queue
    *            the queue
    * @param routingKey
    *            the routing key
    * @return the channel
    * @throws Exception
    *             the exception
    */
public Channel createSubscribersChannel(String exchangeName, String queue, String routingKey) throws Exception {
    // Counter that keeps track of number of retries
    int attempts = 0;

    // Get Plugin
    RabbitMQPlugin plugin = Play.application().plugin(RabbitMQPlugin.class);

    // Create Channel
    Channel channel = this.createChannel();

    // Basic Qos
    if (RabbitMQPlugin.isBasicQos()) {
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
    }

    // Start Daemon
    while (true) {
        // Add to the number of retries
        attempts++;

        // Log Debug
        Logger.debug("Retry " + attempts);

        // Get Next Delivery Message
        try {
            // http://www.rabbitmq.com/api-guide.html
            channel.exchangeDeclare(exchangeName, plugin.getExchangeType(), true);
            channel.queueDeclare(queue, plugin.isDurable(), false, false, null);
            channel.queueBind(queue, exchangeName, routingKey);

            // Log Debug
            Logger.info("RabbitMQ Task Channel Available: " + channel);

            // Return Channel
            return channel;

        } catch (Throwable t) {
            // Log Debug
            Logger.error("Error establishing a connection to RabbitMQ, will keep retrying - Exception: %s",
                    ExceptionUtil.getStackTrace(t));

            // Sleep a little while before retrying
            try {
                Thread.sleep(1000 * 10);
            } catch (InterruptedException ex) {
            }
        }
    }
}

From source file:play.modules.rabbitmq.RabbitMQPlugin.java

License:Apache License

/**
 * Creates the channel to publish messages to an exchange
 * (Exchange is created.  Queue and bindings are NOT created)
 * //from   ww w  .  ja  va 2s .c o  m
 * @param exchangeName
 *            the exchange name
 * @return the channel
 * @throws Exception
 *             the exception
 */
public Channel createPublishersChannel(String exchangeName) throws Exception {
    // Counter that keeps track of number of retries
    int attempts = 0;

    // Get Plugin
    RabbitMQPlugin plugin = Play.application().plugin(RabbitMQPlugin.class);

    // Create Channel
    Channel channel = this.createChannel();

    // Basic Qos
    if (RabbitMQPlugin.isBasicQos()) {
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
    }

    // Start Daemon
    while (true) {
        // Add to the number of retries
        attempts++;

        // Log Debug
        Logger.debug("Retry " + attempts);

        // Get Next Delivery Message
        try {
            // http://www.rabbitmq.com/api-guide.html
            channel.exchangeDeclare(exchangeName, plugin.getExchangeType(), true);

            // Log Debug
            Logger.info("RabbitMQ Task Channel Available: " + channel);

            // Return Channel
            return channel;

        } catch (Throwable t) {
            // Log Debug
            Logger.error("Error establishing a connection to RabbitMQ, will keep retrying - Exception: %s",
                    ExceptionUtil.getStackTrace(t));

            // Sleep a little while before retrying
            try {
                Thread.sleep(1000 * 10);
            } catch (InterruptedException ex) {
            }
        }
    }
}