Java Code Examples for org.apache.pulsar.client.api.Consumer#unsubscribe()
The following examples show how to use
org.apache.pulsar.client.api.Consumer#unsubscribe() .
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: ConsumerUnsubscribeTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testConsumerUnsubscribeReference() throws Exception { PulsarClientImpl client = (PulsarClientImpl) PulsarClient.builder() .serviceUrl(mockBrokerService.getBrokerAddress()) .build(); Consumer<?> consumer = client.newConsumer().topic("persistent://public/default/t1").subscriptionName("sub1").subscribe(); consumer.unsubscribe(); assertEquals(client.consumersCount(), 0); client.close(); }
Example 2
Source File: MessageIdTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test(timeOut = 10000) public void producerSend() throws PulsarClientException { // 1. Basic Config String key = "producerSend"; final String topicName = "persistent://prop/cluster/namespace/topic-" + key; final String subscriptionName = "my-subscription-" + key; final String messagePredicate = "my-message-" + key + "-"; final int numberOfMessages = 30; // 2. Create Producer Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create(); // 3. Create Consumer Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName) .subscribe(); // 4. Publish message and get message id Set<MessageId> messageIds = new HashSet<>(); for (int i = 0; i < numberOfMessages; i++) { String message = messagePredicate + i; messageIds.add(producer.send(message.getBytes())); } // 4. Check if message Ids are correct log.info("Message IDs = " + messageIds); Assert.assertEquals(messageIds.size(), numberOfMessages, "Not all messages published successfully"); for (int i = 0; i < numberOfMessages; i++) { Assert.assertTrue(messageIds.remove(consumer.receive().getMessageId()), "Failed to receive Message"); } log.info("Message IDs = " + messageIds); Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully"); consumer.unsubscribe(); ; }
Example 3
Source File: TopicsConsumerImplTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test(timeOut = testTimeout) public void testSyncProducerAndConsumer() throws Exception { String key = "TopicsConsumerSyncTest"; final String subscriptionName = "my-ex-subscription-" + key; final String messagePredicate = "my-message-" + key + "-"; final int totalMessages = 30; final String topicName1 = "persistent://prop/use/ns-abc/topic-1-" + key; final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key; final String topicName3 = "persistent://prop/use/ns-abc/topic-3-" + key; List<String> topicNames = Lists.newArrayList(topicName1, topicName2, topicName3); TenantInfo tenantInfo = createDefaultTenantInfo(); admin.tenants().createTenant("prop", tenantInfo); admin.topics().createPartitionedTopic(topicName2, 2); admin.topics().createPartitionedTopic(topicName3, 3); // 1. producer connect Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2) .enableBatching(false) .messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition) .create(); Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3) .enableBatching(false) .messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition) .create(); // 2. Create consumer Consumer<byte[]> consumer = pulsarClient.newConsumer() .topics(topicNames) .subscriptionName(subscriptionName) .subscriptionType(SubscriptionType.Shared) .ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS) .receiverQueueSize(4) .subscribe(); assertTrue(consumer instanceof MultiTopicsConsumerImpl); // 3. producer publish messages for (int i = 0; i < totalMessages / 3; i++) { producer1.send((messagePredicate + "producer1-" + i).getBytes()); producer2.send((messagePredicate + "producer2-" + i).getBytes()); producer3.send((messagePredicate + "producer3-" + i).getBytes()); } int messageSet = 0; Message<byte[]> message = consumer.receive(); do { assertTrue(message instanceof TopicMessageImpl); messageSet ++; consumer.acknowledge(message); log.debug("Consumer acknowledged : " + new String(message.getData())); message = consumer.receive(500, TimeUnit.MILLISECONDS); } while (message != null); assertEquals(messageSet, totalMessages); consumer.unsubscribe(); consumer.close(); producer1.close(); producer2.close(); producer3.close(); }
Example 4
Source File: TopicsConsumerImplTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test(timeOut = testTimeout) public void testAsyncConsumer() throws Exception { String key = "TopicsConsumerAsyncTest"; final String subscriptionName = "my-ex-subscription-" + key; final String messagePredicate = "my-message-" + key + "-"; final int totalMessages = 30; final String topicName1 = "persistent://prop/use/ns-abc/topic-1-" + key; final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key; final String topicName3 = "persistent://prop/use/ns-abc/topic-3-" + key; List<String> topicNames = Lists.newArrayList(topicName1, topicName2, topicName3); TenantInfo tenantInfo = createDefaultTenantInfo(); admin.tenants().createTenant("prop", tenantInfo); admin.topics().createPartitionedTopic(topicName2, 2); admin.topics().createPartitionedTopic(topicName3, 3); // 1. producer connect Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2) .enableBatching(false) .messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition) .create(); Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3) .enableBatching(false) .messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition) .create(); // 2. Create consumer Consumer<byte[]> consumer = pulsarClient.newConsumer() .topics(topicNames) .subscriptionName(subscriptionName) .subscriptionType(SubscriptionType.Shared) .ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS) .receiverQueueSize(4) .subscribe(); assertTrue(consumer instanceof MultiTopicsConsumerImpl); // Asynchronously produce messages List<Future<MessageId>> futures = Lists.newArrayList(); for (int i = 0; i < totalMessages / 3; i++) { futures.add(producer1.sendAsync((messagePredicate + "producer1-" + i).getBytes())); futures.add(producer2.sendAsync((messagePredicate + "producer2-" + i).getBytes())); futures.add(producer3.sendAsync((messagePredicate + "producer3-" + i).getBytes())); } log.info("Waiting for async publish to complete : {}", futures.size()); for (Future<MessageId> future : futures) { future.get(); } log.info("start async consume"); CountDownLatch latch = new CountDownLatch(totalMessages); ExecutorService executor = Executors.newFixedThreadPool(1); executor.execute(() -> IntStream.range(0, totalMessages).forEach(index -> consumer.receiveAsync() .thenAccept(msg -> { assertTrue(msg instanceof TopicMessageImpl); try { consumer.acknowledge(msg); } catch (PulsarClientException e1) { fail("message acknowledge failed", e1); } latch.countDown(); log.info("receive index: {}, latch countDown: {}", index, latch.getCount()); }) .exceptionally(ex -> { log.warn("receive index: {}, failed receive message {}", index, ex.getMessage()); ex.printStackTrace(); return null; }))); latch.await(); log.info("success latch wait"); consumer.unsubscribe(); consumer.close(); producer1.close(); producer2.close(); producer3.close(); executor.shutdownNow(); }
Example 5
Source File: TopicsConsumerImplTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test(timeOut = testTimeout) public void multiTopicsInDifferentNameSpace() throws PulsarAdminException, PulsarClientException { List<String> topics = new ArrayList<>(); topics.add("persistent://prop/use/ns-abc/topic-1"); topics.add("persistent://prop/use/ns-abc/topic-2"); topics.add("persistent://prop/use/ns-abc1/topic-3"); admin.clusters().createCluster("use", new ClusterData(brokerUrl.toString())); admin.tenants().createTenant("prop", new TenantInfo(null, Sets.newHashSet("use"))); admin.namespaces().createNamespace("prop/use/ns-abc"); admin.namespaces().createNamespace("prop/use/ns-abc1"); Consumer consumer = pulsarClient.newConsumer() .topics(topics) .subscriptionName("multiTopicSubscription") .subscriptionType(SubscriptionType.Exclusive) .subscribe(); // create Producer Producer<String> producer = pulsarClient.newProducer(Schema.STRING) .topic("persistent://prop/use/ns-abc/topic-1") .producerName("producer") .create(); Producer<String> producer1 = pulsarClient.newProducer(Schema.STRING) .topic("persistent://prop/use/ns-abc/topic-2") .producerName("producer1") .create(); Producer<String> producer2 = pulsarClient.newProducer(Schema.STRING) .topic("persistent://prop/use/ns-abc1/topic-3") .producerName("producer2") .create(); //send message producer.send("ns-abc/topic-1-Message1"); producer1.send("ns-abc/topic-2-Message1"); producer2.send("ns-abc1/topic-3-Message1"); int messageSet = 0; Message<byte[]> message = consumer.receive(); do { messageSet ++; consumer.acknowledge(message); log.info("Consumer acknowledged : " + new String(message.getData())); message = consumer.receive(200, TimeUnit.MILLISECONDS); } while (message != null); assertEquals(messageSet, 3); consumer.unsubscribe(); consumer.close(); producer.close(); producer1.close(); producer2.close(); }
Example 6
Source File: PatternTopicsConsumerImplTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test(timeOut = testTimeout) public void testStartEmptyPatternConsumer() throws Exception { String key = "StartEmptyPatternConsumerTest"; String subscriptionName = "my-ex-subscription-" + key; String topicName1 = "persistent://my-property/my-ns/pattern-topic-1-" + key; String topicName2 = "persistent://my-property/my-ns/pattern-topic-2-" + key; String topicName3 = "persistent://my-property/my-ns/pattern-topic-3-" + key; Pattern pattern = Pattern.compile("persistent://my-property/my-ns/pattern-topic.*"); // 1. create partition TenantInfo tenantInfo = createDefaultTenantInfo(); admin.tenants().createTenant("prop", tenantInfo); admin.topics().createPartitionedTopic(topicName2, 2); admin.topics().createPartitionedTopic(topicName3, 3); // 2. Create consumer, this should success, but with empty sub-consumser internal Consumer<byte[]> consumer = pulsarClient.newConsumer() .topicsPattern(pattern) .patternAutoDiscoveryPeriod(2) .subscriptionName(subscriptionName) .subscriptionType(SubscriptionType.Shared) .ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS) .receiverQueueSize(4) .subscribe(); // 3. verify consumer get methods, to get 5 number of partitions and topics. assertSame(pattern, ((PatternMultiTopicsConsumerImpl<?>) consumer).getPattern()); assertEquals(((PatternMultiTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 5); assertEquals(((PatternMultiTopicsConsumerImpl<?>) consumer).getConsumers().size(), 5); assertEquals(((PatternMultiTopicsConsumerImpl<?>) consumer).getTopics().size(), 2); // 4. create producer String messagePredicate = "my-message-" + key + "-"; int totalMessages = 30; Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2) .enableBatching(false) .messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition) .create(); Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3) .enableBatching(false) .messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition) .create(); // 5. call recheckTopics to subscribe each added topics above log.debug("recheck topics change"); PatternMultiTopicsConsumerImpl<byte[]> consumer1 = ((PatternMultiTopicsConsumerImpl<byte[]>) consumer); consumer1.run(consumer1.getRecheckPatternTimeout()); Thread.sleep(100); // 6. verify consumer get methods, to get number of partitions and topics, value 6=1+2+3. assertSame(pattern, ((PatternMultiTopicsConsumerImpl<?>) consumer).getPattern()); assertEquals(((PatternMultiTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 6); assertEquals(((PatternMultiTopicsConsumerImpl<?>) consumer).getConsumers().size(), 6); assertEquals(((PatternMultiTopicsConsumerImpl<?>) consumer).getTopics().size(), 3); // 7. produce data for (int i = 0; i < totalMessages / 3; i++) { producer1.send((messagePredicate + "producer1-" + i).getBytes()); producer2.send((messagePredicate + "producer2-" + i).getBytes()); producer3.send((messagePredicate + "producer3-" + i).getBytes()); } // 8. should receive all the message int messageSet = 0; Message<byte[]> message = consumer.receive(); do { assertTrue(message instanceof TopicMessageImpl); messageSet ++; consumer.acknowledge(message); log.debug("Consumer acknowledged : " + new String(message.getData())); message = consumer.receive(500, TimeUnit.MILLISECONDS); } while (message != null); assertEquals(messageSet, totalMessages); consumer.unsubscribe(); consumer.close(); producer1.close(); producer2.close(); producer3.close(); }
Example 7
Source File: MessageIdTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test(timeOut = 10000) public void partitionedProducerSendAsync() throws PulsarClientException, PulsarAdminException { // 1. Basic Config String key = "partitionedProducerSendAsync"; final String topicName = "persistent://prop/cluster/namespace/topic-" + key; final String subscriptionName = "my-subscription-" + key; final String messagePredicate = "my-message-" + key + "-"; final int numberOfMessages = 30; int numberOfPartitions = 3; admin.topics().createPartitionedTopic(topicName, numberOfPartitions); // 2. Create Producer Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); // 3. Create Consumer Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName) .subscribe(); // 4. Publish message and get message id Set<MessageId> messageIds = new HashSet<>(); Set<Future<MessageId>> futures = new HashSet<>(); for (int i = 0; i < numberOfMessages; i++) { String message = messagePredicate + i; futures.add(producer.sendAsync(message.getBytes())); } futures.forEach(f -> { try { messageIds.add(f.get()); } catch (Exception e) { Assert.fail("Failed to publish message, Exception: " + e.getMessage()); } }); // 4. Check if message Ids are correct log.info("Message IDs = " + messageIds); Assert.assertEquals(messageIds.size(), numberOfMessages, "Not all messages published successfully"); for (int i = 0; i < numberOfMessages; i++) { MessageId topicMessageId = consumer.receive().getMessageId(); MessageId messageId = ((TopicMessageIdImpl)topicMessageId).getInnerMessageId(); log.info("Message ID Received = " + messageId); Assert.assertTrue(messageIds.remove(messageId), "Failed to receive Message"); } log.info("Message IDs = " + messageIds); Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully"); consumer.unsubscribe(); }
Example 8
Source File: PersistentTopicE2ETest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testSimpleConsumerEvents() throws Exception { final String topicName = "persistent://prop/ns-abc/topic1"; final String subName = "sub1"; final int numMsgs = 10; // 1. client connect Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe(); PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get(); PersistentSubscription subRef = topicRef.getSubscription(subName); assertNotNull(topicRef); assertNotNull(subRef); assertTrue(subRef.getDispatcher().isConsumerConnected()); Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(getAvailablePermits(subRef), 1000 /* default */); Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = 0; i < numMsgs * 2; i++) { String message = "my-message-" + i; producer.send(message.getBytes()); } assertTrue(subRef.getDispatcher().isConsumerConnected()); rolloverPerIntervalStats(); assertEquals(subRef.getNumberOfEntriesInBacklog(false), numMsgs * 2); // 2. messages pushed before client receive Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(getAvailablePermits(subRef), 1000 - numMsgs * 2); Message<byte[]> msg = null; for (int i = 0; i < numMsgs; i++) { msg = consumer.receive(); // 3. in-order message delivery assertEquals(new String(msg.getData()), "my-message-" + i); consumer.acknowledge(msg); } rolloverPerIntervalStats(); // 4. messages deleted on individual acks Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(subRef.getNumberOfEntriesInBacklog(false), numMsgs); for (int i = 0; i < numMsgs; i++) { msg = consumer.receive(); if (i == numMsgs - 1) { consumer.acknowledgeCumulative(msg); } } rolloverPerIntervalStats(); // 5. messages deleted on cumulative acks Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(subRef.getNumberOfEntriesInBacklog(false), 0); // 6. consumer unsubscribe consumer.unsubscribe(); // 6. consumer graceful close consumer.close(); // 7. consumer unsubscribe try { consumer.unsubscribe(); fail("Should have failed"); } catch (PulsarClientException.AlreadyClosedException e) { // ok } Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); subRef = topicRef.getSubscription(subName); assertNull(subRef); producer.close(); Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); }