-
Notifications
You must be signed in to change notification settings - Fork 74
Lyra Cookbook
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 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.
RpcClients can be made recoverable via Lyra with some simple config.
// Create a recoverable connection and channel. Disable consumer.
ConnectionOptions options = new ConnectionOptions();
final Config config = new Config().withRecoveryPolicy(RecoveryPolicies.recoverAlways());
config.withConsumerRecovery(false);
Connection connection = Connections.create(options, config);
Channel channel = connection.createChannel();
// Create an RpcClient that sets up a new consumer after the channel is recovered.
final RpcClient rpcClient = new RpcClient(channel, "foo-exchange", "#") {
{
config.withChannelListeners(new DefaultChannelListener() {
public void onRecovery(Channel channel) {
try {
setupConsumer();
} catch (IOException e) {
}
}
});
}
};