Example usage for com.rabbitmq.client ConnectionFactory setVirtualHost

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

Introduction

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

Prototype

public void setVirtualHost(String virtualHost) 

Source Link

Document

Set the virtual host.

Usage

From source file:com.jbrisbin.vcloud.mbean.CloudInvokerListener.java

License:Apache License

protected Connection getConnection() throws IOException {
    if (null == connection) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(mqHost);/*from   www  .j  a  v a2  s. co  m*/
        factory.setPort(mqPort);
        factory.setUsername(mqUser);
        factory.setPassword(mqPassword);
        factory.setVirtualHost(mqVirtualHost);
        if (DEBUG) {
            log.debug("Connecting to RabbitMQ server...");
        }
        connection = factory.newConnection();
    }
    return connection;
}

From source file:com.jbrisbin.vcloud.session.CloudStore.java

License:Apache License

protected synchronized Connection getMqConnection() throws IOException {
    if (null == mqConnection) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(mqHost);//w w w  .  j a  v a  2 s. c o m
        factory.setPort(mqPort);
        factory.setUsername(mqUser);
        factory.setPassword(mqPassword);
        factory.setVirtualHost(mqVirtualHost);
        factory.setRequestedHeartbeat(10);
        mqConnection = factory.newConnection();
    }
    return mqConnection;
}

From source file:com.modeln.batam.connector.Connector.java

License:Open Source License

/**
 * Begin a connection using parameters. If some or all parameters are null, the connector will use values loaded either from property files or System properties.
 * //from  w  w  w  .  jav a2 s. c  o  m
 * If no system property have been defined using {@see System#setProperty(String, String) setProperty}, This method will use configuration located in the batam.properties file by default.
 * Non null parameters will override default property file values.
 * 
 * To use external property file properties, call {@see com.modeln.batam.connector.Connector#loadProperties(String) loadProperties} function specifying the external property file location before to call this method with null parameters. 
 * 
 * If System properties are defined, they will override values defined in property files (default or external). Non null parameters will also override System properties.
 * 
 * @param host : message broker host configuration.
 * @param username : message broker user name configuration.
 * @param password : message broker password configuration. 
 * @param port : message broker port configuration.
 * @param vhost : message broker VHost configuration.
 * @param queue : message broker queue the connector publish data to.
 * @param publisher : when set to 'off', it prints messages in your console (stdout) instead of publishing them to the message broker. 
 * 
 * @throws IOException
 */
@RetryOnFailure(attempts = RETRY_ON_FAILURE_ATTEMPTS, delay = RETRY_ON_FAILURE_DELAY, unit = TimeUnit.SECONDS)
public void beginConnection(String host, String username, String password, Integer port, String vhost,
        String queue, String publisher) throws IOException {
    ConfigHelper.loadProperties(null);

    if (host == null && this.host == null) {
        this.host = ConfigHelper.HOST;
    } else if (host != null) {
        this.host = host;
    }

    if (username == null && this.username == null) {
        this.username = ConfigHelper.USER;
    } else if (username != null) {
        this.username = username;
    }

    if (password == null && this.password == null) {
        this.password = ConfigHelper.PASSWORD;
    } else if (password != null) {
        this.password = password;
    }

    if (port == null && this.port == null) {
        this.port = ConfigHelper.PORT;
    } else if (port != null) {
        this.port = port;
    }

    if (vhost == null && this.vhost == null) {
        this.vhost = ConfigHelper.VHOST;
    } else if (vhost != null) {
        this.vhost = vhost;
    }

    if (queue == null && this.queue == null) {
        this.queue = ConfigHelper.QUEUE;
    } else if (queue != null) {
        this.queue = queue;
    }

    if (publisher == null) {
        publisher = ConfigHelper.PUBLISHER;
    }
    if ("on".equals(publisher) || "true".equals(publisher)) {
        this.publish = true;
    } else {
        this.publish = false;
        return;
    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.host);
    factory.setPort(this.port);
    factory.setUsername(this.username);
    factory.setPassword(this.password);
    factory.setVirtualHost(this.vhost);
    this.connection = factory.newConnection();
    this.channel = this.connection.createChannel();
    this.channel.queueDeclare(this.queue, false, false, false, null);
}

