net.jodah.lyra.ConnectionOptions Java Examples

The following examples show how to use net.jodah.lyra.ConnectionOptions. 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: ConnectionHandler.java    From lyra with Apache License 2.0 5 votes vote down vote up
public ConnectionHandler(ConnectionOptions options, Config config, ClassLoader classLoader) throws IOException {
  this.options = options;
  this.config = config;
  this.classLoader = Assert.notNull(classLoader, "classLoader");
  this.connectionName =
      options.getName() == null ? String.format("cxn-%s", CONNECTION_COUNTER.incrementAndGet())
          : options.getName();
  consumerThreadPool =
      options.getConsumerExecutor() == null ? Executors.newCachedThreadPool(new NamedThreadFactory(
          String.format("rabbitmq-%s-consumer", connectionName), config.isUsingDaemonThreads())) : options.getConsumerExecutor();
}
 
Example #3
Source File: AbstractFunctionalTest.java    From lyra with Apache License 2.0 5 votes vote down vote up
protected void mockConnection() throws IOException, TimeoutException {
  if (connectionFactory == null) {
    mockConnectionOnly();
    connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.getVirtualHost()).thenReturn("/");
    when(connectionFactory.newConnection(any(ExecutorService.class), any(Address[].class), anyString()))
        .thenReturn(connection);
  }

  if (options == null)
    options = new ConnectionOptions().withHost("test-host");
  options.withConnectionFactory(connectionFactory);
  if (config == null)
    config =
        new Config().withRetryPolicy(
            RetryPolicies.retryAlways().withInterval(Duration.millis(10))).withRecoveryPolicy(
            RecoveryPolicies.recoverAlways());

  if (connectionHandler == null) {
    connectionHandler = new ConnectionHandler(options, config, Connection.class.getClassLoader());
    connectionProxy =
        (ConfigurableConnection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
            new Class<?>[] {ConfigurableConnection.class}, connectionHandler);
    connectionHandler.createConnection(connectionProxy);
    channels = new HashMap<Integer, MockChannel>();
  }
}
 
Example #4
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 #5
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();
}