Example usage for com.rabbitmq.client ConnectionFactory newConnection

List of usage examples for com.rabbitmq.client ConnectionFactory newConnection

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory newConnection.

Prototype

public Connection newConnection() throws IOException, TimeoutException 

Source Link

Document

Create a new broker connection.

Usage

From source file:org.mule.transport.amqp.harness.TestConnectionManager.java

License:Open Source License

public Connection getConnection() throws IOException {
    if (connection != null && connection.isOpen()) {
        return connection;
    }/*from www  . ja v  a 2s.c om*/

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(System.getProperty("amqpHost"));
    factory.setPort(Integer.valueOf(System.getProperty("amqpPort")));
    factory.setUsername(System.getProperty("amqpUserName"));
    factory.setPassword(System.getProperty("amqpPassword"));
    factory.setVirtualHost(System.getProperty("amqpVirtualHost"));
    connection = factory.newConnection();

    return connection;
}

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  www  . ja  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 a2  s .c o 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.nuxeo.rabbit.RabbitMQEventForwarder.java

License:Apache License

protected void produceRabbitMQMessage(Event event) throws RabbitMQException, IOException, TimeoutException {
    DocumentEventContext context = (DocumentEventContext) event.getContext();
    DocumentModel doc = context.getSourceDocument();
    if (!doc.hasSchema("file")) {
        return;/*from   w  w w .  ja  v  a2 s  .  co m*/
    }
    if (connection == null) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Framework.getProperty(RABBITMQ_URL, "localhost"));
        connection = factory.newConnection();
    }
    if (channel == null) {
        channel = connection.createChannel();
    }
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    channel.basicPublish("", QUEUE_NAME, null, message(event, doc).getBytes("UTF-8"));
    channel.close();
    connection.close();
}

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

License:Open Source License

private synchronized CachedConnection getConnection(AMQPConnectionParameters connectionParameters)
        throws IOException {
    String key = connectionParameters.getKey();
    CachedConnection connection = cachedConnections.get(key);
    if (connection == null) {
        logger.debug(String.format("creating a new connection %s", key));

        ConnectionFactory factory = new ConnectionFactory();
        if (socketFactory != null) {
            factory.setSocketFactory(socketFactory);
        }/*w ww .j  av  a 2 s  .  c o  m*/
        factory.setHost(connectionParameters.getHost());
        factory.setPort(connectionParameters.getPort());
        factory.setUsername(connectionParameters.getUsername());
        factory.setPassword(connectionParameters.getPassword());
        factory.setVirtualHost(connectionParameters.getVhost());
        Connection c = factory.newConnection();
        c.addShutdownListener(new AMQPShutDownListener(c.toString()));

        connection = new CachedConnection(this, c);
        cachedConnections.put(key, connection);
    }

    return connection;
}

From source file:org.openbaton.common.vnfm_sdk.amqp.AbstractVnfmSpringAmqp.java

License:Apache License

@Override
protected void unregister() {
    try {//  w  w  w  . ja  va 2s .  c  om
        ((VnfmSpringHelperRabbit) vnfmHelper).sendMessageToQueue(RabbitConfiguration.queueName_vnfmUnregister,
                vnfmManagerEndpoint);
    } catch (IllegalStateException e) {
        log.warn("Got exception while unregistering trying to do it manually");
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost(rabbitHost);
        Connection connection = null;
        try {
            connection = factory.newConnection();

            Channel channel = connection.createChannel();

            String message = gson.toJson(vnfmManagerEndpoint);
            channel.basicPublish("openbaton-exchange", RabbitConfiguration.queueName_vnfmUnregister,
                    MessageProperties.TEXT_PLAIN, message.getBytes("UTF-8"));
            log.debug("Sent '" + message + "'");

            channel.close();
            connection.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:org.openbaton.plugin.PluginListener.java

License:Apache License

private void initRabbitMQ() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(brokerIp);/*from   w  w w.  ja v  a2  s .com*/
    factory.setPort(brokerPort);
    factory.setPassword(password);
    factory.setUsername(username);
    factory.setVirtualHost(virtualHost);

    connection = factory.newConnection();
    channel = connection.createChannel();

    channel.queueDeclare(pluginId, this.durable, false, true, null);
    channel.queueBind(pluginId, exchange, pluginId);

    channel.basicQos(1);
}

From source file:org.openbaton.plugin.utils.PluginCaller.java

License:Apache License

public PluginCaller(String pluginId, String brokerIp, String username, String password, int port,
        int managementPort) throws IOException, TimeoutException, NotFoundException {
    this.pluginId = getFullPluginId(pluginId, brokerIp, username, password, managementPort);
    this.managementPort = managementPort;
    this.brokerIp = brokerIp;
    this.username = username;
    this.password = password;
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(brokerIp);//w ww.  j ava  2s  .c  o m
    if (username != null)
        factory.setUsername(username);
    else
        factory.setUsername("admin");
    if (password != null)
        factory.setPassword(password);
    else
        factory.setPassword("openbaton");
    if (port > 1024)
        factory.setPort(port);
    else
        factory.setPort(5672);
    connection = factory.newConnection();

    //        replyQueueName = channel.queueDeclare().getQueue();
    //        channel.queueBind(replyQueueName,exchange,replyQueueName);
    //        consumer = new QueueingConsumer(channel);
    //        channel.basicConsume(replyQueueName, true, consumer);
}

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  w w w. j ava  2  s .  co 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;
    }
}

From source file:org.openmrs.module.amqpmodule.utils.impl.PublisherServiceImpl.java

License:Open Source License

@Override
public boolean PublisherCreateConnection() throws java.io.IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.43.123");
    factory.setPort(5672);/*w  w  w .  j  a v a 2 s  .  c om*/
    factory.setUsername("chs");
    factory.setPassword("chs123");
    Connection connection = null;
    try {
        connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true);

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

        channel.close();

    } catch (TimeoutException e) {
        System.out.println("Connection Timed out");
        e.printStackTrace();
    }

    connection.close();

    return true;
}