From source file:com.mycompany.dreamteamxml.DreamTeamXML.java

public void sender(double interestRate, String ssn) throws IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setVirtualHost("student");
    factory.setUsername("Dreamteam");
    factory.setPassword("bastian");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(SENDING_QUEUE_NAME, false, false, false, null);

    String message = "<LoanResponse>" + "<interestRate>" + interestRate + "</interestRate>" + "<ssn>" + ssn
            + "</ssn>" + "</LoanResponse>";

    System.out.println("Message created as soap");

    channel.basicPublish("", SENDING_QUEUE_NAME, null, message.getBytes());

    System.out.println(" [x] Sent '" + message + "'");

    channel.close();//from  w ww  . java 2 s  . co m
    connection.close();

}

From source file:com.nifi.processors.amqp.AbstractAMQPProcessor.java

License:Apache License

/**
 * Creates {@link Connection} to AMQP system.
 *///from   w  w w .  j  a  v  a2  s .  co m
private Connection createConnection(ProcessContext context) {
    ConnectionFactory cf = new ConnectionFactory();
    cf.setHost(context.getProperty(HOST).getValue());
    cf.setPort(Integer.parseInt(context.getProperty(PORT).getValue()));
    cf.setUsername(context.getProperty(USER).getValue());
    cf.setPassword(context.getProperty(PASSWORD).getValue());
    String vHost = context.getProperty(V_HOST).getValue();
    if (vHost != null) {
        cf.setVirtualHost(vHost);
    }

    // handles TLS/SSL aspects
    final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE)
            .asControllerService(SSLContextService.class);
    final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
    final SSLContext sslContext;

    if (sslService != null) {
        final SSLContextService.ClientAuth clientAuth;
        if (StringUtils.isBlank(rawClientAuth)) {
            clientAuth = SSLContextService.ClientAuth.REQUIRED;
        } else {
            //                try {
            clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
            //                } catch (final IllegalArgumentException iae) {
            //                    throw new ProviderCreationException(String.format("Unrecognized client auth '%s'. Possible values are [%s]",
            //                            rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
            //                }
        }
        sslContext = sslService.createSSLContext(clientAuth);
    } else {
        sslContext = null;
    }

    // check if the ssl context is set and add it to the factory if so
    if (sslContext != null) {
        cf.useSslProtocol(sslContext);
    }

    try {
        Connection connection = cf.newConnection();
        return connection;
    } catch (Exception e) {
        throw new IllegalStateException("Failed to establish connection with AMQP Broker: " + cf.toString(), e);
    }
}

From source file:com.nxttxn.vramel.components.rabbitMQ.RabbitMQEndpoint.java

License:Apache License

private ConnectionFactory getOrCreateConnectionFactory() {
    if (connectionFactory == null) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(getUsername());
        factory.setPassword(getPassword());
        factory.setVirtualHost(getVhost());
        factory.setHost(getHostname());/*from  w w  w  .  j av  a 2  s .c om*/
        factory.setPort(getPortNumber());
        if (getClientProperties() != null) {
            factory.setClientProperties(getClientProperties());
        }
        factory.setConnectionTimeout(getConnectionTimeout());
        factory.setRequestedChannelMax(getRequestedChannelMax());
        factory.setRequestedFrameMax(getRequestedFrameMax());
        factory.setRequestedHeartbeat(getRequestedHeartbeat());
        if (getSslProtocol() != null) {
            try {
                if (getSslProtocol().equals("true")) {
                    factory.useSslProtocol();
                } else if (getTrustManager() == null) {
                    factory.useSslProtocol(getSslProtocol());
                } else {
                    factory.useSslProtocol(getSslProtocol(), getTrustManager());
                }
            } catch (NoSuchAlgorithmException | KeyManagementException e) {
                throw new IllegalArgumentException("Invalid sslProtocol " + sslProtocol, e);
            }
        }
        if (getAutomaticRecoveryEnabled() != null) {
            factory.setAutomaticRecoveryEnabled(getAutomaticRecoveryEnabled());
        }
        if (getNetworkRecoveryInterval() != null) {
            factory.setNetworkRecoveryInterval(getNetworkRecoveryInterval());
        }
        if (getTopologyRecoveryEnabled() != null) {
            factory.setTopologyRecoveryEnabled(getTopologyRecoveryEnabled());
        }
        connectionFactory = factory;
    }
    return connectionFactory;
}

