Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Pulsar Consumer example

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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) / subscription mode Exclusive (exclusive, default mode) Failover (disaster recovery) Shared (sharing) .subscriptionName ("wbq_1") / / subscriber name .subscribe () While (true) {Message userModelmsg = consumer.receive (); UserModel userModel=userModelmsg.getValue (); System.out.println ("receive message:" + userModel.getName () + "=" + userModel.getAge ()); consumer.acknowledge (userModelmsg.getMessageId ()) / / the subscriber will not receive this message}} 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;}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report