本文整理汇总了Java中org.apache.pulsar.client.api.ProducerConfiguration.setMessageRoutingMode方法的典型用法代码示例。如果您正苦于以下问题:Java ProducerConfiguration.setMessageRoutingMode方法的具体用法?Java ProducerConfiguration.setMessageRoutingMode怎么用?Java ProducerConfiguration.setMessageRoutingMode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pulsar.client.api.ProducerConfiguration
的用法示例。
在下文中一共展示了ProducerConfiguration.setMessageRoutingMode方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPartitionedTopicNameWithSpecialCharacter
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test(timeOut = 30000)
public void testPartitionedTopicNameWithSpecialCharacter() throws Exception {
log.info("-- Starting {} test --", methodName);
int numPartitions = 4;
final String specialCharacter = "! * ' ( ) ; : @ & = + $ , /\\ ? % # [ ]";
final String topicName = "my-partitionedtopic1" + specialCharacter;
DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/" + topicName);
admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
// Try to create producer which does lookup and create connection with broker
Producer producer = pulsarClient.createProducer(dn.toString(), producerConf);
producer.close();
admin.persistentTopics().deletePartitionedTopic(dn.toString());
log.info("-- Exiting {} test --", methodName);
}
示例2: testPartitions
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test
public void testPartitions() throws Exception {
admin.properties().createProperty("sample", new PropertyAdmin());
PulsarClient client = PulsarClient.create("pulsar://localhost:" + proxyConfig.getServicePort());
admin.persistentTopics().createPartitionedTopic("persistent://sample/test/local/partitioned-topic", 2);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = client.createProducer("persistent://sample/test/local/partitioned-topic", producerConf);
// Create a consumer directly attached to broker
Consumer consumer = pulsarClient.subscribe("persistent://sample/test/local/partitioned-topic", "my-sub");
for (int i = 0; i < 10; i++) {
producer.send("test".getBytes());
}
for (int i = 0; i < 10; i++) {
Message msg = consumer.receive(1, TimeUnit.SECONDS);
checkNotNull(msg);
}
client.close();
}
示例3: testPartitions
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test
public void testPartitions() throws Exception {
ClientConfiguration conf = new ClientConfiguration();
conf.setUseTls(true);
conf.setTlsAllowInsecureConnection(false);
conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
PulsarClient client = PulsarClient.create("pulsar://localhost:" + proxyConfig.getServicePortTls(), conf);
admin.properties().createProperty("sample", new PropertyAdmin());
admin.persistentTopics().createPartitionedTopic("persistent://sample/test/local/partitioned-topic", 2);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = client.createProducer("persistent://sample/test/local/partitioned-topic", producerConf);
// Create a consumer directly attached to broker
Consumer consumer = pulsarClient.subscribe("persistent://sample/test/local/partitioned-topic", "my-sub");
for (int i = 0; i < 10; i++) {
producer.send("test".getBytes());
}
for (int i = 0; i < 10; i++) {
Message msg = consumer.receive(1, TimeUnit.SECONDS);
checkNotNull(msg);
}
client.close();
}
示例4: LoadSimulationClient
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
/**
* Create a LoadSimulationClient with the given JCommander arguments.
*
* @param arguments
* Arguments to configure this from.
*/
public LoadSimulationClient(final MainArguments arguments) throws Exception {
payloadCache = new ConcurrentHashMap<>();
topicsToTradeUnits = new ConcurrentHashMap<>();
clientConf = new ClientConfiguration();
clientConf.setConnectionsPerBroker(4);
clientConf.setIoThreads(Runtime.getRuntime().availableProcessors());
// Disable stats on the clients to reduce CPU/memory usage.
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
producerConf = new ProducerConfiguration();
// Disable timeout.
producerConf.setSendTimeout(0, TimeUnit.SECONDS);
producerConf.setMessageRoutingMode(ProducerConfiguration.MessageRoutingMode.RoundRobinPartition);
// Enable batching.
producerConf.setBatchingMaxPublishDelay(1, TimeUnit.MILLISECONDS);
producerConf.setBatchingEnabled(true);
consumerConf = new ConsumerConfiguration();
consumerConf.setMessageListener(ackListener);
admin = new PulsarAdmin(new URL(arguments.serviceURL), clientConf);
client = new PulsarClientImpl(arguments.serviceURL, clientConf);
port = arguments.port;
executor = Executors.newCachedThreadPool(new DefaultThreadFactory("test-client"));
}
示例5: getProducerConfiguration
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
private ProducerConfiguration getProducerConfiguration() {
ProducerConfiguration conf = new ProducerConfiguration();
// Set to false to prevent the server thread from being blocked if a lot of messages are pending.
conf.setBlockIfQueueFull(false);
if (queryParams.containsKey("sendTimeoutMillis")) {
conf.setSendTimeout(Integer.parseInt(queryParams.get("sendTimeoutMillis")), TimeUnit.MILLISECONDS);
}
if (queryParams.containsKey("batchingEnabled")) {
conf.setBatchingEnabled(Boolean.parseBoolean(queryParams.get("batchingEnabled")));
}
if (queryParams.containsKey("batchingMaxMessages")) {
conf.setBatchingMaxMessages(Integer.parseInt(queryParams.get("batchingMaxMessages")));
}
if (queryParams.containsKey("maxPendingMessages")) {
conf.setMaxPendingMessages(Integer.parseInt(queryParams.get("maxPendingMessages")));
}
if (queryParams.containsKey("batchingMaxPublishDelay")) {
conf.setBatchingMaxPublishDelay(Integer.parseInt(queryParams.get("batchingMaxPublishDelay")),
TimeUnit.MILLISECONDS);
}
if (queryParams.containsKey("messageRoutingMode")) {
checkArgument(
Enums.getIfPresent(MessageRoutingMode.class, queryParams.get("messageRoutingMode")).isPresent(),
"Invalid messageRoutingMode %s", queryParams.get("messageRoutingMode"));
MessageRoutingMode routingMode = MessageRoutingMode.valueOf(queryParams.get("messageRoutingMode"));
if (!MessageRoutingMode.CustomPartition.equals(routingMode)) {
conf.setMessageRoutingMode(routingMode);
}
}
if (queryParams.containsKey("compressionType")) {
checkArgument(Enums.getIfPresent(CompressionType.class, queryParams.get("compressionType")).isPresent(),
"Invalid compressionType %s", queryParams.get("compressionType"));
conf.setCompressionType(CompressionType.valueOf(queryParams.get("compressionType")));
}
return conf;
}
示例6: testRoundRobinProducer
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test(timeOut = 30000)
public void testRoundRobinProducer() throws Exception {
log.info("-- Starting {} test --", methodName);
int numPartitions = 4;
DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/my-partitionedtopic1");
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = pulsarClient.createProducer(dn.toString(), producerConf);
Consumer consumer = pulsarClient.subscribe(dn.toString(), "my-partitioned-subscriber", conf);
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < 10; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
Assert.assertNotNull(msg, "Message should not be null");
consumer.acknowledge(msg);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
Assert.assertTrue(messageSet.add(receivedMessage), "Message " + receivedMessage + " already received");
}
producer.close();
consumer.unsubscribe();
consumer.close();
admin.persistentTopics().deletePartitionedTopic(dn.toString());
log.info("-- Exiting {} test --", methodName);
}
示例7: testSinglePartitionProducer
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test(timeOut = 30000)
public void testSinglePartitionProducer() throws Exception {
log.info("-- Starting {} test --", methodName);
int numPartitions = 4;
DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/my-partitionedtopic2");
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.SinglePartition);
Producer producer = pulsarClient.createProducer(dn.toString(), producerConf);
Consumer consumer = pulsarClient.subscribe(dn.toString(), "my-partitioned-subscriber", conf);
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < 10; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
Assert.assertNotNull(msg, "Message should not be null");
consumer.acknowledge(msg);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
producer.close();
consumer.unsubscribe();
consumer.close();
admin.persistentTopics().deletePartitionedTopic(dn.toString());
log.info("-- Exiting {} test --", methodName);
}
示例8: testAsyncPartitionedProducerConsumer
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test(timeOut = 30000)
public void testAsyncPartitionedProducerConsumer() throws Exception {
log.info("-- Starting {} test --", methodName);
final int totalMsg = 100;
final Set<String> produceMsgs = Sets.newHashSet();
final Set<String> consumeMsgs = Sets.newHashSet();
int numPartitions = 4;
DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/my-partitionedtopic1");
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Shared);
admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = pulsarClient.createProducer(dn.toString(), producerConf);
Consumer consumer = pulsarClient.subscribe(dn.toString(), "my-partitioned-subscriber", conf);
// produce messages
for (int i = 0; i < totalMsg; i++) {
String message = "my-message-" + i;
produceMsgs.add(message);
producer.send(message.getBytes());
}
log.info(" start receiving messages :");
// receive messages
CountDownLatch latch = new CountDownLatch(totalMsg);
receiveAsync(consumer, totalMsg, 0, latch, consumeMsgs, executor);
latch.await();
// verify message produced correctly
assertEquals(produceMsgs.size(), totalMsg);
// verify produced and consumed messages must be exactly same
produceMsgs.removeAll(consumeMsgs);
assertTrue(produceMsgs.isEmpty());
producer.close();
consumer.unsubscribe();
consumer.close();
admin.persistentTopics().deletePartitionedTopic(dn.toString());
log.info("-- Exiting {} test --", methodName);
}
示例9: testAsyncPartitionedProducerConsumerQueueSizeOne
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test(timeOut = 30000)
public void testAsyncPartitionedProducerConsumerQueueSizeOne() throws Exception {
log.info("-- Starting {} test --", methodName);
final int totalMsg = 100;
final Set<String> produceMsgs = Sets.newHashSet();
final Set<String> consumeMsgs = Sets.newHashSet();
int numPartitions = 4;
DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/my-partitionedtopic1");
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setReceiverQueueSize(1);
admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = pulsarClient.createProducer(dn.toString(), producerConf);
Consumer consumer = pulsarClient.subscribe(dn.toString(), "my-partitioned-subscriber", conf);
// produce messages
for (int i = 0; i < totalMsg; i++) {
String message = "my-message-" + i;
produceMsgs.add(message);
producer.send(message.getBytes());
}
log.info(" start receiving messages :");
// receive messages
CountDownLatch latch = new CountDownLatch(totalMsg);
receiveAsync(consumer, totalMsg, 0, latch, consumeMsgs, executor);
latch.await();
// verify message produced correctly
assertEquals(produceMsgs.size(), totalMsg);
// verify produced and consumed messages must be exactly same
produceMsgs.removeAll(consumeMsgs);
assertTrue(produceMsgs.isEmpty());
producer.close();
consumer.unsubscribe();
consumer.close();
admin.persistentTopics().deletePartitionedTopic(dn.toString());
log.info("-- Exiting {} test --", methodName);
}
示例10: testFairDistributionForPartitionConsumers
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
/**
* It verifies that consumer consumes from all the partitions fairly.
*
* @throws Exception
*/
@Test(timeOut = 30000)
public void testFairDistributionForPartitionConsumers() throws Exception {
log.info("-- Starting {} test --", methodName);
final int numPartitions = 2;
final String topicName = "persistent://my-property/use/my-ns/my-topic";
final String producer1Msg = "producer1";
final String producer2Msg = "producer2";
final int queueSize = 10;
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setReceiverQueueSize(queueSize);
admin.persistentTopics().createPartitionedTopic(topicName, numPartitions);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer1 = pulsarClient.createProducer(topicName + "-partition-0", producerConf);
Producer producer2 = pulsarClient.createProducer(topicName + "-partition-1", producerConf);
Consumer consumer = pulsarClient.subscribe(topicName, "my-partitioned-subscriber", conf);
int partition2Msgs = 0;
// produce messages on Partition-1: which will makes partitioned-consumer's queue full
for (int i = 0; i < queueSize - 1; i++) {
producer1.send((producer1Msg + "-" + i).getBytes());
}
Thread.sleep(1000);
// now queue is full : so, partition-2 consumer will be pushed to paused-consumer list
for (int i = 0; i < 5; i++) {
producer2.send((producer2Msg + "-" + i).getBytes());
}
// now, Queue should take both partition's messages
// also: we will keep producing messages to partition-1
int produceMsgInPartition1AfterNumberOfConsumeMessages = 2;
for (int i = 0; i < 3 * queueSize; i++) {
Message msg = consumer.receive();
partition2Msgs += (new String(msg.getData())).startsWith(producer2Msg) ? 1 : 0;
if (i >= produceMsgInPartition1AfterNumberOfConsumeMessages) {
producer1.send(producer1Msg.getBytes());
Thread.sleep(100);
}
}
assertTrue(partition2Msgs >= 4);
producer1.close();
producer2.close();
consumer.unsubscribe();
consumer.close();
admin.persistentTopics().deletePartitionedTopic(topicName);
log.info("-- Exiting {} test --", methodName);
}
示例11: testSharedSingleAckedPartitionedTopic
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test(timeOut = testTimeout)
public void testSharedSingleAckedPartitionedTopic() throws Exception {
String key = "testSharedSingleAckedPartitionedTopic";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-shared-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 20;
final int numberOfPartitions = 3;
admin.properties().createProperty("prop", new PropertyAdmin());
admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
// Special step to create partitioned topic
// 1. producer connect
ProducerConfiguration prodConfig = new ProducerConfiguration();
prodConfig.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = pulsarClient.createProducer(topicName, prodConfig);
// 2. Create consumer
ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
consumerConfig.setReceiverQueueSize(100);
consumerConfig.setSubscriptionType(SubscriptionType.Shared);
consumerConfig.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
consumerConfig.setConsumerName("Consumer-1");
Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
consumerConfig.setConsumerName("Consumer-2");
Consumer consumer2 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
MessageId msgId = producer.send(message.getBytes());
log.info("Message produced: {} -- msgId: {}", message, msgId);
}
// 4. Receive messages
int messageCount1 = receiveAllMessage(consumer1, false /* no-ack */);
int messageCount2 = receiveAllMessage(consumer2, true /* yes-ack */);
int ackCount1 = 0;
int ackCount2 = messageCount2;
log.info(key + " messageCount1 = " + messageCount1);
log.info(key + " messageCount2 = " + messageCount2);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(messageCount1 + messageCount2, totalMessages);
// 5. Check if Messages redelivered again
// Since receive is a blocking call hoping that timeout will kick in
Thread.sleep((int) (ackTimeOutMillis * 1.1));
log.info(key + " Timeout should be triggered now");
messageCount1 = receiveAllMessage(consumer1, true);
messageCount2 += receiveAllMessage(consumer2, false);
ackCount1 = messageCount1;
log.info(key + " messageCount1 = " + messageCount1);
log.info(key + " messageCount2 = " + messageCount2);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(messageCount1 + messageCount2, totalMessages);
assertEquals(ackCount1 + messageCount2, totalMessages);
Thread.sleep((int) (ackTimeOutMillis * 1.1));
// Since receive is a blocking call hoping that timeout will kick in
log.info(key + " Timeout should be triggered again");
ackCount1 += receiveAllMessage(consumer1, true);
ackCount2 += receiveAllMessage(consumer2, true);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(ackCount1 + ackCount2, totalMessages);
}
示例12: testPersistentTopicExpireMessageOnParitionTopic
import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
/**
* Verify: PersistentTopics.expireMessages()/expireMessagesForAllSubscriptions() for PartitionTopic
*
* @throws Exception
*/
@Test
public void testPersistentTopicExpireMessageOnParitionTopic() throws Exception {
admin.persistentTopics().createPartitionedTopic("persistent://prop-xyz/use/ns1/ds1", 4);
// create consumer and subscription
URL pulsarUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
PulsarClient client = PulsarClient.create(pulsarUrl.toString(), clientConf);
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = client.subscribe("persistent://prop-xyz/use/ns1/ds1", "my-sub", conf);
ProducerConfiguration prodConf = new ProducerConfiguration();
prodConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = client.createProducer("persistent://prop-xyz/use/ns1/ds1", prodConf);
for (int i = 0; i < 10; i++) {
String message = "message-" + i;
producer.send(message.getBytes());
}
PartitionedTopicStats topicStats = admin.persistentTopics()
.getPartitionedStats("persistent://prop-xyz/use/ns1/ds1", true);
assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 10);
PersistentTopicStats partitionStatsPartition0 = topicStats.partitions
.get("persistent://prop-xyz/use/ns1/ds1-partition-0");
PersistentTopicStats partitionStatsPartition1 = topicStats.partitions
.get("persistent://prop-xyz/use/ns1/ds1-partition-1");
assertEquals(partitionStatsPartition0.subscriptions.get("my-sub").msgBacklog, 3, 1);
assertEquals(partitionStatsPartition1.subscriptions.get("my-sub").msgBacklog, 3, 1);
Thread.sleep(1000);
admin.persistentTopics().expireMessagesForAllSubscriptions("persistent://prop-xyz/use/ns1/ds1", 1);
Thread.sleep(1000);
topicStats = admin.persistentTopics().getPartitionedStats("persistent://prop-xyz/use/ns1/ds1", true);
partitionStatsPartition0 = topicStats.partitions.get("persistent://prop-xyz/use/ns1/ds1-partition-0");
partitionStatsPartition1 = topicStats.partitions.get("persistent://prop-xyz/use/ns1/ds1-partition-1");
assertEquals(partitionStatsPartition0.subscriptions.get("my-sub").msgBacklog, 0);
assertEquals(partitionStatsPartition1.subscriptions.get("my-sub").msgBacklog, 0);
producer.close();
consumer.close();
client.close();
}