Rob Lewis
06/20/2024, 1:37 PMRunLLM
06/20/2024, 1:37 PMKey_Shared mode, you can utilize the ConsumerEventListener interface. This interface allows you to handle events related to changes in the active consumer.
Here’s a concise example of how to set this up:
1. Implement the ConsumerEventListener interface:
import org.apache.pulsar.client.api.ConsumerEventListener;
import org.apache.pulsar.client.api.Consumer;
public class MyConsumerEventListener implements ConsumerEventListener {
@Override
public void becameActive(Consumer<?> consumer, int partitionId) {
System.out.println("Consumer became active: " + consumer.getConsumerName());
}
@Override
public void becameInactive(Consumer<?> consumer, int partitionId) {
System.out.println("Consumer became inactive: " + consumer.getConsumerName());
}
}
2. Set the ConsumerEventListener when creating the consumer:
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionType;
public class PulsarConsumerExample {
public static void main(String[] args) throws Exception {
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://localhost:6650")
.build();
Consumer<String> consumer = client.newConsumer(Schema.STRING)
.topic("my-topic")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Key_Shared)
.consumerEventListener(new MyConsumerEventListener())
.subscribe();
// Keep the application running to listen for events
Thread.sleep(Long.MAX_VALUE);
}
}
This code sets up a consumer in Key_Shared mode and attaches a ConsumerEventListener to handle events when the consumer becomes active or inactive.
Would you like more details on any specific part of this setup?
Sources:
- https://github.com/apache/pulsar/blob/master/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopicTest.java
- https://pulsar.apache.org/docs/3.2.x/concepts-messaging#failover--non-partitioned-topics
Note: I wasn’t able to find highly relevant data sources, but above are a few potentially relevant links. 👍 0 button 👎 1 button 💡 Hint: Mention @RunLLM in the thread for followups.Rob Lewis
06/20/2024, 1:40 PMRunLLM
06/20/2024, 1:40 PMConsumerEventListener directly in Key_Shared mode might not be supported or could lead to exceptions due to the way Key_Shared subscriptions handle consumer events. Instead, you can monitor the state of the consumers using a custom approach.
One alternative is to periodically check the status of the consumers using the Pulsar Admin API. Here’s how you can do it:
1. Set up the Pulsar Admin Client:
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.admin.TopicStats;
import org.apache.pulsar.common.policies.data.ConsumerStats;
import java.util.Map;
public class PulsarAdminExample {
public static void main(String[] args) throws PulsarAdminException, InterruptedException {
PulsarAdmin admin = PulsarAdmin.builder()
.serviceHttpUrl("http://localhost:8080")
.build();
String topic = "persistent://public/default/my-topic";
String subscription = "my-subscription";
while (true) {
TopicStats stats = admin.topics().getStats(topic);
Map<String, ConsumerStats> consumers = stats.getSubscriptions().get(subscription).getConsumers();
for (Map.Entry<String, ConsumerStats> entry : consumers.entrySet()) {
System.out.println("Consumer: " + entry.getKey() + ", Connected: " + entry.getValue().isConnected());
}
Thread.sleep(5000); // Check every 5 seconds
}
}
}
This code periodically checks the status of consumers subscribed to the specified topic and subscription. It prints the consumer name and connection status.
Would you like more details on any specific part of this setup?
Sources:
- Pulsar Admin API
- Pulsar Topic Stats 👍 0 button 👎 1 button 💡 Hint: Mention @RunLLM in the thread for followups.