您好,登錄后才能下訂單哦!
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();
//第二步創(chuàng)建生產(chǎn)者對(duì)象
//指定發(fā)送數(shù)據(jù)格式(詳細(xì)查看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);//同步發(fā)送 producer.sendAsync(userModel) 異步發(fā)送
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;
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。