The Shared Subscription is used to create a work queue. Let’s test out the Shared Subscription with our topic with 3 partitions.
To test the Shared Subscription, we reintroduce the receiverQueueSize and simulating work.
Consumer consumer = client.newConsumer()
.topic("persistent://public/default/bytetopic")
.subscriptionName("mysubscription")
.subscriptionType(SubscriptionType.Shared)
//default receiverQueueSize is 1000, reduce to 1 for testing only to watch Shared Subscription distribute load
.receiverQueueSize(1)
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.subscribe();
import java.util.Random;
Random random = new Random();
//random wait to simulate work when demonstrating Shared subscription with small receive queue size
System.out.println("Received messaged " + new String(msg.getData()));
Thread.sleep(random.nextInt(10000));
Messages are distributed across the multiple consumers without message ordering guarantees. All consumers may receive messages from all partitions.