本文整理汇总了Java中org.apache.pulsar.client.api.Producer.sendAsync方法的典型用法代码示例。如果您正苦于以下问题:Java Producer.sendAsync方法的具体用法?Java Producer.sendAsync怎么用?Java Producer.sendAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pulsar.client.api.Producer
的用法示例。
在下文中一共展示了Producer.sendAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBatchMessagesRateOut
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
public void testBatchMessagesRateOut() throws PulsarClientException, InterruptedException, PulsarAdminException {
log.info("-- Starting {} test --", methodName);
String topicName = "persistent://my-property/cluster/my-ns/testBatchMessagesRateOut";
double produceRate = 17;
int batchSize = 5;
ConsumerConfiguration consumerConf = new ConsumerConfiguration();
consumerConf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, "my-subscriber-name", consumerConf);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setBatchingMaxMessages(batchSize);
producerConf.setBatchingEnabled(true);
producerConf.setBatchingMaxPublishDelay(2, TimeUnit.SECONDS);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
AtomicBoolean runTest = new AtomicBoolean(true);
Thread t1 = new Thread(() -> {
RateLimiter r = RateLimiter.create(produceRate);
while (runTest.get()) {
r.acquire();
producer.sendAsync("Hello World".getBytes());
consumer.receiveAsync().thenAccept(message -> consumer.acknowledgeAsync(message));
}
});
t1.start();
Thread.sleep(2000); // Two seconds sleep
runTest.set(false);
pulsar.getBrokerService().updateRates();
double actualRate = admin.persistentTopics().getStats(topicName).msgRateOut;
assertTrue(actualRate > (produceRate / batchSize));
consumer.unsubscribe();
log.info("-- Exiting {} test --", methodName);
}
示例2: testPartitionKey
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test(timeOut = 10000)
public void testPartitionKey() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/testPartitionKey";
org.apache.pulsar.client.api.Consumer consumer = pulsarClient.subscribe(topicName, "my-subscription");
// 1. producer with batch enabled
ProducerConfiguration conf = new ProducerConfiguration();
conf.setBatchingEnabled(true);
Producer producerWithBatches = pulsarClient.createProducer(topicName, conf);
// 2. Producer without batches
Producer producerWithoutBatches = pulsarClient.createProducer(topicName);
producerWithBatches.sendAsync(MessageBuilder.create().setKey("key-1").setContent("msg-1".getBytes()).build());
producerWithBatches.sendAsync(MessageBuilder.create().setKey("key-2").setContent("msg-2".getBytes()).build())
.get();
producerWithoutBatches
.sendAsync(MessageBuilder.create().setKey("key-3").setContent("msg-3".getBytes()).build());
for (int i = 1; i <= 3; i++) {
Message msg = consumer.receive();
assertTrue(msg.hasKey());
assertEquals(msg.getKey(), "key-" + i);
assertEquals(msg.getData(), ("msg-" + i).getBytes());
consumer.acknowledge(msg);
}
}
示例3: main
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
PulsarClient pulsarClient = PulsarClient.create("http://127.0.0.1:8080");
Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic");
while (true) {
producer.sendAsync("my-message".getBytes());
}
}
示例4: testSendTimeout
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test(dataProvider = "batch")
public void testSendTimeout(int batchMessageDelayMs) throws Exception {
log.info("-- Starting {} test --", methodName);
ConsumerConfiguration consumerConf = new ConsumerConfiguration();
consumerConf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe("persistent://my-property/tp1/my-ns/my-topic5", "my-subscriber-name",
consumerConf);
ProducerConfiguration producerConf = new ProducerConfiguration();
if (batchMessageDelayMs != 0) {
producerConf.setBatchingMaxPublishDelay(2 * batchMessageDelayMs, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(5);
producerConf.setBatchingEnabled(true);
}
producerConf.setSendTimeout(1, TimeUnit.SECONDS);
Producer producer = pulsarClient.createProducer("persistent://my-property/tp1/my-ns/my-topic5", producerConf);
final String message = "my-message";
// Trigger the send timeout
stopBroker();
Future<MessageId> future = producer.sendAsync(message.getBytes());
try {
future.get();
fail("Send operation should have failed");
} catch (ExecutionException e) {
// Expected
}
startBroker();
// We should not have received any message
Message msg = consumer.receive(3, TimeUnit.SECONDS);
assertNull(msg);
consumer.close();
producer.close();
Thread.sleep(1000);
ConsumerStats cStat = consumer.getStats();
ProducerStats pStat = producer.getStats();
assertEquals(pStat.getTotalMsgsSent(), 0);
assertEquals(pStat.getTotalSendFailed(), 1);
assertEquals(cStat.getTotalMsgsReceived(), 0);
assertEquals(cStat.getTotalMsgsReceived(), cStat.getTotalAcksSent());
log.info("-- Exiting {} test --", methodName);
}
示例5: testPayloadCorruptionDetection
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test
public void testPayloadCorruptionDetection() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic1";
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
Consumer consumer = pulsarClient.subscribe(topicName, "my-sub");
Message msg1 = MessageBuilder.create().setContent("message-1".getBytes()).build();
CompletableFuture<MessageId> future1 = producer.sendAsync(msg1);
// Stop the broker, and publishes messages. Messages are accumulated in the producer queue and they're checksums
// would have already been computed. If we change the message content at that point, it should result in a
// checksum validation error
stopBroker();
Message msg2 = MessageBuilder.create().setContent("message-2".getBytes()).build();
CompletableFuture<MessageId> future2 = producer.sendAsync(msg2);
// Taint msg2
msg2.getData()[msg2.getData().length - 1] = '3'; // new content would be 'message-3'
// Restart the broker to have the messages published
startBroker();
future1.get();
try {
future2.get();
fail("since we corrupted the message, it should be rejected by the broker");
} catch (Exception e) {
// ok
}
// We should only receive msg1
Message msg = consumer.receive(1, TimeUnit.SECONDS);
assertEquals(new String(msg.getData()), "message-1");
while ((msg = consumer.receive(1, TimeUnit.SECONDS)) != null) {
assertEquals(new String(msg.getData()), "message-1");
}
}