Example usage for com.rabbitmq.client ConnectionFactory setUri

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

Introduction

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

Prototype

public void setUri(String uriString)
        throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException 

Source Link

Document

Convenience method for setting the fields in an AMQP URI: host, port, username, password and virtual host.

Usage

From source file:com.googlecode.jmxtrans.model.output.RabbitMQWriter.java

License:Open Source License

private Channel createChannel() throws Exception {
    ConnectionFactory cf = new ConnectionFactory();
    cf.setUri(this.uri);
    Connection connection = cf.newConnection();
    return connection.createChannel();
}

From source file:com.googlesource.gerrit.plugins.rabbitmq.AMQPSession.java

License:Apache License

public void connect() {
    LOGGER.info("Connect to {}...", properties.getString(Keys.AMQP_URI));
    ConnectionFactory factory = new ConnectionFactory();
    try {// w  w  w .ja v  a2  s  . c o  m
        if (StringUtils.isNotEmpty(properties.getString(Keys.AMQP_URI))) {
            factory.setUri(properties.getString(Keys.AMQP_URI));
            if (StringUtils.isNotEmpty(properties.getString(Keys.AMQP_USERNAME))) {
                factory.setUsername(properties.getString(Keys.AMQP_USERNAME));
            }
            if (StringUtils.isNotEmpty(properties.getString(Keys.AMQP_PASSWORD))) {
                factory.setPassword(properties.getString(Keys.AMQP_PASSWORD));
            }
            connection = factory.newConnection();
            connection.addShutdownListener(this);
            LOGGER.info("Connection established.");
        }
        //TODO: Consume review
        // setupConsumer();
    } catch (URISyntaxException ex) {
        LOGGER.error("URI syntax error: {}", properties.getString(Keys.AMQP_URI));
    } catch (IOException ex) {
        LOGGER.error("Connection cannot be opened.");
    } catch (Exception ex) {
        LOGGER.warn("Connection has something error. it will be disposed.", ex);
    }
}

From source file:com.googlesource.gerrit.plugins.rabbitmq.session.type.AMQPSession.java

License:Apache License

@Override
public void connect() {
    if (connection != null && connection.isOpen()) {
        LOGGER.info(MSG("Already connected."));
        return;/*w  ww .j  a  v  a  2  s . c o m*/
    }
    AMQP amqp = properties.getSection(AMQP.class);
    LOGGER.info(MSG("Connect to {}..."), amqp.uri);
    ConnectionFactory factory = new ConnectionFactory();
    try {
        if (StringUtils.isNotEmpty(amqp.uri)) {
            factory.setUri(amqp.uri);
            if (StringUtils.isNotEmpty(amqp.username)) {
                factory.setUsername(amqp.username);
            }
            Gerrit gerrit = properties.getSection(Gerrit.class);
            String securePassword = gerrit.getAMQPUserPassword(amqp.username);
            if (StringUtils.isNotEmpty(securePassword)) {
                factory.setPassword(securePassword);
            } else if (StringUtils.isNotEmpty(amqp.password)) {
                factory.setPassword(amqp.password);
            }
            connection = factory.newConnection();
            connection.addShutdownListener(connectionListener);
            LOGGER.info(MSG("Connection established."));
        }
    } catch (URISyntaxException ex) {
        LOGGER.error(MSG("URI syntax error: {}"), amqp.uri);
    } catch (IOException ex) {
        LOGGER.error(MSG("Connection cannot be opened."), ex);
    } catch (KeyManagementException | NoSuchAlgorithmException ex) {
        LOGGER.error(MSG("Security error when opening connection."), ex);
    }
}

From source file:com.nesscomputing.amqp.AmqpFactoryProvider.java

License:Apache License

@Override
public ConnectionFactory get() {
    final URI amqpUri = amqpConfig.getAmqpConnectionUrl();

    try {/*from   w w w. j ava 2  s.  c  o  m*/
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(amqpUri);
        return connectionFactory;
    } catch (Exception e) {
        Throwables.propagateIfPossible(e);
    }
    return null;
}

From source file:com.sitewhere.connectors.rabbitmq.RabbitMqOutboundEventProcessor.java

License:Open Source License

@Override
public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException {
    super.start(monitor);

    // Start multicaster if configured.
    if (multicaster != null) {
        startNestedComponent(multicaster, monitor, true);
    }//from  w w w . java 2  s.co m

    // Start route builder if configured.
    if (routeBuilder != null) {
        startNestedComponent(routeBuilder, monitor, true);
    }
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri(getConnectionUri());
        this.connection = factory.newConnection();
        this.channel = connection.createChannel();
        this.exchange = getTenantEngine().getTenant().getId() + DEFAULT_EXCHANGE_SUFFIX;
        channel.exchangeDeclare(exchange, "topic");
        LOGGER.info("RabbitMQ outbound processor connected to: " + getConnectionUri());
    } catch (Exception e) {
        throw new SiteWhereException("Unable to start RabbitMQ event processor.", e);
    }
}

