Example usage for com.rabbitmq.client ConnectionFactory setConnectionTimeout

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

Introduction

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

Prototype

public void setConnectionTimeout(int timeout) 

Source Link

Document

Set the TCP connection timeout.

Usage

From source file:org.apache.flume.amqp.AmqpSource.java

License:Apache License

@VisibleForTesting
static ConnectionFactory createConnectionFactoryFrom(Context context) {
    String host = context.getString(AmqpSourceConfigurationConstants.HOST, Constants.Defaults.HOST);
    int port = context.getInteger(AmqpSourceConfigurationConstants.PORT, Constants.Defaults.PORT);
    String virtualHost = context.getString(AmqpSourceConfigurationConstants.VIRTUAL_HOST,
            Constants.Defaults.VIRTUAL_HOST);
    String userName = context.getString(AmqpSourceConfigurationConstants.USER_NAME,
            Constants.Defaults.USER_NAME);
    String password = context.getString(AmqpSourceConfigurationConstants.PASSWORD, Constants.Defaults.PASSWORD);
    int connectionTimeout = context.getInteger(AmqpSourceConfigurationConstants.CONNECTION_TIMEOUT,
            Constants.Defaults.CONNECTION_TIMEOUT);
    int requestHeartbeat = context.getInteger(AmqpSourceConfigurationConstants.REQUEST_HEARTBEAT,
            Constants.Defaults.REQUESTED_HEARTBEAT);

    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(host);//w w w.  j  ava  2 s. c om
    connectionFactory.setPort(port);
    connectionFactory.setVirtualHost(virtualHost);
    connectionFactory.setUsername(userName);
    connectionFactory.setPassword(password);
    connectionFactory.setConnectionTimeout(connectionTimeout);
    connectionFactory.setRequestedHeartbeat(requestHeartbeat);

    return connectionFactory;
}

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  ww. ja  v  a 2s  .c  o  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.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  va  2s.c  om*/
 */
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.graylog2.radio.transports.amqp.AMQPSender.java

License:Open Source License

public void connect() throws IOException {
    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);/*from  w w w. ja va2 s .c o  m*/
    factory.setPort(port);

    factory.setVirtualHost(vHost);

    // Authenticate?
    if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
        factory.setUsername(username);
        factory.setPassword(password);
    }

    factory.setConnectionTimeout((int) connectTimeout.getMillis());

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

    // It's ok if the queue or exchange already exist.
    channel.queueDeclare(queueName, true, false, false, null);
    channel.exchangeDeclare(exchangeName, queueType, false, false, null);

    channel.queueBind(queueName, exchangeName, routingKey);
}

From source file:org.kairosdb.plugin.rabbitmq.core.RabbitmqService.java

License:MIT License

@Override
public void start() throws KairosDBException {

    try {/*from   w  ww  .  j  a v a2s.co  m*/
        LOGGER.info("[KRMQ] Starting to RabbitMQ consumer thread.");

        // Socket abstract connection with broker
        ConnectionFactory rabbitmqConnectionFactory = new ConnectionFactory();
        rabbitmqConnectionFactory.setHost(rabbmitmqHost);
        rabbitmqConnectionFactory.setVirtualHost(rabbitmqVirtualHost);
        rabbitmqConnectionFactory.setUsername(rabbitmqUser);
        rabbitmqConnectionFactory.setPassword(rabbitmqPassword);
        rabbitmqConnectionFactory.setPort(rabbitmqPort);
        rabbitmqConnectionFactory.setConnectionTimeout(rabbitmqTimeout);
        rabbitmqConnectionFactory.setRequestedChannelMax(rabbitmqChannelMax);
        rabbitmqConnectionFactory.setRequestedFrameMax(rabbitmqFrameMax);
        rabbitmqConnectionFactory.setRequestedHeartbeat(rabbitmqHearbeat);
        rabbitmqConnectionFactory.setAutomaticRecoveryEnabled(true);

        // Get KairosDatastore implementation
        KairosDatastore kairosDatabase = googleInjector.getInstance(KairosDatastore.class);

        // Create consumer thread
        RabbitmqConsumer consumer = new RabbitmqConsumer(kairosDatabase, rabbitmqConnectionFactory,
                bindingsFile, configurationJSONFieldValue, configurationJSONTimeStamp, configurationJSONTags,
                configurationCSVSeperator, configurationDefaultContentType);

        // Start consumer thread
        consumerThread = new Thread(consumer);
        consumerThread.start();

    } catch (Exception e) {
        LOGGER.error("[KRMQ] An error occurred: ", e);
    }
}

From source file:org.thingsboard.rule.engine.rabbitmq.TbRabbitMqNode.java

License:Apache License

