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<UserModel> 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<UserModel> 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例子
浏览地址:https://www.cdcxhl.com/article22/pgodcc.html

成都网站建设公司_创新互联,为您提供企业建站搜索引擎优化小程序开发动态网站响应式网站商城网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营