Java Code Examples for org.apache.pulsar.client.api.Producer#close()
The following examples show how to use
org.apache.pulsar.client.api.Producer#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChecksumTest.java From pulsar with Apache License 2.0 | 6 votes |
@Test public void verifyChecksumSentToConsumer() throws Exception { final String topicName = "persistent://prop/use/ns-abc/topic-1"; Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create(); RawReader reader = RawReader.create(pulsarClient, topicName, "sub").get(); producer.send("Hello".getBytes()); RawMessage msg = reader.readNextAsync().get(); ByteBuf b = msg.getHeadersAndPayload(); assertTrue(Commands.hasChecksum(b)); int parsedChecksum = Commands.readChecksum(b); int computedChecksum = Crc32cIntChecksum.computeChecksum(b); assertEquals(parsedChecksum, computedChecksum); producer.close(); reader.closeAsync().get(); }
Example 2
Source File: AdminApiTest.java From pulsar with Apache License 2.0 | 6 votes |
private void publishMessagesOnPersistentTopic(String topicName, int messages, int startIdx, boolean nullValue) throws Exception { Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES) .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = startIdx; i < (messages + startIdx); i++) { if (nullValue) { producer.send(null); } else { String message = "message-" + i; producer.send(message.getBytes()); } } producer.close(); }
Example 3
Source File: AdminApiTest2.java From pulsar with Apache License 2.0 | 6 votes |
@Test public void testTenantNameWithUnderscore() throws Exception { TenantInfo tenantInfo = new TenantInfo(Sets.newHashSet("role1", "role2"), Sets.newHashSet("test")); admin.tenants().createTenant("prop_xyz", tenantInfo); admin.namespaces().createNamespace("prop_xyz/my-namespace", Sets.newHashSet("test")); String topic = "persistent://prop_xyz/use/my-namespace/my-topic"; Producer<byte[]> producer = pulsarClient.newProducer().topic(topic) .create(); TopicStats stats = admin.topics().getStats(topic); assertEquals(stats.publishers.size(), 1); producer.close(); }
Example 4
Source File: CLITest.java From pulsar with Apache License 2.0 | 5 votes |
private void testPublishAndConsume(String topic, String sub, Schema type) throws PulsarClientException { PulsarClient client = PulsarClient.builder().serviceUrl(pulsarCluster.getPlainTextServiceUrl()).build(); Producer<Tick> producer = client.newProducer(type) .topic(topic + "-message") .create(); Consumer<Tick> consumer = client.newConsumer(type) .topic(topic + "-message") .subscriptionName(sub) .subscribe(); final int numOfMessages = 10; for (int i = 1; i < numOfMessages; ++i) { producer.send(new Tick(i, "Stock_" + i, 100 + i, 110 + i)); } for (int i = 1; i < numOfMessages; ++i) { Tick expected = new Tick(i, "Stock_" + i, 100 + i, 110 + i); Message<Tick> receive = consumer.receive(5, TimeUnit.SECONDS); Assert.assertEquals(receive.getValue(), expected); } producer.close(); consumer.close(); client.close(); }
Example 5
Source File: PulsarMultiHostClientTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testGetPartitionedTopicMetaData() throws Exception { log.info("-- Starting {} test --", methodName); final String topicName = "persistent://my-property/my-ns/my-topic1"; final String subscriptionName = "my-subscriber-name"; try { String url = pulsar.getWebServiceAddress(); if (isTcpLookup) { url = pulsar.getBrokerServiceUrl(); } PulsarClient client = newPulsarClient(url, 0); Consumer<byte[]> consumer = client.newConsumer().topic(topicName).subscriptionName(subscriptionName) .acknowledgmentGroupTime(0, TimeUnit.SECONDS).subscribe(); Producer<byte[]> producer = client.newProducer().topic(topicName).create(); consumer.close(); producer.close(); client.close(); } catch (PulsarClientException pce) { log.error("create producer or consumer error: ", pce); fail(); } log.info("-- Exiting {} test --", methodName); }
Example 6
Source File: AdminApiTest2.java From pulsar with Apache License 2.0 | 5 votes |
private void publishMessagesOnPersistentTopic(String topicName, int messages, int startIdx) throws Exception { Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = startIdx; i < (messages + startIdx); i++) { String message = "message-" + i; producer.send(message.getBytes()); } producer.close(); }
Example 7
Source File: V1_AdminApiTest2.java From pulsar with Apache License 2.0 | 5 votes |
private void publishMessagesOnPersistentTopic(String topicName, int messages, int startIdx) throws Exception { Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = startIdx; i < (messages + startIdx); i++) { String message = "message-" + i; producer.send(message.getBytes()); } producer.close(); }
Example 8
Source File: V1_AdminApiTest2.java From pulsar with Apache License 2.0 | 5 votes |
private void publishMessagesOnTopic(String topicName, int messages, int startIdx) throws Exception { Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = startIdx; i < (messages + startIdx); i++) { String message = "message-" + i; producer.send(message.getBytes()); } producer.close(); }
Example 9
Source File: PrometheusMetricsTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testManagedLedgerCacheStats() throws Exception { Producer<byte[]> p1 = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic1").create(); Producer<byte[]> p2 = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic2").create(); for (int i = 0; i < 10; i++) { String message = "my-message-" + i; p1.send(message.getBytes()); p2.send(message.getBytes()); } ByteArrayOutputStream statsOut = new ByteArrayOutputStream(); PrometheusMetricsGenerator.generate(pulsar, false, false, statsOut); String metricsStr = new String(statsOut.toByteArray()); Multimap<String, Metric> metrics = parseMetrics(metricsStr); metrics.entries().forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()) ); List<Metric> cm = (List<Metric>) metrics.get("pulsar_ml_cache_evictions"); assertEquals(cm.size(), 1); assertEquals(cm.get(0).tags.get("cluster"), "test"); cm = (List<Metric>) metrics.get("pulsar_ml_cache_hits_rate"); assertEquals(cm.size(), 1); assertEquals(cm.get(0).tags.get("cluster"), "test"); p1.close(); p2.close(); }
Example 10
Source File: PersistentTopicE2ETest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testSimpleCloseTopic() throws Exception { final String topicName = "persistent://prop/ns-abc/topic5"; final String subName = "sub5"; Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe(); Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get(); assertNotNull(topicRef); PersistentSubscription subRef = topicRef.getSubscription(subName); assertNotNull(subRef); Message<byte[]> 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(); assertFalse(pulsar.getBrokerService().getTopicReference(topicName).isPresent()); }
Example 11
Source File: V1_AdminApiTest.java From pulsar with Apache License 2.0 | 4 votes |
/** * Verify: PersistentTopicsBase.expireMessages()/expireMessagesForAllSubscriptions() for PartitionTopic * * @throws Exception */ @Test public void testPersistentTopicExpireMessageOnParitionTopic() throws Exception { admin.topics().createPartitionedTopic("persistent://prop-xyz/use/ns1/ds1", 4); // create consumer and subscription PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) .build(); Consumer<byte[]> consumer = client.newConsumer().topic("persistent://prop-xyz/use/ns1/ds1") .subscriptionName("my-sub").subscribe(); Producer<byte[]> producer = client.newProducer(Schema.BYTES) .topic("persistent://prop-xyz/use/ns1/ds1") .enableBatching(false) .messageRoutingMode(MessageRoutingMode.RoundRobinPartition) .create(); for (int i = 0; i < 10; i++) { String message = "message-" + i; producer.send(message.getBytes()); } PartitionedTopicStats topicStats = admin.topics() .getPartitionedStats("persistent://prop-xyz/use/ns1/ds1", true); assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 10); TopicStats partitionStatsPartition0 = topicStats.partitions .get("persistent://prop-xyz/use/ns1/ds1-partition-0"); TopicStats 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.topics().expireMessagesForAllSubscriptions("persistent://prop-xyz/use/ns1/ds1", 1); Thread.sleep(1000); topicStats = admin.topics().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(); }
Example 12
Source File: PersistentTopicE2ETest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testMessageExpiry() throws Exception { int messageTTLSecs = 1; String namespaceName = "prop/expiry-check"; admin.namespaces().createNamespace(namespaceName); admin.namespaces().setNamespaceReplicationClusters(namespaceName, Sets.newHashSet("test")); admin.namespaces().setNamespaceMessageTTL(namespaceName, messageTTLSecs); final String topicName = "persistent://prop/expiry-check/topic1"; final String subName = "sub1"; final int numMsgs = 10; Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe(); PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get(); PersistentSubscription subRef = topicRef.getSubscription(subName); consumer.close(); assertFalse(subRef.getDispatcher().isConsumerConnected()); Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = 0; i < numMsgs; i++) { String message = "my-message-" + i; producer.send(message.getBytes()); } rolloverPerIntervalStats(); assertEquals(subRef.getNumberOfEntriesInBacklog(false), numMsgs); Thread.sleep(TimeUnit.SECONDS.toMillis(messageTTLSecs)); runMessageExpiryCheck(); // 1. check all messages expired for this unconnected subscription assertEquals(subRef.getNumberOfEntriesInBacklog(false), 0); // clean-up producer.close(); consumer.close(); admin.topics().deleteSubscription(topicName, subName); admin.topics().delete(topicName); admin.namespaces().deleteNamespace(namespaceName); }
Example 13
Source File: PrometheusMetricsTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testPerConsumerStats() throws Exception { Producer<byte[]> p1 = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic1").create(); Producer<byte[]> p2 = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic2").create(); Consumer<byte[]> c1 = pulsarClient.newConsumer() .topic("persistent://my-property/use/my-ns/my-topic1") .subscriptionName("test") .subscribe(); Consumer<byte[]> c2 = pulsarClient.newConsumer() .topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("test") .subscribe(); final int messages = 10; for (int i = 0; i < messages; i++) { String message = "my-message-" + i; p1.send(message.getBytes()); p2.send(message.getBytes()); } for (int i = 0; i < messages; i++) { c1.acknowledge(c1.receive()); c2.acknowledge(c2.receive()); } ByteArrayOutputStream statsOut = new ByteArrayOutputStream(); PrometheusMetricsGenerator.generate(pulsar, true, true, statsOut); String metricsStr = new String(statsOut.toByteArray()); Multimap<String, Metric> metrics = parseMetrics(metricsStr); metrics.entries().forEach(e -> { System.out.println(e.getKey() + ": " + e.getValue()); }); // There should be 1 metric aggregated per namespace List<Metric> cm = (List<Metric>) metrics.get("pulsar_out_bytes_total"); assertEquals(cm.size(), 4); assertEquals(cm.get(0).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(0).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic2"); assertEquals(cm.get(0).tags.get("subscription"), "test"); assertEquals(cm.get(1).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(1).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic2"); assertEquals(cm.get(1).tags.get("subscription"), "test"); assertEquals(cm.get(1).tags.get("consumer_id"), "1"); assertEquals(cm.get(2).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(2).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic1"); assertEquals(cm.get(2).tags.get("subscription"), "test"); assertEquals(cm.get(3).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(3).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic1"); assertEquals(cm.get(3).tags.get("subscription"), "test"); assertEquals(cm.get(3).tags.get("consumer_id"), "0"); cm = (List<Metric>) metrics.get("pulsar_out_messages_total"); assertEquals(cm.size(), 4); assertEquals(cm.get(0).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(0).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic2"); assertEquals(cm.get(0).tags.get("subscription"), "test"); assertEquals(cm.get(1).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(1).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic2"); assertEquals(cm.get(1).tags.get("subscription"), "test"); assertEquals(cm.get(1).tags.get("consumer_id"), "1"); assertEquals(cm.get(2).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(2).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic1"); assertEquals(cm.get(2).tags.get("subscription"), "test"); assertEquals(cm.get(3).tags.get("namespace"), "my-property/use/my-ns"); assertEquals(cm.get(3).tags.get("topic"), "persistent://my-property/use/my-ns/my-topic1"); assertEquals(cm.get(3).tags.get("subscription"), "test"); assertEquals(cm.get(3).tags.get("consumer_id"), "0"); p1.close(); p2.close(); c1.close(); c2.close(); }
Example 14
Source File: JodaTimeTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testJodaTime() throws PulsarAdminException, PulsarClientException { final String tenant = PUBLIC_TENANT; final String namespace = "test-namespace-" + randomName(16); final String topic = "test-joda-time-schema"; final String fqtn = TopicName.get( TopicDomain.persistent.value(), tenant, namespace, topic ).toString(); admin.namespaces().createNamespace( tenant + "/" + namespace, Sets.newHashSet(pulsarCluster.getClusterName()) ); JodaSchema forSend = new JodaSchema(); forSend.setDecimal(new BigDecimal("12.34")); forSend.setTimeMicros(System.currentTimeMillis() * 1000); forSend.setTimestampMillis(new DateTime("2019-03-26T04:39:58.469Z", ISOChronology.getInstanceUTC())); forSend.setTimeMillis(LocalTime.now()); forSend.setTimeMicros(System.currentTimeMillis() * 1000); forSend.setDate(LocalDate.now()); Producer<JodaSchema> producer = client .newProducer(Schema.AVRO(JodaSchema.class)) .topic(fqtn) .create(); Consumer<JodaSchema> consumer = client .newConsumer(Schema.AVRO(JodaSchema.class)) .topic(fqtn) .subscriptionName("test") .subscribe(); producer.send(forSend); JodaSchema received = consumer.receive().getValue(); assertEquals(received, forSend); producer.close(); consumer.close(); log.info("Successfully Joda time logical type message : {}", received); }
Example 15
Source File: V1_AdminApiTest2.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testPublishConsumerStats() throws Exception { final String topicName = "statTopic"; final String subscriberName = topicName + "-my-sub-1"; final String topic = "persistent://prop-xyz/use/ns1/" + topicName; final String producerName = "myProducer"; @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(pulsar.getWebServiceAddress()).build(); Consumer<byte[]> consumer = client.newConsumer().topic(topic).subscriptionName(subscriberName) .subscriptionType(SubscriptionType.Shared).subscribe(); Producer<byte[]> producer = client.newProducer() .topic(topic) .producerName(producerName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); retryStrategically((test) -> { TopicStats stats; try { stats = admin.topics().getStats(topic); return stats.publishers.size() > 0 && stats.subscriptions.get(subscriberName) != null && stats.subscriptions.get(subscriberName).consumers.size() > 0; } catch (PulsarAdminException e) { return false; } }, 5, 200); TopicStats topicStats = admin.topics().getStats(topic); assertEquals(topicStats.publishers.size(), 1); assertNotNull(topicStats.publishers.get(0).getAddress()); assertNotNull(topicStats.publishers.get(0).getClientVersion()); assertNotNull(topicStats.publishers.get(0).getConnectedSince()); assertNotNull(topicStats.publishers.get(0).getProducerName()); assertEquals(topicStats.publishers.get(0).getProducerName(), producerName); SubscriptionStats subscriber = topicStats.subscriptions.get(subscriberName); assertNotNull(subscriber); assertEquals(subscriber.consumers.size(), 1); ConsumerStats consumerStats = subscriber.consumers.get(0); assertNotNull(consumerStats.getAddress()); assertNotNull(consumerStats.getClientVersion()); assertNotNull(consumerStats.getConnectedSince()); producer.close(); consumer.close(); }
Example 16
Source File: MessageParserTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testWithBatches() throws Exception { String topic = "persistent://my-tenant/my-ns/my-topic-with-batch"; TopicName topicName = TopicName.get(topic); int n = 10; Producer<String> producer = pulsarClient.newProducer(Schema.STRING).enableBatching(true) .batchingMaxPublishDelay(10, TimeUnit.SECONDS).topic(topic).create(); ManagedCursor cursor = ((PersistentTopic) pulsar.getBrokerService().getTopicReference(topic).get()) .getManagedLedger().newNonDurableCursor(PositionImpl.earliest); for (int i = 0; i < n - 1; i++) { producer.sendAsync("hello-" + i); } producer.send("hello-" + (n - 1)); // Read through raw data assertEquals(cursor.getNumberOfEntriesInBacklog(false), 1); Entry entry = cursor.readEntriesOrWait(1).get(0); List<RawMessage> messages = Lists.newArrayList(); try { MessageParser.parseMessage(topicName, entry.getLedgerId(), entry.getEntryId(), entry.getDataBuffer(), (message) -> { messages.add(message); }, Commands.DEFAULT_MAX_MESSAGE_SIZE); } finally { entry.release(); } assertEquals(messages.size(), 10); for (int i = 0; i < n; i++) { assertEquals(messages.get(i).getData(), Unpooled.wrappedBuffer(("hello-" + i).getBytes())); } messages.forEach(RawMessage::release); producer.close(); }
Example 17
Source File: AdminApiTest2.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testPublishConsumerStats() throws Exception { final String topicName = "statTopic"; final String subscriberName = topicName + "-my-sub-1"; final String topic = "persistent://prop-xyz/ns1/" + topicName; final String producerName = "myProducer"; @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(pulsar.getWebServiceAddress()).build(); Consumer<byte[]> consumer = client.newConsumer().topic(topic).subscriptionName(subscriberName) .subscriptionType(SubscriptionType.Shared).subscribe(); Producer<byte[]> producer = client.newProducer() .topic(topic) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .producerName(producerName) .create(); retryStrategically((test) -> { TopicStats stats; try { stats = admin.topics().getStats(topic); return stats.publishers.size() > 0 && stats.subscriptions.get(subscriberName) != null && stats.subscriptions.get(subscriberName).consumers.size() > 0; } catch (PulsarAdminException e) { return false; } }, 5, 200); TopicStats topicStats = admin.topics().getStats(topic); assertEquals(topicStats.publishers.size(), 1); assertNotNull(topicStats.publishers.get(0).getAddress()); assertNotNull(topicStats.publishers.get(0).getClientVersion()); assertNotNull(topicStats.publishers.get(0).getConnectedSince()); assertNotNull(topicStats.publishers.get(0).getProducerName()); assertEquals(topicStats.publishers.get(0).getProducerName(), producerName); SubscriptionStats subscriber = topicStats.subscriptions.get(subscriberName); assertNotNull(subscriber); assertEquals(subscriber.consumers.size(), 1); ConsumerStats consumerStats = subscriber.consumers.get(0); assertNotNull(consumerStats.getAddress()); assertNotNull(consumerStats.getClientVersion()); assertNotNull(consumerStats.getConnectedSince()); producer.close(); consumer.close(); }
Example 18
Source File: PersistentTopicE2ETest.java From pulsar with Apache License 2.0 | 4 votes |
@Test(enabled = false) // TODO: enable this after java client supports graceful close public void testGracefulClose() throws Exception { final String topicName = "persistent://prop/ns-abc/topic4"; final String subName = "sub4"; Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get(); assertNotNull(topicRef); ExecutorService executor = Executors.newCachedThreadPool(); CountDownLatch latch = new CountDownLatch(1); executor.submit(() -> { for (int i = 0; i < 10; i++) { String message = "my-message-" + i; producer.send(message.getBytes()); } latch.countDown(); return null; }); producer.close(); // 1. verify there are no pending publish acks once the producer close // is completed on client assertEquals(topicRef.getProducers().values().iterator().next().getPendingPublishAcks(), 0); // safety latch in case of failure, // wait for the spawned thread to complete latch.await(); Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe(); PersistentSubscription subRef = topicRef.getSubscription(subName); assertNotNull(subRef); Message<byte[]> msg = null; for (int i = 0; i < 10; i++) { msg = consumer.receive(); } // 2. verify consumer close fails when there are outstanding // message acks try { consumer.close(); fail("should have failed"); } catch (IllegalStateException e) { // Expected - messages not acked } consumer.acknowledgeCumulative(msg); Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); // 3. verify consumer close succeeds once all messages are ack'ed consumer.close(); Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertTrue(subRef.getDispatcher().isConsumerConnected()); executor.shutdown(); }
Example 19
Source File: AdminApiTest.java From pulsar with Apache License 2.0 | 4 votes |
/** * Verify: PersistentTopicsBase.expireMessages()/expireMessagesForAllSubscriptions() for PartitionTopic * * @throws Exception */ @Test public void testPersistentTopicExpireMessageOnParitionTopic() throws Exception { admin.topics().createPartitionedTopic("persistent://prop-xyz/ns1/ds1", 4); // create consumer and subscription URL pulsarUrl = new URL(pulsar.getWebServiceAddress()); PulsarClient client = PulsarClient.builder().serviceUrl(pulsarUrl.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); Consumer<byte[]> consumer = client.newConsumer().topic("persistent://prop-xyz/ns1/ds1") .subscriptionName("my-sub").subscribe(); Producer<byte[]> producer = client.newProducer(Schema.BYTES) .topic("persistent://prop-xyz/ns1/ds1") .enableBatching(false) .messageRoutingMode(MessageRoutingMode.RoundRobinPartition) .create(); for (int i = 0; i < 10; i++) { String message = "message-" + i; producer.send(message.getBytes()); } PartitionedTopicStats topicStats = admin.topics().getPartitionedStats("persistent://prop-xyz/ns1/ds1", true); assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 10); TopicStats partitionStatsPartition0 = topicStats.partitions .get("persistent://prop-xyz/ns1/ds1-partition-0"); TopicStats partitionStatsPartition1 = topicStats.partitions .get("persistent://prop-xyz/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.topics().expireMessagesForAllSubscriptions("persistent://prop-xyz/ns1/ds1", 1); Thread.sleep(1000); topicStats = admin.topics().getPartitionedStats("persistent://prop-xyz/ns1/ds1", true); partitionStatsPartition0 = topicStats.partitions.get("persistent://prop-xyz/ns1/ds1-partition-0"); partitionStatsPartition1 = topicStats.partitions.get("persistent://prop-xyz/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(); }
Example 20
Source File: AdminApiTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test(dataProvider = "numBundles") public void testNamespaceBundleUnload(Integer numBundles) throws Exception { admin.namespaces().createNamespace("prop-xyz/ns1-bundles", numBundles); admin.namespaces().setNamespaceReplicationClusters("prop-xyz/ns1-bundles", Sets.newHashSet("test")); assertEquals(admin.topics().getList("prop-xyz/ns1-bundles"), Lists.newArrayList()); // Force to create a topic publishMessagesOnPersistentTopic("persistent://prop-xyz/ns1-bundles/ds2", 0); assertEquals(admin.topics().getList("prop-xyz/ns1-bundles"), Lists.newArrayList("persistent://prop-xyz/ns1-bundles/ds2")); // create consumer and subscription Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://prop-xyz/ns1-bundles/ds2") .subscriptionName("my-sub").subscribe(); assertEquals(admin.topics().getSubscriptions("persistent://prop-xyz/ns1-bundles/ds2"), Lists.newArrayList("my-sub")); // Create producer Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES) .topic("persistent://prop-xyz/ns1-bundles/ds2") .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = 0; i < 10; i++) { String message = "message-" + i; producer.send(message.getBytes()); } NamespaceBundle bundle = (NamespaceBundle) pulsar.getNamespaceService() .getBundle(TopicName.get("persistent://prop-xyz/ns1-bundles/ds2")); consumer.close(); producer.close(); admin.namespaces().unloadNamespaceBundle("prop-xyz/ns1-bundles", bundle.getBundleRange()); // check that no one owns the namespace bundle assertFalse(pulsar.getNamespaceService().isServiceUnitOwned(bundle)); assertFalse(otherPulsar.getNamespaceService().isServiceUnitOwned(bundle)); LOG.info("--- RELOAD ---"); // Force reload of namespace and wait for topic to be ready for (int i = 0; i < 30; i++) { try { admin.topics().getStats("persistent://prop-xyz/ns1-bundles/ds2"); break; } catch (PulsarAdminException e) { LOG.warn("Failed to get topic stats.. {}", e.getMessage()); Thread.sleep(1000); } } admin.topics().deleteSubscription("persistent://prop-xyz/ns1-bundles/ds2", "my-sub"); admin.topics().delete("persistent://prop-xyz/ns1-bundles/ds2"); }