Example usage for com.rabbitmq.client ConnectionFactory setAutomaticRecoveryEnabled

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

Introduction

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

Prototype

public void setAutomaticRecoveryEnabled(boolean automaticRecovery) 

Source Link

Document

Enables or disables <a href="https://www.rabbitmq.com/api-guide.html#recovery">automatic connection recovery</a>.

Usage

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();/*from   w w w . j a v  a  2s .com*/
    connection.close();
}

From source file:com.zero_x_baadf00d.play.module.rabbitmq.RabbitMQModuleImpl.java

License:Open Source License

/**
 * Build an instance./*from  w  ww .  ja  v a2 s  .  c om*/
 *
 * @param lifecycle     The current application lifecyle
 * @param configuration The current application configuration
 * @since 16.05.19
 */
@Inject
public RabbitMQModuleImpl(final ApplicationLifecycle lifecycle, final Config configuration) {
    this.configuration = configuration;
    try {
        final String uri = configuration.getString(RabbitMQModuleImpl.RABBITMQ_CONN_URI);
        if (uri == null || uri.isEmpty()) {
            throw new RuntimeException("URI is empty");
        }
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(uri);
        connectionFactory
                .setRequestedHeartbeat(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_CONN_HEARTBEAT));
        connectionFactory
                .setNetworkRecoveryInterval(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_CONN_RECOVERY));
        connectionFactory.setConnectionTimeout(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_CONN_TIMEOUT));
        connectionFactory.setAutomaticRecoveryEnabled(
                configuration.getBoolean(RabbitMQModuleImpl.RABBITMQ_AUTO_RECOVERY));
        if (uri.toLowerCase(Locale.ENGLISH).startsWith("amqps://")) {
            connectionFactory.useSslProtocol();
        }

        final ExecutorService es = Executors
                .newFixedThreadPool(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_EXECUTOR));
        this.rabbitConnection = connectionFactory.newConnection(es);
        RabbitMQModuleImpl.LOGGER.info("RabbitMQ connected at {}",
                String.format("amqp%s://%s:%d/%s", connectionFactory.isSSL() ? "s" : "",
                        connectionFactory.getHost(), connectionFactory.getPort(),
                        connectionFactory.getVirtualHost()));
    } catch (Exception ex) {
        this.rabbitConnection = null;
        if (!this.configuration.getBoolean(RabbitMQModuleImpl.RABBITMQ_BYPASS_ERROR)) {
            RabbitMQModuleImpl.LOGGER.error("Can't initialize RabbitMQ module", ex);
            throw new RuntimeException(ex);
        } else {
            RabbitMQModuleImpl.LOGGER.warn("Can't initialize RabbitMQ module: {}", ex.getMessage());
        }
    }

    lifecycle.addStopHook(() -> {
        RabbitMQModuleImpl.LOGGER.info("Shutting down RabbitMQ");
        if (this.rabbitConnection != null) {
            this.rabbitConnection.close();
        }
        return CompletableFuture.completedFuture(null);
    });
}

From source file:edu.iu.messaging.service.core.impl.RabbitMQPublisher.java

License:Apache License

private void connect() {
    try {//from   w w w  . j a  va2 s. c  om
        logger.info("connect() -> Connecting to server");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(properties.getBrokerUrl());
        connectionFactory.setAutomaticRecoveryEnabled(properties.isAutoRecoveryEnable());
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });

        channel = connection.createChannel();

        /*
        Not required for work queue implementation
         */
        //channel.basicQos(properties.getPrefetchCount());
        //channel.exchangeDeclare(properties.getExchangeName(), properties.getExchangeType(), true);

    } catch (Exception e) {
        logger.error("connect() -> Error connecting to server.", e);
    }

}

From source file:edu.iu.messaging.service.core.impl.RabbitMQSubscriber.java

License:Apache License

private void createConnection() {
    try {//from  ww w  .  ja  v  a2  s . c om
        logger.info("createConnection() -> Connecting to server");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(properties.getBrokerUrl());
        connectionFactory.setAutomaticRecoveryEnabled(properties.isAutoRecoveryEnable());
        connection = connectionFactory.newConnection();
        addShutdownListener();
        channel = connection.createChannel();

        /*
        Not required for work queue implementation
         */
        //channel.basicQos(Constants.PREFETCH_COUT);
        //channel.exchangeDeclare(properties.getExchangeName(), properties.getExchangeType(), true);

    } catch (Exception e) {
        logger.error("createConnection() -> Error connecting to server.", e);
    }
}

From source file:eu.netide.util.topology.update.impl.NotificationProducer.java

License:Open Source License

public void init(String rabbitHost, int rabbitPort, String rabbitUser, String rabbitPassword,
        String rabbitVirtualHost, String exchangeName, String baseTopicName, String nodeTopicName,
        String nodeConnectorTopicName, String linkTopicName) {

    this.exchangeName = exchangeName;
    this.baseTopicName = baseTopicName;
    this.nodeTopicName = nodeTopicName;
    this.nodeConnectorTopicName = nodeConnectorTopicName;
    this.linkTopicName = linkTopicName;

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(rabbitUser);//from w  ww  . ja v  a2 s.  co m
    factory.setPassword(rabbitPassword);
    factory.setVirtualHost(rabbitVirtualHost);
    factory.setHost(rabbitHost);
    factory.setPort(rabbitPort);
    factory.setAutomaticRecoveryEnabled(true);

    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(exchangeName, "topic", true);
        init = true;
    } catch (IOException e) {
        LOG.error(e.getMessage());
    } catch (TimeoutException e) {
        LOG.error(e.getMessage());
    }
}

