pulsar 批量及事务消息

本文详细介绍了Pulsar的批量生产和消费机制,包括如何设置batching参数、事务消息的使用注意事项,以及如何结合事务进行混合发送。重点讲解了批量发送的最佳实践和事务模式在Pulsar中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

批量

pulsar 的批量发送与rocketmq不同,本质是client 进行缓存,根据producer创建参数自行进行控制,没有显示batch发送操作,示意代码如下:

Producer<byte[]> BatchProducer = client.newProducer().topic("mybatch-topic").batchingMaxMessages(10)
				.batchingMaxPublishDelay(10, TimeUnit.MINUTES).sendTimeout(10, TimeUnit.SECONDS).blockIfQueueFull(true)
				.enableBatching(true).create();

		// BatchProducer.

		for (int i = 0; i < 25; i++) {
			// BatchProducer.newMessage().value();
			TypedMessageBuilder<byte[]> w = BatchProducer.newMessage().key("key" + i).value(("value" + i).getBytes())
					.property("user-defined-property", "value");

			w.sendAsync().thenAccept(messageId -> {
				System.out.println("Published batch message: " + messageId);
			}).exceptionally(ex -> {
				System.err.println("Failed to publish: " + ex);
				return null;
			});
			;
		}
		//FLUSH will send all remained message to broker
		BatchProducer.flush();

要注意几点
1、设置合理批量发送数量及最大间隔发送时间(batchingMaxPublishDelay)
2、适当考虑调用flush,以保障未达到batchingMaxMessages还在队列中的消息都发送给broker

具体批量发送的逻辑可以看ProducerImpl类,多个函数都有批量考虑,如private void serializeAndSendMessage。
实际批量发送函数为 doBatchSendAndAdd(msg, callback, payload)

事务消息

事务消息不能与批量同时使用,事务有超时控制机制,创建代码如下:

Transaction txn = client.newTransaction().withTransactionTimeout(1, TimeUnit.MINUTES).build().get();

如果使用了batch模式并设置了debug,可能有如下信息不断输出

2021-11-23 16:08:01.791  INFO 1040 --- [r-client-io-1-1] o.a.pulsar.client.impl.ProducerImpl      : [mybatch-topic] [standalone-0-6] Batching the messages from the batch container with 0 messages

同时消息不能设置超时时间,如果设置了可能有如下错误:

Only producers disabled sendTimeout are allowed to produce transactional

事务消息可以混杂立即发送和延迟发送消息,完整的发送事务代码如下:

// BatchProducer.
		Transaction txn = client.newTransaction().withTransactionTimeout(1, TimeUnit.MINUTES).build().get();
		// Only producers disabled sendTimeout are allowed to produce transactional
		// messages
		for (int i = 0; i < 25; i++) {
			// BatchProducer.newMessage().value();
			TypedMessageBuilder<byte[]> w ;
			if (i %2 ==0) {
			w=BatchProducer.newMessage(txn).key("key" + i).value(("value" + i).getBytes())
					.property("user-defined-property", "value");
			}else {
				w=BatchProducer.newMessage(txn).deliverAfter(100, TimeUnit.SECONDS).key("key" + i).value(("value" + i).getBytes())
						.property("user-defined-property", "value");
			}
			w.sendAsync().thenAccept(messageId -> {
				System.out.println("Published batch message: " + messageId);
			}).exceptionally(ex -> {
				System.err.println("Failed to publish: " + ex);
				return null;
			});
			 
		}
		}
		// BatchProducer.flush();
		txn.commit();

消费侧事务

pulsar消费也支持事务模式,官方文档给的示意是可以完成多个topic的消费后提交,以下是一个简单批量消费后一起提交的示意代码

Consumer consumer = client.newConsumer().topic("mybatch-topic").subscriptionName("my-subscription")
				.subscriptionType(SubscriptionType.Key_Shared).batchReceivePolicy(BatchReceivePolicy.builder()
						.maxNumMessages(5).maxNumBytes(1024 * 1024).timeout(200, TimeUnit.MILLISECONDS).build())
				.subscribe();

		//Messages m = consumer.batchReceive();
		int i=0;
		Transaction txn = client.newTransaction().withTransactionTimeout(1, TimeUnit.MINUTES).build().get();

		while (true) {
      	  // Wait for a message
      	  Message msg = consumer.receive();

      	  try {
      	      // Do something with the message
      	      System.out.println("Message received: " + new String(msg.getData()));

      	      // Acknowledge the message so that it can be deleted by the message broker
      	      consumer.acknowledgeCumulativeAsync(msg.getMessageId(),txn);
      	  } catch (Exception e) {
      	      // Message failed to process, redeliver later
      	      consumer.negativeAcknowledge(msg);
      	  }
      	  i++;
      	  if (i>=5) {
      		  txn.commit();
      		  txn = client.newTransaction().withTransactionTimeout(1, TimeUnit.MINUTES).build().get();

      		  i=0;
      	  }
      	  
      	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40455124

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值