Skip to content
jhalterman edited this page Oct 31, 2014 · 16 revisions

Below are some recipes for using Lyra to reliably handle common Rabbit use cases:

Publish / Subscribe

Publish / Subscribe in Rabbit often involves queues that are automatically deleted when their consumers are lost. Lyra handles this automatically, recovering queues, bindings, connections, channels and consumers:

Config config = new Config().withRecoveryPolicy(RecoveryPolicies.recoverAlways());

// Create a recoverable connection, channel, queue, queue binding, and consumer    
Connection connection = Connections.create(config);
Channel channel = connection.createChannel();
channel.queueDeclare("foo-queue", false, false, true, null);
channel.queueBind("foo-queue", "foo-exchange", "#");
channel.basicConsume("foo-queue", myConsumer);

When the connection fails everything is automatically recovered, according to the recovery policy.

Recoverable RpcClient

RpcClients can be made recoverable via Lyra with some simple config.

// Create a recoverable connection and channel. Disable consumer recovery.
final Config config = new Config()
    .withRecoveryPolicy(RecoveryPolicies.recoverAlways());
    .withConsumerRecovery(false);

Connection connection = Connections.create(config);
Channel channel = connection.createChannel();

// Create an RpcClient that creates a new consumer after the channel is recovered.
RpcClient rpcClient = new RpcClient(channel, "foo-exchange", "#") {
  {
    config.withChannelListeners(new DefaultChannelListener() {
      public void onRecovery(Channel channel) {
        try {
          // Create new internal RpcClient consumer
          setupConsumer();
        } catch (IOException e) {
        }
      }
    });
  }
};
Clone this wiki locally