Example usage for com.rabbitmq.client Connection createChannel

List of usage examples for com.rabbitmq.client Connection createChannel

Introduction

In this page you can find the example usage for com.rabbitmq.client Connection createChannel.

Prototype

Channel createChannel() throws IOException;

Source Link

Document

Create a new channel, using an internally allocated channel number.

Usage

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//from w  w  w . j  a  v  a2 s.c o  m
 */
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.event.adaptor.rabbitmq.input.RabbitMQInputEventAdaptorTypeTest.java

License:Apache License

@AfterClass
public static void afterClass() {
    try {//from  w w  w.  j a v a 2  s .c  o m
        ConnectionFactory factory = EventAdapterHelper.getConnectionFactory("localhost", 5672, "admin", "admin",
                "/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDelete("wso2cep_test_input_queue");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.carbon.event.adaptor.rabbitmq.output.RabbitMQOutputEventAdaptorTypeTest.java

License:Apache License

@AfterClass
public static void afterClass() {
    try {/*ww w  . j  a v  a 2 s. c  o m*/
        ConnectionFactory factory = EventAdapterHelper.getConnectionFactory("localhost", 5672, "admin", "admin",
                "/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDelete("wso2cep_test_output_queue");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

public static boolean isQueueAvailable(Connection connection, String queueName) throws IOException {
    Channel channel = connection.createChannel();
    try {//www  .j  a  va  2 s.c  o m
        // check availability of the named queue
        // if an error is encountered, including if the queue does not exist and if the
        // queue is exclusively owned by another connection
        channel.queueDeclarePassive(queueName);
        return true;
    } catch (IOException e) {
        return false;
    }
}

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 . j  av  a 2s .  co  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

/**
 *
 * @param connection//from ww  w . j a  v  a2 s  . co m
 * @param queueName
 * @param isDurable
 * @param isExclusive
 * @param isAutoDelete
 * @throws IOException
 */
public static void declareQueue(Connection connection, String queueName, boolean isDurable, boolean isExclusive,
        boolean isAutoDelete) throws IOException {

    boolean queueAvailable = isQueueAvailable(connection, queueName);
    Channel channel = connection.createChannel();

    if (!queueAvailable) {
        if (log.isDebugEnabled()) {
            log.debug("Queue :" + queueName + " not found or already declared exclusive. Declaring the queue.");
        }
        // Declare the named queue if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }
        try {
            channel.queueDeclare(queueName, isDurable, isExclusive, isAutoDelete, null);
        } catch (IOException e) {
            handleException("Error while creating queue: " + queueName, e);
        }
    }
}

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

License:Open Source License

public static void declareQueue(Connection connection, String queueName, Hashtable<String, String> properties)
        throws IOException {
    Boolean queueAvailable = isQueueAvailable(connection, queueName);
    Channel channel = connection.createChannel();

    if (!queueAvailable) {
        // Declare the named queue if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }/*from  www  . j a v a 2 s.c om*/
        try {
            channel.queueDeclare(queueName, isDurableQueue(properties), isExclusiveQueue(properties),
                    isAutoDeleteQueue(properties), null);

        } catch (IOException e) {
            handleException("Error while creating queue: " + queueName, e);
        }
    }
}

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 a2 s.  c  om
        // 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:org.wso2.siddhi.debs2017.input.rabbitmq.RabbitMQConsumer.java

License:Open Source License

/**
 *  Consumes the sample data//from w w w.ja  va  2 s  .  c  o  m
 *
 */
public void consume(String queue, String host, String host1, int port1, String host2, int port2, String host3,
        int port3, int executorSize) {
    TASK_QUEUE_NAME = queue;
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    final Connection connection;
    final Channel channel;
    final Consumer consumer;
    try {
        connection = factory.newConnection(EXECUTORS);
        channel = connection.createChannel();
        consumer = new EventDispatcher(channel, host1, port1, host2, port2, host3, port3, executorSize);
        boolean autoAck = true; // acknowledgment is covered below
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.siddhi.debs2017.input.rabbitmq.RabbitMQSampleDataPublisher.java

License:Open Source License

public static void start(String[] argv) {

    try {//  w  w w  .j a  v a  2 s  . c  o m
        if (argv.length > 0) {
            TASK_QUEUE_NAME = argv[0];
        }
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
        String data = "";
        BufferedReader reader = new BufferedReader(new FileReader("frmattedData_63.nt"));//molding_machine_100M.nt rdfSample.txt //Machine_59 //frmattedData.txt
        // read file line by line
        String line;
        Scanner scanner;
        int count = 0;
        while ((line = reader.readLine()) != null) {
            scanner = new Scanner(line);
            while (scanner.hasNext()) {
                String dataInLine = scanner.next();
                if (dataInLine.contains("----")) {
                    if (data.length() > 100) {
                        for (int i = 0; i < 5; i++) {
                            count++;
                            System.out.println(count);
                            String data1 = data.replace("Machine_59", "Machine_" + i).replace("_59_",
                                    "_" + i + "_");
                            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,
                                    data1.getBytes());
                        }
                    }
                    data = "";
                } else {
                    data += " " + dataInLine;
                    if (dataInLine.contains(".") && !dataInLine.contains("<")) {
                        data += "\n";
                    }
                }
            }
        }

        channel.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}