import java.util.concurrent.TimeUnit;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.impl.schema.JSONSchema;
public class SendMsgTest {
public static void main(String[] args){
String url = "http://192.168.1.48:8080";
try{
// 第一步建立连接
PulsarClient client =PulsarClient.builder()
.serviceUrl(url)
.connectionTimeout(10,TimeUnit.SECONDS)
.build();
//第二步创建生产者对象
//指定发送数据格式(详细查看Pulsar schemas)
Producer<UserModel> producer=client.newProducer(JSONSchema.of(UserModel.class))
.topic("my-tenant/my-namespace/testschema-topic")
.sendTimeout(10,TimeUnit.SECONDS)
.producerName("senduser")
.create();
UserModel userModel=new UserModel();
userModel.setName("testmsg");
userModel.setAge(21);
producer.send(userModel);//同步发送 producer.sendAsync(userModel) 异步发送
System.out.print("send ok");
client.close();
}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;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。