net.jodah.lyra.Connections Java Examples

The following examples show how to use net.jodah.lyra.Connections. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectionUtils.java    From simpleci with MIT License 5 votes vote down vote up
public static Connection createRabbitmqConnection(String host, int port, String user, String password) throws IOException, TimeoutException {
    Config config = new Config()
            .withRecoveryPolicy(new RecoveryPolicy()
                    .withBackoff(Duration.seconds(1), Duration.seconds(30))
                    .withMaxAttempts(20));
    ConnectionOptions options = new ConnectionOptions()
            .withHost(host)
            .withPort(port)
            .withUsername(user)
            .withPassword(password);

    return Connections.create(options, config);
}
 
Example #2
Source File: RabbitMQMessagingService.java    From elasticactors with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void start() throws IOException, TimeoutException {
    // millis
    connectionFactory.setConnectionTimeout(1000);
    // seconds
    connectionFactory.setRequestedHeartbeat(4);
    // turn off recovery as we are using Lyra
    connectionFactory.setAutomaticRecoveryEnabled(false);
    connectionFactory.setTopologyRecoveryEnabled(false);
    // lyra reconnect logic
    Config config = new Config()
            .withRecoveryPolicy(new RecoveryPolicy()
                    .withMaxAttempts(-1)
                    .withInterval(Duration.seconds(1)))
            .withChannelListeners(this);

    ConnectionOptions connectionOptions = new ConnectionOptions(connectionFactory)
            .withHosts(StringUtils.commaDelimitedListToStringArray(rabbitmqHosts))
            .withPort(rabbitmqPort)
            .withUsername(username)
            .withPassword(password);
    // create single connection
    //clientConnection = connectionFactory.newConnection(Address.parseAddresses(rabbitmqHosts));
    clientConnection = Connections.create(connectionOptions,config);
    // create a seperate consumer channel
    consumerChannel = clientConnection.createChannel();
    consumerChannel.basicQos(prefetchCount);
    // prepare the consumer channels
    for (int i = 0; i < queueExecutor.getThreadCount(); i++) {
        producerChannels.add(clientConnection.createChannel());
    }
    // add logging shutdown listener
    consumerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE);
    for (Channel producerChannel : producerChannels) {
        producerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE);
    }
    // ensure the exchange is there
    consumerChannel.exchangeDeclare(exchangeName,"direct",true);
    if(ackType == BUFFERED) {
        messageAcker = new BufferingMessageAcker(consumerChannel);
    } else if(ackType == WRITE_BEHIND) {
        messageAcker = new WriteBehindMessageAcker(consumerChannel);
    } else if(ackType == ASYNC) {
        messageAcker = new AsyncMessageAcker(consumerChannel);
    } else {
        messageAcker = new DirectMessageAcker(consumerChannel);
    }
    messageAcker.start();
}
 
Example #3
Source File: RabbitMQMessagingService.java    From elasticactors with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void start() throws IOException, TimeoutException {
    // millis
    connectionFactory.setConnectionTimeout(1000);
    // seconds
    connectionFactory.setRequestedHeartbeat(4);
    // turn off recovery as we are using Lyra
    connectionFactory.setAutomaticRecoveryEnabled(false);
    connectionFactory.setTopologyRecoveryEnabled(false);
    // lyra reconnect logic
    Config config = new Config()
            .withRecoveryPolicy(new RecoveryPolicy()
                    .withMaxAttempts(-1)
                    .withInterval(Duration.seconds(1)))
            .withChannelListeners(this);

    ConnectionOptions connectionOptions = new ConnectionOptions(connectionFactory)
            .withHosts(StringUtils.commaDelimitedListToStringArray(rabbitmqHosts))
            .withPort(rabbitmqPort)
            .withUsername(username)
            .withPassword(password);
    // create single connection
    //clientConnection = connectionFactory.newConnection(Address.parseAddresses(rabbitmqHosts));
    clientConnection = Connections.create(connectionOptions,config);
    // create a seperate producer and a seperate consumer channel
    consumerChannel = clientConnection.createChannel();
    consumerChannel.basicQos(prefetchCount);
    producerChannel = clientConnection.createChannel();
    // add logging shutdown listener
    consumerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE);
    producerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE);
    // ensure the exchange is there
    consumerChannel.exchangeDeclare(exchangeName,"direct",true);
    if(ackType == BUFFERED) {
        messageAcker = new BufferingMessageAcker(consumerChannel);
    } else if(ackType == WRITE_BEHIND) {
        messageAcker = new WriteBehindMessageAcker(consumerChannel);
    } else if(ackType == ASYNC) {
        messageAcker = new AsyncMessageAcker(consumerChannel);
    } else {
        messageAcker = new DirectMessageAcker(consumerChannel);
    }
    messageAcker.start();
}