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:

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

// Create a recoverable connection, channel, queue, queue binding, and consumer    
Connection connection = Connections.create(options, 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 with some simple config. First, we need to disable Lyra's consumer recovery, which is enabled by default:

config.withConsumerRecovery(false);

Then using a ChannelListener we can instruct the RpcClient to setup a new consumer after the channel is recoverred:

config.withChannelListeners(new AbstractChannelListener() {
    public void onRecovery(Channel channel) {
        rpcClient.setupConsumer();
    }
});
Clone this wiki locally