From source file:com.sitewhere.protobuf.test.ActiveMQTests.java

License:Open Source License

@Test
public void doRabbitMQTest() throws Exception {
    String exchangeName = "sitewhere";
    String queueName = "SITEWHERE.IN";
    String routingKey = "sitewhere";

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri("amqp://localhost:5672/SITEWHERE.IN");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(exchangeName, "direct", true);
    channel.queueDeclare(queueName, true, false, false, null);
    channel.queueBind(queueName, exchangeName, routingKey);

    byte[] messageBodyBytes = generateEncodedMeasurementsMessage();
    channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes);

    channel.close();//from   www. j  a v  a2 s  . c o  m
    connection.close();
}

From source file:com.sitewhere.rabbitmq.RabbitMqInboundEventReceiver.java

License:Open Source License

@Override
public void start() throws SiteWhereException {
    executors = Executors.newFixedThreadPool(getNumConsumers());
    try {/*from   w  w  w .  j a  v  a  2s.c om*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri(getConnectionUri());
        this.connection = factory.newConnection(executors);
        this.channel = connection.createChannel();

        LOGGER.info("RabbitMQ receiver connected to: " + getConnectionUri());

        channel.queueDeclare(getQueueName(), isDurable(), false, false, null);

        LOGGER.info("RabbitMQ receiver using " + (isDurable() ? "durable " : "") + "queue: " + getQueueName());

        // Add consumer callback for channel.
        Consumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                EventProcessingLogic.processRawPayload(RabbitMqInboundEventReceiver.this, body, null);
            }
        };
        channel.basicConsume(getQueueName(), true, consumer);
    } catch (Exception e) {
        throw new SiteWhereException("Unable to start RabbitMQ event receiver.", e);
    }
}

From source file:com.sitewhere.rabbitmq.RabbitMqOutboundEventProcessor.java

License:Open Source License

@Override
public void start() throws SiteWhereException {
    super.start();
    try {/*from  w ww  .ja  v a 2  s.c  o m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri(getConnectionUri());
        this.connection = factory.newConnection();
        this.channel = connection.createChannel();
        this.exchange = getTenant().getId() + DEFAULT_EXCHANGE_SUFFIX;
        channel.exchangeDeclare(exchange, "topic");
        LOGGER.info("RabbitMQ outbound processor connected to: " + getConnectionUri());
    } catch (Exception e) {
        throw new SiteWhereException("Unable to start RabbitMQ event processor.", e);
    }
}

From source file:com.sitewhere.sources.ActiveMQTests.java

License:Open Source License

@Test
public void doRabbitMQTest() throws Exception {
    String exchangeName = "sitewhere";
    String queueName = "sitewhere.input";
    String routingKey = "sitewhere";

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri("amqp://localhost:5672");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(exchangeName, "direct", true);
    channel.queueDeclare(queueName, true, false, false, null);
    channel.queueBind(queueName, exchangeName, routingKey);

    byte[] messageBodyBytes = EventsHelper.generateEncodedMeasurementsMessage(HARDWARE_ID);
    channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes);

    channel.close();/*from w w w.  ja  va2  s .c o  m*/
    connection.close();
}

From source file:com.sonymobile.jenkins.plugins.mq.mqnotifier.MQNotifierConfig.java

License:Open Source License

/**
 * Tests connection to the server URI.//from  ww  w  . j a va 2 s.  c  om
 *
 * @param uri the URI.
 * @param name the user name.
 * @param pw the user password.
 * @return FormValidation object that indicates ok or error.
 * @throws javax.servlet.ServletException Exception for servlet.
 */
public FormValidation doTestConnection(@QueryParameter(SERVER_URI) final String uri,
        @QueryParameter(USERNAME) final String name, @QueryParameter(PASSWORD) final Secret pw)
        throws ServletException {
    UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS);
    FormValidation result = FormValidation.ok();
    if (urlValidator.isValid(uri)) {
        try {
            ConnectionFactory conn = new ConnectionFactory();
            conn.setUri(uri);
            if (StringUtils.isNotEmpty(name)) {
                conn.setUsername(name);
                if (StringUtils.isNotEmpty(Secret.toString(pw))) {
                    conn.setPassword(Secret.toString(pw));
                }
            }
            conn.newConnection();
        } catch (URISyntaxException e) {
            result = FormValidation.error("Invalid Uri");
        } catch (PossibleAuthenticationFailureException e) {
            result = FormValidation.error("Authentication Failure");
        } catch (Exception e) {
            result = FormValidation.error(e.getMessage());
        }
    } else {
        result = FormValidation.error("Invalid Uri");

    }
    return result;
}