本文整理汇总了Java中org.apache.pulsar.client.api.Producer.close方法的典型用法代码示例。如果您正苦于以下问题:Java Producer.close方法的具体用法?Java Producer.close怎么用?Java Producer.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pulsar.client.api.Producer
的用法示例。
在下文中一共展示了Producer.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPartitionedTopicNameWithSpecialCharacter
import org.apache.pulsar.client.api.Producer; //导入方法依赖的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: testDeleteNamespace
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
/**
* Verifies that deleteNamespace cleans up policies(global,local), bundle cache and bundle ownership
*
* @throws Exception
*/
@Test
public void testDeleteNamespace() throws Exception {
final String namespace = this.testProperty + "/use/deleteNs";
admin.namespaces().createNamespace(namespace, 100);
assertEquals(admin.namespaces().getPolicies(namespace).bundles.numBundles, 100);
// (1) Force topic creation and namespace being loaded
final String topicName = "persistent://" + namespace + "/my-topic";
DestinationName destination = DestinationName.get(topicName);
Producer producer = pulsarClient.createProducer(topicName);
producer.close();
NamespaceBundle bundle1 = pulsar.getNamespaceService().getBundle(destination);
// (2) Delete topic
admin.persistentTopics().delete(topicName);
// (3) Delete ns
admin.namespaces().deleteNamespace(namespace);
// (4) check bundle
NamespaceBundle bundle2 = pulsar.getNamespaceService().getBundle(destination);
assertNotEquals(bundle1.getBundleRange(), bundle2.getBundleRange());
// returns full bundle if policies not present
assertEquals("0x00000000_0xffffffff", bundle2.getBundleRange());
}
示例3: testNamespaceSplitBundle
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test
public void testNamespaceSplitBundle() throws Exception {
// Force to create a destination
final String namespace = "prop-xyz/use/ns1";
final String topicName = (new StringBuilder("persistent://")).append(namespace).append("/ds2").toString();
Producer producer = pulsarClient.createProducer(topicName);
producer.send("message".getBytes());
publishMessagesOnPersistentTopic(topicName, 0);
assertEquals(admin.persistentTopics().getList(namespace), Lists.newArrayList(topicName));
try {
admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0xffffffff", true);
} catch (Exception e) {
fail("split bundle shouldn't have thrown exception");
}
// bundle-factory cache must have updated split bundles
NamespaceBundles bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
String[] splitRange = { namespace + "/0x00000000_0x7fffffff", namespace + "/0x7fffffff_0xffffffff" };
for (int i = 0; i < bundles.getBundles().size(); i++) {
assertEquals(bundles.getBundles().get(i).toString(), splitRange[i]);
}
producer.close();
}
示例4: testSimpleCloseTopic
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test
public void testSimpleCloseTopic() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic5";
final String subName = "sub5";
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
PersistentSubscription subRef = topicRef.getSubscription(subName);
assertNotNull(subRef);
Message msg;
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
msg = consumer.receive();
consumer.acknowledge(msg);
}
producer.close();
consumer.close();
topicRef.close().get();
assertNull(pulsar.getBrokerService().getTopicReference(topicName));
}
示例5: testSimpleBatchProducerWithFixedBatchSizeAndTime
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test(dataProvider = "codec")
public void testSimpleBatchProducerWithFixedBatchSizeAndTime(CompressionType compressionType) throws Exception {
int numMsgs = 100;
final String topicName = "persistent://prop/use/ns-abc/testSimpleBatchProducerWithFixedBatchSizeAndTime";
final String subscriptionName = "time-size-sub-1" + compressionType.toString();
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
consumer.close();
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setBatchingMaxPublishDelay(10, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(5);
producerConf.setCompressionType(compressionType);
producerConf.setBatchingEnabled(true);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
Random random = new Random();
List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
for (int i = 0; i < numMsgs; i++) {
// put a random sleep from 0 to 3 ms
Thread.sleep(random.nextInt(4));
byte[] message = ("msg-" + i).getBytes();
Message msg = MessageBuilder.create().setContent(message).build();
sendFutureList.add(producer.sendAsync(msg));
}
FutureUtil.waitForAll(sendFutureList).get();
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
rolloverPerIntervalStats();
assertTrue(topic.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
LOG.info("Sent {} messages, backlog is {} messages", numMsgs,
topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog());
assertTrue(topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog() < numMsgs);
producer.close();
}
示例6: testTopicLoadingOnDisableNamespaceBundle
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test
public void testTopicLoadingOnDisableNamespaceBundle() throws Exception {
final String namespace = "prop/use/disableBundle";
admin.namespaces().createNamespace(namespace);
// own namespace bundle
final String topicName = "persistent://" + namespace + "/my-topic";
DestinationName destination = DestinationName.get(topicName);
Producer producer = pulsarClient.createProducer(topicName);
producer.close();
// disable namespace-bundle
NamespaceBundle bundle = pulsar.getNamespaceService().getBundle(destination);
pulsar.getNamespaceService().getOwnershipCache().updateBundleState(bundle, false);
// try to create topic which should fail as bundle is disable
CompletableFuture<Topic> futureResult = pulsar.getBrokerService().createPersistentTopic(topicName);
try {
futureResult.get();
fail("Topic creation should fail due to disable bundle");
} catch (Exception e) {
if (!(e.getCause() instanceof BrokerServiceException.ServiceUnitNotReadyException)) {
fail("Topic creation should fail with ServiceUnitNotReadyException");
}
}
}
示例7: testGC
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test
public void testGC() throws Exception {
// 1. Simple successful GC
String topicName = "persistent://prop/use/ns-abc/topic-10";
Producer producer = pulsarClient.createProducer(topicName);
producer.close();
assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));
runGC();
assertNull(pulsar.getBrokerService().getTopicReference(topicName));
// 2. Topic is not GCed with live connection
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
String subName = "sub1";
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
runGC();
assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));
// 3. Topic with subscription is not GCed even with no connections
consumer.close();
runGC();
assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));
// 4. Topic can be GCed after unsubscribe
admin.persistentTopics().deleteSubscription(topicName, subName);
runGC();
assertNull(pulsar.getBrokerService().getTopicReference(topicName));
}
示例8: testDeleteTopics
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test
public void testDeleteTopics() throws Exception {
BrokerService brokerService = pulsar.getBrokerService();
// 1. producers connect
Producer producer1 = pulsarClient.createProducer("persistent://prop/use/ns-abc/topic-1");
Producer producer2 = pulsarClient.createProducer("persistent://prop/use/ns-abc/topic-2");
brokerService.updateRates();
Map<String, NamespaceBundleStats> bundleStatsMap = brokerService.getBundleStats();
assertEquals(bundleStatsMap.size(), 1);
NamespaceBundleStats bundleStats = bundleStatsMap.get("prop/use/ns-abc/0x00000000_0xffffffff");
assertNotNull(bundleStats);
producer1.close();
admin.persistentTopics().delete("persistent://prop/use/ns-abc/topic-1");
brokerService.updateRates();
bundleStatsMap = brokerService.getBundleStats();
assertEquals(bundleStatsMap.size(), 1);
bundleStats = bundleStatsMap.get("prop/use/ns-abc/0x00000000_0xffffffff");
assertNotNull(bundleStats);
// // Delete 2nd topic as well
// producer2.close();
// admin.persistentTopics().delete("persistent://prop/use/ns-abc/topic-2");
//
// brokerService.updateRates();
//
// bundleStatsMap = brokerService.getBundleStats();
// assertEquals(bundleStatsMap.size(), 0);
}
示例9: publishMessagesOnPersistentTopic
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
private void publishMessagesOnPersistentTopic(String topicName, int messages, int startIdx) throws Exception {
Producer producer = pulsarClient.createProducer(topicName);
for (int i = startIdx; i < (messages + startIdx); i++) {
String message = "message-" + i;
producer.send(message.getBytes());
}
producer.close();
}
示例10: testSimpleBatchProducerWithFixedBatchTime
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test(dataProvider = "codec")
public void testSimpleBatchProducerWithFixedBatchTime(CompressionType compressionType) throws Exception {
int numMsgs = 100;
final String topicName = "persistent://prop/use/ns-abc/testSimpleBatchProducerWithFixedBatchTime";
final String subscriptionName = "time-sub-1" + compressionType.toString();
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
consumer.close();
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setCompressionType(compressionType);
producerConf.setBatchingMaxPublishDelay(10, TimeUnit.MILLISECONDS);
producerConf.setBatchingEnabled(true);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
Random random = new Random();
List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
for (int i = 0; i < numMsgs; i++) {
// put a random sleep from 0 to 3 ms
Thread.sleep(random.nextInt(4));
byte[] message = ("msg-" + i).getBytes();
Message msg = MessageBuilder.create().setContent(message).build();
sendFutureList.add(producer.sendAsync(msg));
}
FutureUtil.waitForAll(sendFutureList).get();
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
rolloverPerIntervalStats();
assertTrue(topic.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
LOG.info("Sent {} messages, backlog is {} messages", numMsgs,
topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog());
assertTrue(topic.getSubscription(subscriptionName).getNumberOfEntriesInBacklog() < numMsgs);
producer.close();
}
示例11: testSyncProducerAndConsumer
import org.apache.pulsar.client.api.Producer; //导入方法依赖的package包/类
@Test(dataProvider = "batch_with_timeout")
public void testSyncProducerAndConsumer(int batchMessageDelayMs, int ackTimeoutSec) throws Exception {
log.info("-- Starting {} test --", methodName);
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
// Cumulative Ack-counter works if ackTimeOutTimer-task is enabled
boolean isAckTimeoutTaskEnabledForCumulativeAck = ackTimeoutSec > 0;
if (ackTimeoutSec > 0) {
conf.setAckTimeout(ackTimeoutSec, TimeUnit.SECONDS);
}
Consumer consumer = pulsarClient.subscribe("persistent://my-property/tp1/my-ns/my-topic1", "my-subscriber-name",
conf);
ProducerConfiguration producerConf = new ProducerConfiguration();
if (batchMessageDelayMs != 0) {
producerConf.setBatchingEnabled(true);
producerConf.setBatchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(5);
}
Producer producer = pulsarClient.createProducer("persistent://my-property/tp1/my-ns/my-topic1", producerConf);
int numMessages = 11;
for (int i = 0; i < numMessages; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < numMessages; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
String receivedMessage = new String(msg.getData());
log.info("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
// Acknowledge the consumption of all messages at once
consumer.acknowledgeCumulative(msg);
Thread.sleep(2000);
consumer.close();
producer.close();
validatingLogInfo(consumer, producer, isAckTimeoutTaskEnabledForCumulativeAck);
log.info("-- Exiting {} test --", methodName);
}
示例12: 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);
}
示例13: testRoundRobinProducer
import org.apache.pulsar.client.api.Producer; //导入方法依赖的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);
}
示例14: testSinglePartitionProducer
import org.apache.pulsar.client.api.Producer; //导入方法依赖的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);
}
示例15: testAsyncPartitionedProducerConsumerQueueSizeOne
import org.apache.pulsar.client.api.Producer; //导入方法依赖的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);
}