脉冲星生产商例子

  
 <代码>
  进口java.util.concurrent.TimeUnit;
  进口org.apache.pulsar.client.api.Producer;
  进口org.apache.pulsar.client.api.PulsarClient;
  进口org.apache.pulsar.client.impl.schema.JSONSchema;
  
  公开课SendMsgTest {
  公共静态void main (String [] args) {
  字符串url=" http://192.168.1.48:8080 ";
  尝试{//第一步建立连接
  PulsarClient客户=PulsarClient.builder ()
  .serviceUrl (url)
  TimeUnit.SECONDS .connectionTimeout (10)
  .build ();//第二步创建生产者对象//指定发送数据格式(详细查看脉冲星模式)
  Producer制片人=client.newProducer (JSONSchema.of (UserModel.class))
  .topic(“我的租客/my-namespace testschema-topic”)
  TimeUnit.SECONDS .sendTimeout (10)
  .producerName (“senduser”)
  共创();
  
  UserModel UserModel=new UserModel ();
  userModel.setName (“testmsg”);
  userModel.setAge (21);
  producer.send (userModel);//同步发送producer.sendAsync (userModel)异步发送
  system . out。打印(“发送ok”);
  client.close ();
  }捕捉(异常e) {
  e.printStackTrace ();
  }
  }
  } 
  公共类UserModel {

  
 <代码>私人字符串名称;
  
  私人int年龄;
  
  公共字符串getName () {
  返回名称;
  }
  
  公共空间setName(字符串名称){
  this.name=名称;
  }
  
  公共int getAge () {
  返回年龄;
  }
  
  公共空间setAge (int年龄){
  这一点。年龄=年龄;
  } 
  

}

脉冲星生产商例子