|
| 1 | +package com.baeldung.subscriptions; |
| 2 | + |
| 3 | +import org.apache.pulsar.client.api.Consumer; |
| 4 | +import org.apache.pulsar.client.api.ConsumerBuilder; |
| 5 | +import org.apache.pulsar.client.api.Message; |
| 6 | +import org.apache.pulsar.client.api.MessageBuilder; |
| 7 | +import org.apache.pulsar.client.api.Producer; |
| 8 | +import org.apache.pulsar.client.api.PulsarClient; |
| 9 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 10 | +import org.apache.pulsar.client.api.SubscriptionType; |
| 11 | + |
| 12 | +import java.util.stream.IntStream; |
| 13 | + |
| 14 | +public class FailoverSubscriptionTest { |
| 15 | + private static final String SERVICE_URL = "pulsar://localhost:6650"; |
| 16 | + private static final String TOPIC_NAME = "failover-subscription-test-topic"; |
| 17 | + private static final String SUBSCRIPTION_NAME = "test-subscription"; |
| 18 | + private static final SubscriptionType SUBSCRIPTION_TYPE = SubscriptionType.Failover; |
| 19 | + private static final int NUM_MSGS = 10; |
| 20 | + |
| 21 | + public static void main(String[] args) throws PulsarClientException { |
| 22 | + PulsarClient client = PulsarClient.builder() |
| 23 | + .serviceUrl(SERVICE_URL) |
| 24 | + .build(); |
| 25 | + |
| 26 | + Producer<byte[]> producer = client.newProducer() |
| 27 | + .topic(TOPIC_NAME) |
| 28 | + .create(); |
| 29 | + |
| 30 | + ConsumerBuilder<byte[]> consumerBuilder = client.newConsumer() |
| 31 | + .topic(TOPIC_NAME) |
| 32 | + .subscriptionName(SUBSCRIPTION_NAME) |
| 33 | + .subscriptionType(SUBSCRIPTION_TYPE); |
| 34 | + |
| 35 | + Consumer<byte[]> mainConsumer = consumerBuilder |
| 36 | + .consumerName("consumer-a") |
| 37 | + .messageListener((consumer, msg) -> { |
| 38 | + System.out.println("Message received by main consumer"); |
| 39 | + |
| 40 | + try { |
| 41 | + consumer.acknowledge(msg); |
| 42 | + } catch (PulsarClientException e) { |
| 43 | + System.out.println(e.getMessage()); |
| 44 | + } |
| 45 | + }) |
| 46 | + .subscribe(); |
| 47 | + |
| 48 | + Consumer<byte[]> failoverConsumer = consumerBuilder |
| 49 | + .consumerName("consumer-b") |
| 50 | + .messageListener((consumer, msg) -> { |
| 51 | + System.out.println("Message received by failover consumer"); |
| 52 | + |
| 53 | + try { |
| 54 | + consumer.acknowledge(msg); |
| 55 | + } catch (PulsarClientException e) { |
| 56 | + System.out.println(e.getMessage()); |
| 57 | + } |
| 58 | + }) |
| 59 | + .subscribe(); |
| 60 | + |
| 61 | + IntStream.range(0, NUM_MSGS).forEach(i -> { |
| 62 | + Message<byte[]> msg = MessageBuilder.create() |
| 63 | + .setContent(String.format("message-%d", i).getBytes()) |
| 64 | + .build(); |
| 65 | + try { |
| 66 | + producer.send(msg); |
| 67 | + |
| 68 | + Thread.sleep(100); |
| 69 | + |
| 70 | + if (i > 5) mainConsumer.close(); |
| 71 | + } catch (InterruptedException | PulsarClientException e) { |
| 72 | + System.out.println(e.getMessage()); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
0 commit comments