PulsarConsumer例子-成都快上网建站

PulsarConsumer例子


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.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.impl.schema.JSONSchema;
public class ReceiveMsgTest {
    public static void main(String[] args) {
    String url = "http://192.168.1.48:8080";        
        try{
            PulsarClient client =PulsarClient.builder()
                    .serviceUrl(url)                    
                    .build();
             Consumer consumer=client.newConsumer(JSONSchema.of(UserModel.class))
             .topic("my-tenant/my-namespace/testschema-topic")           
             .subscriptionType(SubscriptionType.Exclusive)//订阅模式  Exclusive(独占,默认模式) Failover(灾备)Shared(共享)
             .subscriptionName("wbq_1")//订阅者名称
             .subscribe();
             while (true) {
                Message userModelmsg = consumer.receive();
                UserModel userModel=userModelmsg.getValue();
                System.out.println("receive message: " +userModel.getName()+"="+userModel.getAge());
                consumer.acknowledge(userModelmsg.getMessageId());//应答后此订阅者不会在收到此消息
             }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

public class UserModel {

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

网站题目:PulsarConsumer例子
本文来源:http://kswjz.com/article/pgodcc.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流