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:org.apache.flume.rabbitmq.RabbitMQConnectionFactory.java

License:Apache License

private RabbitMQConnectionFactory(List<Address> hosts, int port, String virtualHost, int connectionTimeout,
        int requestedHeartbeat, int requestedChannelMax, int requestedFrameMax, String userName,
        String password, ConnectionFactory connectionFactory) {

    Preconditions.checkArgument(hosts != null, "host can not be null");
    Preconditions.checkArgument(!hosts.isEmpty(), "hosts can not be empty");
    Preconditions.checkArgument(userName != null, "user name can not be null");
    Preconditions.checkArgument(password != null, "password can not be null");
    Preconditions.checkArgument(virtualHost != null, "virtual host can not be null");
    Preconditions.checkArgument(connectionFactory != null, "connection factory can not be null");

    this.hosts = hosts;

    this.connectionFactory = connectionFactory;
    connectionFactory.setPort(port);
    if (this.hosts.size() == 1) { // Only one host to connect
        Address address = hosts.get(0);/*from  w  ww .j av  a 2  s. c o  m*/
        this.connectionFactory.setHost(address.getHost());
        if (address.getPort() != -1) { // If port is also supplied then use it.
            this.connectionFactory.setPort(address.getPort());
        }
    }
    this.connectionFactory.setVirtualHost(virtualHost);
    this.connectionFactory.setConnectionTimeout(connectionTimeout);
    this.connectionFactory.setRequestedHeartbeat(requestedHeartbeat);
    this.connectionFactory.setRequestedChannelMax(requestedChannelMax);
    this.connectionFactory.setRequestedFrameMax(requestedFrameMax);
    this.connectionFactory.setUsername(userName);
    this.connectionFactory.setPassword(password);
}

From source file:org.apache.flume.RabbitMQUtil.java

License:Apache License

public static ConnectionFactory getFactory(Context context) {
    Preconditions.checkArgument(context != null, "context cannot be null.");
    ConnectionFactory factory = new ConnectionFactory();

    String hostname = context.getString("hostname");
    Preconditions.checkArgument(hostname != null, "No hostname specified.");
    factory.setHost(hostname);// w w w.  j  a  v a2s . co m

    int port = context.getInteger(RabbitMQConstants.CONFIG_PORT, -1);

    if (-1 != port) {
        factory.setPort(port);
    }

    String username = context.getString(RabbitMQConstants.CONFIG_USERNAME);

    if (null == username) {
        factory.setUsername(ConnectionFactory.DEFAULT_USER);
    } else {
        factory.setUsername(username);
    }

    String password = context.getString(RabbitMQConstants.CONFIG_PASSWORD);

    if (null == password) {
        factory.setPassword(ConnectionFactory.DEFAULT_PASS);
    } else {
        factory.setPassword(password);
    }

    String virtualHost = context.getString(RabbitMQConstants.CONFIG_VIRTUALHOST);

    if (null != virtualHost) {
        factory.setVirtualHost(virtualHost);
    }

    int connectionTimeout = context.getInteger(RabbitMQConstants.CONFIG_CONNECTIONTIMEOUT, -1);

    if (connectionTimeout > -1) {
        factory.setConnectionTimeout(connectionTimeout);
    }

    //        boolean useSSL = context.getBoolean("usessl", false);
    //        if(useSSL){
    //            factory.useSslProtocol();
    //        }

    return factory;
}

From source file:org.apache.james.backend.rabbitmq.DockerRabbitMQ.java

License:Apache License

public ConnectionFactory connectionFactory() {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(getHostIp());
    connectionFactory.setPort(getPort());
    connectionFactory.setUsername(getUsername());
    connectionFactory.setPassword(getPassword());
    return connectionFactory;
}

From source file:org.apache.nifi.amqp.processors.AbstractAMQPProcessor.java

License:Apache License

