pulsar两种消息发送策略,同步发送与异步发送
生产者producer
同步发送模式
楼主创建的producer数据类型是
{ this.producer = client.newProducer(Schema.BYTES) }
同步发送
MessageId messageId = producer.send( str1.getBytes() );
异步发送
方式一,只管发
CompletableFuture future = producer.sendAsync(str1.getBytes());
方式二,确认发送成功
List<CompletableFuture> futures = Lists.newArrayList();
for( int i = 1 ; i <num ; i++ ){
str1 = df.format( new Date() ) + “\t” + i + “||” ;
CompletableFuture future = this.producer.sendAsync(str1.getBytes());
future.handle((v, ex) -> {
if (ex == null) {
// System.out.print(“Message persisted: {}”);
} else {
System.out.println( “Error persisting message: {}”);
log.error(“Error persisting message: {}”, ex);
}
ret