@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
    this.config = TbNodeUtils.convert(configuration, TbRabbitMqNodeConfiguration.class);
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.config.getHost());
    factory.setPort(this.config.getPort());
    factory.setVirtualHost(this.config.getVirtualHost());
    factory.setUsername(this.config.getUsername());
    factory.setPassword(this.config.getPassword());
    factory.setAutomaticRecoveryEnabled(this.config.isAutomaticRecoveryEnabled());
    factory.setConnectionTimeout(this.config.getConnectionTimeout());
    factory.setHandshakeTimeout(this.config.getHandshakeTimeout());
    this.config.getClientProperties().forEach((k, v) -> factory.getClientProperties().put(k, v));
    try {/*from w  ww  .  ja va  2s  . co m*/
        this.connection = factory.newConnection();
        this.channel = this.connection.createChannel();
    } catch (Exception e) {
        throw new TbNodeException(e);
    }
}

From source file:org.trianacode.TrianaCloud.Broker.Receiver.java

License:Open Source License

public String init() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);//w  w  w. j a  va  2s  . co  m
    factory.setPort(port);
    factory.setUsername(user);
    factory.setPassword(password);
    factory.setVirtualHost(vHost);
    factory.setConnectionTimeout(60);

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

        consumer = new QueueingConsumer(channel);
        receiveQueueName = channel.queueDeclare("", false, false, true, null).getQueue();
        channel.basicConsume(receiveQueueName, true, consumer);
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        return null;
    }

    return receiveQueueName;
}

From source file:org.trianacode.TrianaCloud.Broker.RPCServer.java

License:Open Source License

public void init() throws ServletException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);/*from w  w w.  j a va 2  s.c  o  m*/
    factory.setPort(port);
    factory.setUsername(user);
    factory.setPassword(password);
    factory.setVirtualHost(vHost);
    factory.setConnectionTimeout(60);
    try {
        keepRunning = true;
        td = new TaskDAO();

        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(rpc_queue_name, false, false, false, null);

        channel.basicQos(1);

        consumer = new QueueingConsumer(channel);
        channel.basicConsume(rpc_queue_name, false, consumer);
        logger.info("[x] Awaiting RPC requests");
    } catch (Exception e) {
        ServletException se = new ServletException(e);
        logger.fatal("Something Happened!", se);
        throw se;
    }
}

From source file:org.wso2.carbon.event.adapter.rabbitmq.internal.util.RabbitMQOutputEventAdapterPublisher.java

License:Open Source License

/**
 * <pre>/*from  w  w  w. j  a  v a 2s.c  om*/
 * Create a rabbitmq ConnectionFactory instance
 * </pre>
 * @param hostName
 * @param port
 * @param userName
 * @param password
 * @param virtualHost
 * @return
 */
private synchronized static ConnectionFactory getConnectionFactory(String hostName, int port, String userName,
        String password, String virtualHost) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);
    factory.setPort(port);
    factory.setUsername(userName);
    factory.setPassword(password);
    factory.setVirtualHost(virtualHost);

    /**
     * Add connection recovery logic
     * @author Sang-Cheon Park
     * @date 2015.07.16
     */
    /**
     * connection that will recover automatically
     */
    factory.setAutomaticRecoveryEnabled(true);
    /**
     * attempt recovery every 5 seconds
     */
    factory.setNetworkRecoveryInterval(5 * 1000);
    /**
     * wait maximum 10 seconds to connect(if connection failed, will be retry to connect after 5 seconds)
     */
    factory.setConnectionTimeout(10 * 1000);

    return factory;
}

From source file:org.wso2.carbon.event.adaptor.rabbitmq.internal.EventAdapterHelper.java

License:Apache License

/**
 * <pre>//  ww  w  .  j  a  va2  s .co  m
 * Create a rabbitmq ConnectionFactory instance
 * </pre>
 * @param hostName
 * @param port
 * @param userName
 * @param password
 * @param virtualHost
 * @return
 */
public synchronized static ConnectionFactory getConnectionFactory(String hostName, int port, String userName,
        String password, String virtualHost) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);
    factory.setPort(port);
    factory.setUsername(userName);
    factory.setPassword(password);
    factory.setVirtualHost(virtualHost);

    /**
     * Add connection recovery logic
     * @author Sang-Cheon Park
     * @date 2015.07.16
     */
    /**
     * connection that will recover automatically
     */
    factory.setAutomaticRecoveryEnabled(true);
    /**
     * attempt recovery every 5 seconds
     */
    factory.setNetworkRecoveryInterval(5 * 1000);
    /**
     * wait maximum 10 seconds to connect(if connection failed, will be retry to connect after 5 seconds)
     */
    factory.setConnectionTimeout(10 * 1000);

    return factory;
}