/**
 * Creates {@link Connection} to AMQP system.
 *///from  w  ww . j  a v  a2 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 Boolean useCertAuthentication = context.getProperty(USE_CERT_AUTHENTICATION).asBoolean();
    final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE)
            .asControllerService(SSLContextService.class);
    // if the property to use cert authentication is set but the SSL service hasn't been configured, throw an exception.
    if (useCertAuthentication && sslService == null) {
        throw new ProviderCreationException("This processor is configured to use cert authentication, "
                + "but the SSL Context Service hasn't been configured. You need to configure the SSL Context Service.");
    }
    final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();

    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(), ", ")));
            }
        }
        final SSLContext sslContext = sslService.createSSLContext(clientAuth);
        cf.useSslProtocol(sslContext);

        if (useCertAuthentication) {
            // this tells the factory to use the cert common name for authentication and not user name and password
            // REF: https://github.com/rabbitmq/rabbitmq-auth-mechanism-ssl
            cf.setSaslConfig(DefaultSaslConfig.EXTERNAL);
        }
    }

    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:org.apache.nutch.indexwriter.rabbit.RabbitIndexWriter.java

License:Apache License

@Override
public void open(JobConf JobConf, String name) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(serverHost);//from  ww w  .j  a va 2s  .  com
    factory.setPort(serverPort);

    if (serverVirtualHost != null) {
        factory.setVirtualHost(serverVirtualHost);
    }

    factory.setUsername(serverUsername);
    factory.setPassword(serverPassword);

    try {
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.exchangeDeclare(exchangeServer, exchangeType, true);
        channel.queueDeclare(queueName, queueDurable, false, false, null);
        channel.queueBind(queueName, exchangeServer, queueRoutingKey);

    } catch (TimeoutException | IOException ex) {
        throw makeIOException(ex);
    }
}

From source file:org.apache.nutch.publisher.rabbitmq.RabbitMQPublisherImpl.java

License:Apache License

@Override
public boolean setConfig(Configuration conf) {
    try {//  www .j  ava  2 s  .c om
        EXCHANGE_SERVER = conf.get("rabbitmq.exchange.server", "fetcher_log");
        EXCHANGE_TYPE = conf.get("rabbitmq.exchange.type", "fanout");

        HOST = conf.get("rabbitmq.host", "localhost");
        PORT = conf.getInt("rabbitmq.port", 5672);
        VIRTUAL_HOST = conf.get("rabbitmq.virtualhost", null);
        USERNAME = conf.get("rabbitmq.username", null);
        PASSWORD = conf.get("rabbitmq.password", null);

        QUEUE_NAME = conf.get("rabbitmq.queue.name", "fanout.queue");
        QUEUE_DURABLE = conf.getBoolean("rabbitmq.queue.durable", true);
        QUEUE_ROUTING_KEY = conf.get("rabbitmq.queue.routingkey", "fanout.key");

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST);
        factory.setPort(PORT);

        if (VIRTUAL_HOST != null) {
            factory.setVirtualHost(VIRTUAL_HOST);
        }

        if (USERNAME != null) {
            factory.setUsername(USERNAME);
            factory.setPassword(PASSWORD);
        }

        Connection connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_SERVER, EXCHANGE_TYPE);
        channel.queueDeclare(QUEUE_NAME, QUEUE_DURABLE, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_SERVER, QUEUE_ROUTING_KEY);

        LOG.info("Configured RabbitMQ publisher");
        return true;
    } catch (Exception e) {
        LOG.error("Could not initialize RabbitMQ publisher - {}", StringUtils.stringifyException(e));
        return false;
    }

}

From source file:org.apache.nutch.rabbitmq.RabbitMQClient.java

License:Apache License

/**
 * Builds a new instance of {@link RabbitMQClient}
 *
 * @param serverHost        The server host.
 * @param serverPort        The server port.
 * @param serverVirtualHost The virtual host into the RabbitMQ server.
 * @param serverUsername    The username to access the server.
 * @param serverPassword    The password to access the server.
 * @throws IOException It is thrown if there is some issue during the connection creation.
 *//*  w  ww .  ja va  2 s .co  m*/
public RabbitMQClient(String serverHost, int serverPort, String serverVirtualHost, String serverUsername,
        String serverPassword) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(getValue(serverHost, "localhost"));
    factory.setPort(getValue(serverPort, 5672));

    factory.setVirtualHost(getValue(serverVirtualHost, "/"));

    factory.setUsername(getValue(serverUsername, "guest"));
    factory.setPassword(getValue(serverPassword, "guest"));

    try {
        connection = factory.newConnection();
    } catch (TimeoutException e) {
        throw makeIOException(e);
    }
}

