Example usage for com.rabbitmq.client ConnectionFactory setHost

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

Introduction

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

Prototype

public void setHost(String host) 

Source Link

Usage

From source file:com.mycompany.receiptlist.ReceiptList.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("Dreamteam");
    factory.setPassword("bastian");
    Connection connection = factory.newConnection();
    Channel listeningChannel = connection.createChannel();
    final Channel sendingChannel = connection.createChannel();

    listeningChannel.queueDeclare(QUEUE_NAME, false, false, false, null);
    sendingChannel.exchangeDeclare(EXCHANGE_NAME, "direct");
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(listeningChannel) {
        @Override/*from  w  w  w .  j  av a 2 s .c  om*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");

            String[] arr = message.split(",");

            for (int i = 0; i < arr.length; i++) {
                switch (arr[i]) {
                case "DreamTeamXMLQueue":
                    sendingChannel.basicPublish(EXCHANGE_NAME, "DreamTeamBankXML", null, message.getBytes());
                    break;
                case "DreamTeamJSONQueue":
                    sendingChannel.basicPublish(EXCHANGE_NAME, "DreamTeamBankJSON", null, message.getBytes());
                    break;
                case "cphbusiness.bankXML":
                    sendingChannel.basicPublish(EXCHANGE_NAME, "CphBusinessXML", null, message.getBytes());
                    break;
                case "cphbusiness.bankJSON":
                    sendingChannel.basicPublish(EXCHANGE_NAME, "CphBusinessJSON", null, message.getBytes());
                    break;

                }
            }
        }
    };

    listeningChannel.basicConsume(QUEUE_NAME, true, consumer);
}

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

License:Apache License

/**
 * Creates {@link Connection} to AMQP system.
 *///from   w  w  w  .j av  a  2  s .c  om
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());
        factory.setPort(getPortNumber());
        if (getClientProperties() != null) {
            factory.setClientProperties(getClientProperties());
        }/*from   w  w  w .ja v a2 s . c  o m*/
        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 {//from   w w  w  .  j a  v a2s  . c  o  m
        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);
    }
}

From source file:com.pcs.test.amqp.Consumer.java

License:Open Source License

public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException,
        ConsumerCancelledException, InterruptedException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("pcss-hdop04");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println("listen for messages");

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String msg = new String(delivery.getBody(), "UTF-8");
        System.out.println("Msg received " + msg);
    }/*  w w w .jav a 2 s. co m*/
}

From source file:com.pcs.test.amqp.Publisher.java

License:Open Source License

public static void main(String[] args) throws IOException, TimeoutException {
    System.out.println("starting");
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("pcss-hdop04");
    factory.setAutomaticRecoveryEnabled(true);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "first message , hello world";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    System.out.println("message sent");

    channel.close();//w w w. ja  v  a2 s.  c om
    connection.close();
}

From source file:com.project.finalproject.Recv.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override/* w ww . j a  v  a  2 s . c  om*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");

            System.out.println(" [x] Received '" + message + "'");
        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);
}

From source file:com.project.finalproject.Send.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    try {/*from w ww  .  j av  a 2s .c  om*/
        InputStream is = new FileInputStream("hr.json");
        String jsontxt = IOUtils.toString(is);
        JSONObject jsonObject = new JSONObject(jsontxt);
        JSONArray stream = jsonObject.getJSONArray("stream");
        for (int i = 0; i < stream.length(); i++) {
            String message = stream.getJSONObject(i).getString("value");
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
            TimeUnit.SECONDS.sleep(1);
        }
    } catch (FileNotFoundException fe) {
        fe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    channel.close();
    connection.close();
}

From source file:com.qt.core.util.MqConnectionUtil.java

License:Open Source License

private Connection createPooledConnection(MqConn conn) throws IOException {
    ConnectionFactory connectionFactory = new ConnectionFactory(); // 
    connectionFactory.setHost(conn.getHost());
    connectionFactory.setPort(conn.getPort());
    connectionFactory.setUsername(conn.getAccount());
    connectionFactory.setPassword(conn.getPassword());
    Connection connection = connectionFactory.newConnection();
    return connection;
}

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/*from   w  w w.java 2 s  .  co  m*/
 */
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;
}