From source file:com.saasovation.common.port.adapter.messaging.rabbitmq.BrokerChannel.java

License:Apache License

/**
 * Answers a new ConnectionFactory configured with aConnectionSettings.
 * @param aConnectionSettings the ConnectionFactory
 * @return ConnectionFactory//w w w .java2  s.c om
 */
protected ConnectionFactory configureConnectionFactoryUsing(ConnectionSettings aConnectionSettings) {

    ConnectionFactory factory = new ConnectionFactory();

    factory.setHost(aConnectionSettings.hostName());

    if (aConnectionSettings.hasPort()) {
        factory.setPort(aConnectionSettings.port());
    }

    factory.setVirtualHost(aConnectionSettings.virtualHost());

    if (aConnectionSettings.hasUserCredentials()) {
        factory.setUsername(aConnectionSettings.username());
        factory.setPassword(aConnectionSettings.password());
    }

    return factory;
}

From source file:com.simple.sftpfetch.publish.RabbitClient.java

License:Apache License

/**
 * Initialize the RabbitClient, establish a connection and declare the exchange
 *
 * @param factory the RabbitMQ ConnectionFactory
 * @param connectionInfo a bean containing the necessary connection information
 *
 * @throws NoSuchAlgorithmException//from   w ww . jav a 2  s  .  c  o m
 * @throws KeyManagementException
 * @throws URISyntaxException
 * @throws IOException
 */
public RabbitClient(ConnectionFactory factory, RabbitConnectionInfo connectionInfo)
        throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException {
    factory.setHost(connectionInfo.getHostname());
    factory.setPort(connectionInfo.getPort());
    factory.setUsername(connectionInfo.getUsername());
    factory.setPassword(connectionInfo.getPassword());
    factory.setVirtualHost(connectionInfo.getVhost());
    factory.setConnectionTimeout(connectionInfo.getTimeout());
    Connection conn = factory.newConnection();
    exchange = connectionInfo.getExchange();
    channel = conn.createChannel();
    channel.exchangeDeclare(exchange, EXCHANGE_TYPE, true);
    this.amqpProperties = new AMQP.BasicProperties.Builder().contentType(CONTENT_TYPE).deliveryMode(2).build();
}

From source file:com.siva.rabbitmq.AmqpMsgPublisher.java

public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setUsername("admin");
    factory.setPassword("welcome01");
    factory.setPort(5672);//  w ww.  j a  va2s. c  o m
    factory.setVirtualHost("/admin_vhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

    String routingKey = "mqtt_topic.iot.admin_vhost";
    String message = "Test Message from IoT";

    channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
    System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

    connection.close();
}

From source file:com.trivago.mail.pigeon.queue.ConnectionPool.java

License:Apache License

public static Connection getConnection() {
    if (connection == null) {
        ConnectionFactory factory = new ConnectionFactory();
        Configuration configuration = settings.getConfiguration();

        factory.setUsername(configuration.getString("rabbit.username"));
        factory.setPassword(configuration.getString("rabbit.password"));
        factory.setVirtualHost(configuration.getString("rabbit.vhost"));
        factory.setHost(configuration.getString("rabbit.hostname"));
        factory.setPort(configuration.getInt("rabbit.port"));

        try {//from   w  w w  .j  a v a2  s .co  m
            connection = factory.newConnection();
        } catch (IOException e) {
            log.error(e);
            throw new RuntimeException(e);
        }
    }
    return connection;
}