From source file:org.ballerinalang.messaging.rabbitmq.util.ConnectionUtils.java

License:Open Source License

/**
 * Creates a RabbitMQ Connection using the given connection parameters.
 *
 * @param connectionConfig Parameters used to initialize the connection.
 * @return RabbitMQ Connection object.//from  w  w  w. j  a va2 s .  co m
 */
public static Connection createConnection(BMap<String, BValue> connectionConfig) {
    try {
        ConnectionFactory connectionFactory = new ConnectionFactory();

        String host = RabbitMQUtils.getStringFromBValue(connectionConfig,
                RabbitMQConstants.RABBITMQ_CONNECTION_HOST);
        connectionFactory.setHost(host);

        int port = RabbitMQUtils.getIntFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_PORT,
                LOGGER);
        connectionFactory.setPort(port);

        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_USER) != null) {
            connectionFactory.setUsername(RabbitMQUtils.getStringFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_USER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_PASS) != null) {
            connectionFactory.setPassword(RabbitMQUtils.getStringFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_PASS));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_TIMEOUT) != null) {
            connectionFactory.setConnectionTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_TIMEOUT, LOGGER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_HANDSHAKE_TIMEOUT) != null) {
            connectionFactory.setHandshakeTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_HANDSHAKE_TIMEOUT, LOGGER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_SHUTDOWN_TIMEOUT) != null) {
            connectionFactory.setShutdownTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_SHUTDOWN_TIMEOUT, LOGGER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_HEARTBEAT) != null) {
            connectionFactory.setRequestedHeartbeat(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_HEARTBEAT, LOGGER));
        }
        return connectionFactory.newConnection();
    } catch (IOException | TimeoutException exception) {
        LOGGER.error(RabbitMQConstants.CREATE_CONNECTION_ERROR, exception);
        throw new RabbitMQConnectorException(RabbitMQConstants.CREATE_CONNECTION_ERROR + exception.getMessage(),
                exception);
    }
}

From source file:org.belio.mq.RabbitConsumer.java

@Override
public void open() throws IOException {

    try {// w  ww. ja va 2s  . c o  m
        com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
        factory.setHost(mqproperties.getProperty("host"));
        factory.setVirtualHost(mqproperties.getProperty("vhost"));
        factory.setUsername(mqproperties.getProperty("username"));
        factory.setPassword(mqproperties.getProperty("password"));
        factory.setPort(Integer.parseInt(mqproperties.getProperty("port")));
        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(1);
        // Create a new connection to MQ
        connection = factory.newConnection();
        // Create a new channel and declare it's type and exhange as well
        //Create a new rabbit publisher           
        executor = Executors.newScheduledThreadPool(
                Integer.parseInt(threadproperties.getProperty(queueType.name().toLowerCase()).split(",")[0]));

    } catch (IOException ex) {
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ShutdownSignalException ex) {
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ConsumerCancelledException ex) {
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.belio.mq.RabbitConsumer.java

private void openPublisher() {
    try {//from   www.j  ava 2 s  .  com
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(mqproperties.getProperty("host"));
        factory.setVirtualHost(mqproperties.getProperty("vhost"));
        factory.setUsername(mqproperties.getProperty("username"));
        factory.setPassword(mqproperties.getProperty("password"));
        factory.setPort(Integer.parseInt(mqproperties.getProperty("port")));
        // Create a new connection to MQ
        publishingConnection = factory.newConnection();
        // Create a new channel and declare it's type and exhange as well
        publishingChannel = connection.createChannel();
        publishingChannel.queueDeclare(queueType.name().concat(QUEUE_SUFFIX), true, false, false, null);
        publishingChannel.exchangeDeclare(queueType.name().concat(EXCHANGE_SUFFIX),
                mqproperties.getProperty("type"));
        publishingChannel.queueBind(queueType.name().concat(QUEUE_SUFFIX),
                queueType.name().concat(EXCHANGE_SUFFIX), "");
    } catch (IOException exception) {
        Launcher.LOG.error(exception.getLocalizedMessage(), exception);
    }
}