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.opennaas.extensions.genericnetwork.capability.nclprovisioner.components.NetworkObservationsPusher.java

License:Apache License

private void sendStatistics(String topicName, String csvMessage)
        throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException {

    Connection conn = null;/*from   w w  w . j a  v  a  2s .  co  m*/
    Channel channel = null;

    try {

        log.debug("Establishing RabbitMQ connection with sla manager with URI: " + slaManagerUri);

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(slaManagerUri.getHost());

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

        channel.exchangeDeclare(topicName, "direct");

        log.debug("Publishing message in RabbitMQ channel.");

        channel.basicPublish(topicName, RABBIT_MQ_ROUTING_KEY,
                new AMQP.BasicProperties.Builder().contentType(OBSERVATIONS_CONTENT_TYPE).build(),
                csvMessage.getBytes());

        log.debug("Message successfully sent to SLA manager.");

    } finally {
        if (channel != null && channel.isOpen())
            channel.close();
        if (conn != null && conn.isOpen())
            conn.close();

        log.debug("Connection to RabbitMQ server closed.");

    }

}

From source file:org.pascani.dsl.lib.infrastructure.rabbitmq.EndPoint.java

License:Open Source License

/**
 * Creates a RabbitMQ end point; that is, a connection to the RabbitMQ
 * server. This is commonly used by all RabbitMQ consumers and producers.
 * /*from  ww  w  . ja v a2s .  c om*/
 * @param uri
 *            The RabbitMQ connection URI
 * 
 * @throws Exception
 *             If something bat happens. Check exceptions in
 *             {@link ConnectionFactory#setUri(String)},
 *             {@link ConnectionFactory#newConnection()}, and
 *             {@link Connection#createChannel()}
 */
public EndPoint(final String uri) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setAutomaticRecoveryEnabled(true);
    factory.setUri(uri);
    this.connection = factory.newConnection();
    this.channel = this.connection.createChannel();
}

From source file:org.pushtrigger.mule.agent.CreateQueueAgent.java

License:Apache License

@Override
public void start() throws MuleException {
    System.out.println("Create Queue Agent is running...");

    Connection connection = null;
    Channel channel = null;//from   w w w  . j  a  va 2s  . c  om
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World! I am creating a queue...";

        Map<String, Object> props = new HashMap<String, Object>();
        props.put("path", "queue");
        props.put("branch", "create");

        AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
        AMQP.BasicProperties basicProps = bob.headers(props).build();

        channel.basicPublish("", QUEUE_NAME, basicProps, message.getBytes());
        System.out.println("Agent has created the queue...");
    } catch (IOException e) {
        System.out.println("Something wrong " + e);
    } finally {
        try {
            if (channel != null)
                channel.close();
        } catch (IOException e) {
        }
        try {
            if (connection != null)
                connection.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.s23m.cell.repository.client.connector.RepositoryClientConnector.java

License:Mozilla Public License

private void initClient() {
    final ConnectionFactory cfconn = new ConnectionFactory();
    cfconn.setHost(ConfigValues.getString("RepositoryClientServer.HOST_NAME"));
    cfconn.setPort(Integer.parseInt(ConfigValues.getString("RepositoryClientServer.PORT")));
    cfconn.setVirtualHost(ConfigValues.getString("RepositoryClientServer.VHOST_NAME"));
    cfconn.setUsername(ConfigValues.getString("RepositoryClientServer.USER_NAME"));
    cfconn.setPassword(ConfigValues.getString("RepositoryClientServer.PW"));
    Connection conn;//w w  w . j a va  2s  .c o m
    try {
        conn = cfconn.newConnection();
        final Channel ch = conn.createChannel();
        clientService = new RpcClient(ch, "", ConfigValues.getString("RepositoryClientServer.QUEUE"));
    } catch (final IOException ex) {
        throw new IllegalStateException("Client set up failed", ex);
    }
}

From source file:org.s23m.cell.repository.client.server.RepositoryClientServer.java

License:Mozilla Public License

private void setupConnection() throws IOException {
    final ConnectionFactory connFactory = new ConnectionFactory();
    connFactory.setHost(LOCALHOST);/*from ww w  .j  a  va2s. c  o  m*/
    connFactory.setPort(PORT);
    connFactory.setVirtualHost(ConfigValues.getString("RepositoryClientServer.VHOST_NAME"));
    connFactory.setUsername(ConfigValues.getString("RepositoryClientServer.USER_NAME"));
    connFactory.setPassword(ConfigValues.getString("RepositoryClientServer.PW"));
    final Connection conn = connFactory.newConnection();
    ch = conn.createChannel();
    ch.queueDeclare(QUEUE_NAME, false, false, false, null);
}

From source file:org.sdw.scheduler.QueueProcessor.java

License:Apache License

/**
 * Implementation of the Runnable interface's run method
 * Polls for messages in the shared queue and logs the results on arrival of message
 *//*from  w  w w .  j ava 2 s. co m*/
@Override
public void run() {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri(System.getenv("CLOUDAMQP_URL"));
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String queueName = "work-queue-1";
        Map<String, Object> params = new HashMap<>();
        params.put("x-ha-policy", "all");
        channel.queueDeclare(queueName, true, false, false, params);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, false, consumer);

        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            if (delivery != null) {
                String msg = new String(delivery.getBody(), "UTF-8");
                LOG.info("Message Received: " + msg);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    } catch (IOException | KeyManagementException | NoSuchAlgorithmException | URISyntaxException
            | ShutdownSignalException | ConsumerCancelledException | InterruptedException ex) {
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:org.seaduck.murrelet.impl.rabbitmq.AsyncReceiver.java

License:Apache License

public AsyncReceiver(String busName, String host) {
    super(busName);

    this.logger = LoggerFactory.getLogger(AsyncReceiver.class);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);/*  www . jav  a  2 s. c o  m*/

    try {
        this.connection = factory.newConnection();
        this.channel = connection.createChannel();
        channel.queueDeclare(busName, false, false, false, null);
    } catch (IOException e) {
        this.logger.error("Error in opening connection.");
        e.printStackTrace();
    }

    this.logger.info("Bus established: " + super.getBusName());
}

From source file:org.seaduck.murrelet.impl.rabbitmq.AsyncSender.java

License:Apache License

public AsyncSender(String busName, String host) {
    super(busName);

    this.logger = LoggerFactory.getLogger(AsyncSender.class);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);//from w  ww. j  av a2  s .co  m
    try {
        this.connection = factory.newConnection();
        this.channel = connection.createChannel();
    } catch (IOException e) {
        this.logger.error("Error in opening connection.");
        e.printStackTrace();
    }

    this.logger.info("Bus established: " + super.getBusName());
}

From source file:org.smartdeveloperhub.curator.connector.BrokerController.java

License:Apache License

void connect() throws ControllerException {
    this.write.lock();
    try {/*from  w  ww  .ja  v a 2s. c o m*/
        if (this.connected) {
            return;
        }
        final ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(this.broker.host());
        factory.setPort(this.broker.port());
        factory.setVirtualHost(this.broker.virtualHost());
        factory.setThreadFactory(brokerThreadFactory());
        factory.setExceptionHandler(new BrokerControllerExceptionHandler(this));
        this.connection = factory.newConnection();
        createChannel();
    } catch (IOException | TimeoutException e) {
        this.connected = false;
        final String message = String.format("Could not connect to broker at %s:%s using virtual host %s",
                this.broker.host(), this.broker.port(), this.broker.virtualHost());
        throw new ControllerException(message, e);
    } finally {
        this.write.unlock();
    }
}

From source file:org.smartdeveloperhub.harvesters.it.notification.ConnectionManager.java

License:Apache License

void connect() throws ControllerException {
    this.lock.lock();
    try {//from   w  w  w.  ja v a  2 s. c  o m
        if (connected()) {
            return;
        }
        final ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(this.brokerHost);
        factory.setPort(this.brokerPort);
        factory.setVirtualHost(this.virtualHost);
        factory.setThreadFactory(brokerThreadFactory());
        factory.setExceptionHandler(new ConnectionManagerExceptionHandler(this));
        this.connection = factory.newConnection();
        createChannel();
    } catch (IOException | TimeoutException e) {
        final String message = String.format("Could not connect to broker at %s:%s using virtual host %s",
                this.brokerHost, this.brokerPort, this.virtualHost);
        throw new ControllerException(this.brokerHost, this.brokerPort, this.virtualHost, message, e);
    } finally {
        this.lock.unlock();
    }
}