Example usage for com.rabbitmq.client ConnectionFactory setPort

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

Introduction

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

Prototype

public void setPort(int port) 

Source Link

Document

Set the target port.

Usage

From source file:com.hp.ov.sdk.messaging.core.RabbitMqClientConnectionFactory.java

License:Apache License

public static ConnectionFactory getConnectionFactory(final SSLContext sslContext, final RestParams params) {

    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(params.getHostname());
    factory.setPort(params.getAmqpPort());

    // Set Auth mechanism to "EXTERNAL" so that commonName of the client
    // certificate is mapped to AMQP user name. Hence, No need to set
    // userId/Password here.
    factory.setSaslConfig(DefaultSaslConfig.EXTERNAL);
    factory.useSslProtocol(sslContext);//www  . j  av  a  2 s.  co m
    factory.setAutomaticRecoveryEnabled(true);

    return factory;
}

From source file:com.insa.tp3g1.esbsimulator.view.MessageHandler.java

public static String sendConfig1(String who, String config) throws Exception {

    /* calling connectionFactory to create a custome connexion with
    * rabbitMQ server information./*from w  w w .java  2s.c om*/
    */
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("146.148.27.98");
    factory.setUsername("admin");
    factory.setPassword("adminadmin");
    factory.setPort(5672);
    System.out.println("connection ok");
    // establish the connection with RabbitMQ server using our factory.
    Connection connection = factory.newConnection();
    // System.out.println("connection ok1");
    // We're connected now, to the broker on the cloud machine.
    // If we wanted to connect to a broker on a the local machine we'd simply specify "localhost" as IP adresse.
    // creating a "configuration" direct channel/queue
    Channel channel = connection.createChannel();
    //  System.out.println("connection ok2");
    channel.exchangeDeclare(EXCHANGE_NAME, "direct");
    //  System.out.println("exchangeDeclare");
    // the message and the distination
    String forWho = who;
    String message = config;

    // publish the message
    channel.basicPublish(EXCHANGE_NAME, forWho, null, message.getBytes());
    //  System.out.println("basicPublish");
    // close the queue and the connexion
    channel.close();
    connection.close();
    return " [x] Sent '" + forWho + "':'" + message + "'";
}

From source file:com.insa.tp3g1.esbsimulator.view.MessageHandler.java

public static void logGetter() throws java.io.IOException {
    ConnectionFactory factory = new ConnectionFactory();
    LogHandler logHandler = new LogHandler();
    factory.setHost("146.148.27.98");
    factory.setUsername("admin");
    factory.setPassword("adminadmin");
    factory.setPort(5672);

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

    channel.exchangeDeclare(LOG_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, LOG_NAME, "");

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queueName, true, consumer);
    count = 0;// w w w  . j  av a  2 s.  co m
    boolean over = false;
    while (!over) {

        QueueingConsumer.Delivery delivery = null;
        try {
            delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            //System.out.println(" [x] Received '" + message + "'");
            synchronized (count) {
                count++;
            }
            logHandler.add(message);
            System.out.print(Thread.currentThread().getId() + " ");

        } catch (InterruptedException interruptedException) {
            System.out.println("finished");
            System.out.println(logHandler);

            System.out.println(logHandler);
            Thread.currentThread().interrupt();
            System.out.println(Thread.currentThread().getId() + " ");
            break;
            // Thread.currentThread().stop();
            // over = true;
        } catch (ShutdownSignalException shutdownSignalException) {
            System.out.println("finished");
            // Thread.currentThread().stop();
            over = true;
        } catch (ConsumerCancelledException consumerCancelledException) {
            System.out.println("finished");
            //  Thread.currentThread().stop();
            over = true;
        } catch (IllegalMonitorStateException e) {
            System.out.println("finished");
            // Thread.currentThread().stop();
            over = true;
        }

    }
    System.out.println("before close");
    channel.close();
    connection.close();
    System.out.println("finished handling");
    Result res = logHandler.fillInResultForm(logHandler.getTheLog());
    ResultHandler resH = new ResultHandler(res);
    resH.createResultFile("/home/alpha/alphaalpha.xml");
    final OverlaidBarChart demo = new OverlaidBarChart("Response Time Chart", res);
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    int lost = Integer.parseInt(res.getTotalResult().getLostRequests()) / logHandler.getNbRequests();
    PieChart demo1 = new PieChart("Messages", lost * 100);
    demo1.pack();
    demo1.setVisible(true);
    //System.out.println(logHandler);

}

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  w  w  w . j  av a2s  . 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);//from   w w  w. ja  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  www . ja v a 2 s  . co 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.aggregator.Aggregator.java

public void initializeResponseQueue() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(5672);
    factory.setUsername("test");
    factory.setPassword("test");

    responseConnection = factory.newConnection();
    responseChannel = responseConnection.createChannel();
    responseChannel.queueDeclare(QUEUE_BEST_NAME, false, false, false, null);
}

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

License:Apache License

/**
 * Creates {@link Connection} to AMQP system.
 *//*from  ww w.  j av  a 2  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  ww w.ja  v a 2s.  c  o  m
        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.paxxis.cornerstone.messaging.service.amqp.AMQPServiceBusConnector.java

License:Apache License

protected void initConnection() {
    try {//  w ww. j  a v  a 2s  .com
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        factory.setPort(port);
        factory.setAutomaticRecoveryEnabled(autoRecover);
        factory.setConnectionTimeout(timeout);
        factory.setNetworkRecoveryInterval(recoveryInterval);
        factory.setRequestedHeartbeat(heartbeat);
        factory.setTopologyRecoveryEnabled(autoTopologyRecover);
        factory.setExceptionHandler(exceptionHandler);

        connection = factory.newConnection();
        session = (AMQPSession) createSession();
    } catch (IOException e) {
        logger.error(e);
        throw new RuntimeException(e);
    }
}