一、独享模式(Exclusive)
默认情况下是独享模式,相同的suscription name的消费者不能有多个。
subscription name有点类似于kafka的消费者组。
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.*;
public class ExclusiveModeConsumer01 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic8")
.subscriptionName("my-subscription")
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.*;
public class ExclusiveModeConsumer02 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic8")
.subscriptionName("my-subscription")
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
同时运行这两个类,后启动的报错,将subscription name修改不同后就不报错了。topic为1个分区或多个分区都如此。
二、fail over模式
可以修改所有的消费者的模式为fail over,那么,当topic有一个分区时,只会有一个消费者拿到数据,当它挂了的时候其它消费者才能拿到数据,当topic有多个分区时,不同的消费者会分别拿到不同分区的数据。
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.*;
public class ExclusiveModeConsumer01 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic8")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Failover)
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.*;
public class ExclusiveModeConsumer02 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic8")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Failover)
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
三、共享模式(shared)
可以将消费类型设置为Shared,这样同一subscription name的消费者对消息进行轮询
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
public class ShareModeConsumer01 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic9")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Shared)
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
public class ShareModeConsumer02 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic9")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Shared)
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
四、key shared模式
还可以指定消费模式为key shared,当消息发送时带key,那么,相同key的消息会被同一个消费者消费
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
public class KeySharedModeConsumer01 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic12")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Key_Shared)
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
public class KeySharedModeConsumer02 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws Exception{
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Consumer consumer = client.newConsumer()
.topic("my-topic12")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Key_Shared)
.subscribe();
while (true){
Message message = consumer.receive();
try {
System.out.println("Message received: " + new String(message.getData()));
consumer.acknowledge(message);
} catch (Exception e) {
consumer.negativeAcknowledge(message);
}
}
}
}
package cn.edu.tju.test1;
import org.apache.pulsar.client.api.*;
public class KeyedProducer01 {
private static String SERVER_4 = "xx.xx.xx.xx";
public static void main(String[] args) throws PulsarClientException {
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://" + SERVER_4 + ":6650")
.build();
Producer<String> stringProducer = client.newProducer(Schema.STRING)
.topic("my-topic12")
.create();
stringProducer.newMessage().key("key1").value("message1").send();
stringProducer.newMessage().key("key2").value("message2").send();
stringProducer.newMessage().key("key3").value("message3").send();
stringProducer.newMessage().key("key1").value("message4").send();
stringProducer.newMessage().key("key3").value("message5").send();
stringProducer.newMessage().key("key2").value("message6").send();
stringProducer.close();
client.close();
}
}