From source file:info.pancancer.arch3.utils.Utilities.java

License:Open Source License

/**
 * Clears database state and known queues for testing.
 *
 * @param settings//ww  w. ja v a  2s .c om
 * @throws IOException
 * @throws java.util.concurrent.TimeoutException
 */
public static void clearState(HierarchicalINIConfiguration settings) throws IOException, TimeoutException {
    File configFile = FileUtils.getFile("src", "test", "resources", "config");
    HierarchicalINIConfiguration parseConfig = Utilities.parseConfig(configFile.getAbsolutePath());
    PostgreSQL postgres = new PostgreSQL(parseConfig);
    // clean up the database
    postgres.clearDatabase();

    String server = settings.getString(Constants.RABBIT_HOST);
    String user = settings.getString(Constants.RABBIT_USERNAME);
    String pass = settings.getString(Constants.RABBIT_PASSWORD);

    Channel channel;

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(server);
    factory.setUsername(user);
    factory.setPassword(pass);
    factory.setAutomaticRecoveryEnabled(true);
    Connection connection = factory.newConnection();
    channel = connection.createChannel();
    channel.basicQos(1);
    channel.confirmSelect();

    String prefix = settings.getString(Constants.RABBIT_QUEUE_NAME);
    String[] queues = { prefix + "_jobs", prefix + "_orders", prefix + "_vms", prefix + "_for_CleanupJobs",
            prefix + "_for_CleanupVMs" };
    for (String queue : queues) {
        try {
            channel.queueDelete(queue);
        } catch (IOException e) {
            Log.info("Could not delete " + queue);
        }
    }
}

From source file:info.pancancer.arch3.utils.Utilities.java

License:Open Source License

public static Channel setupQueue(HierarchicalINIConfiguration settings, String queue)
        throws IOException, TimeoutException {

    String server = settings.getString(Constants.RABBIT_HOST);
    String user = settings.getString(Constants.RABBIT_USERNAME);
    String pass = settings.getString(Constants.RABBIT_PASSWORD);

    Channel channel = null;//w ww.  ja  va2  s  . co m

    try {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(server);
        factory.setUsername(user);
        factory.setPassword(pass);
        factory.setAutomaticRecoveryEnabled(true);
        Connection connection = factory.newConnection();
        channel = connection.createChannel();
        channel.basicQos(1);
        channel.queueDeclare(queue, true, false, false, null);
        channel.confirmSelect();
        // channel.queueDeclarePassive(queue);

    } catch (IOException | TimeoutException ex) {
        // Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);
        LOG.error("Error setting up queue connections to queue:" + queue + " on host: " + server
                + "; error is: " + ex.getMessage(), ex);
    }
    return channel;

}

From source file:info.pancancer.arch3.utils.Utilities.java

License:Open Source License

public static Channel setupExchange(HierarchicalINIConfiguration settings, String queue)
        throws IOException, TimeoutException {

    String server = settings.getString(Constants.RABBIT_HOST);
    String user = settings.getString(Constants.RABBIT_USERNAME);
    String pass = settings.getString(Constants.RABBIT_PASSWORD);

    Channel channel = null;// w  w w . j  ava2s  .co m

    try {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(server);
        factory.setUsername(user);
        factory.setPassword(pass);
        factory.setAutomaticRecoveryEnabled(true);
        Connection connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(queue, "fanout", true, false, null);
        channel.confirmSelect();

    } catch (IOException | TimeoutException ex) {
        // Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);
        LOG.error("Error setting up queue connections: " + ex.getMessage(), ex);
        throw ex;
    }
    return channel;
}

From source file:io.bootique.rabbitmq.client.connection.ConnectionConfig.java

License:Apache License

public Connection createConnection(String connectionName) {
    com.rabbitmq.client.ConnectionFactory factory = createConnectionFactory();

    factory.setRequestedChannelMax(requestedChannelMax);
    factory.setRequestedFrameMax(requestedFrameMax);
    factory.setRequestedHeartbeat(requestedHeartbeat);
    factory.setConnectionTimeout(connectionTimeout);
    factory.setHandshakeTimeout(handshakeTimeout);
    factory.setShutdownTimeout(shutdownTimeout);
    factory.setAutomaticRecoveryEnabled(automaticRecoveryEnabled);
    factory.setTopologyRecoveryEnabled(topologyRecovery);
    factory.setNetworkRecoveryInterval(networkRecoveryInterval);

    LOGGER.info("Creating RabbitMQ connection.");
    try {//from  w ww.  j a  v a 2s  .com
        return factory.newConnection();
    } catch (IOException | TimeoutException e) {
        throw new RuntimeException(String.format("Can't create connection \"%s\".", connectionName), e);
    }
}

From source file:it.av.fac.messaging.rabbitmq.RabbitMQConnectionWrapper.java

private RabbitMQConnectionWrapper() throws FileNotFoundException, IOException, TimeoutException {
    Properties msgProperties = new Properties();
    msgProperties.load(new FileInputStream(MessagingConfig.PROPERTIES_FILE));

    String addr = msgProperties.getProperty("provider.addr", "127.0.0.1");
    int port = Integer.valueOf(msgProperties.getProperty("provider.port", "5672"));
    String username = msgProperties.getProperty("provider.auth.user", "guest");
    String password = msgProperties.getProperty("provider.auth.pass", "guest");

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(addr);/*w  ww. j  a  v a 2s  .c om*/
    factory.setPort(port);
    factory.setUsername(username);
    factory.setPassword(password);
    factory.setAutomaticRecoveryEnabled(true);
    this.conn = factory.newConnection();
}