org.apache.pulsar.client.api.MessageRoutingMode Java Examples

The following examples show how to use org.apache.pulsar.client.api.MessageRoutingMode. 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: CompactionTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(timeOut = 20000, dataProvider = "lastDeletedBatching")
public void testCompactionWithLastDeletedKey(boolean batching) throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-topic1";

    Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).enableBatching(batching)
            .messageRoutingMode(MessageRoutingMode.SinglePartition).create();

    pulsarClient.newConsumer().topic(topic).subscriptionName("sub1").readCompacted(true).subscribe().close();

    producer.newMessage().key("1").value("1".getBytes()).send();
    producer.newMessage().key("2").value("2".getBytes()).send();
    producer.newMessage().key("3").value("3".getBytes()).send();
    producer.newMessage().key("1").value("".getBytes()).send();
    producer.newMessage().key("2").value("".getBytes()).send();

    Compactor compactor = new TwoPhaseCompactor(conf, pulsarClient, bk, compactionScheduler);
    compactor.compact(topic).get();

    Set<String> expected = Sets.newHashSet("3");
    // consumer with readCompacted enabled only get compacted entries
    try (Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topic).subscriptionName("sub1")
            .readCompacted(true).subscribe()) {
        Message<byte[]> m = consumer.receive(2, TimeUnit.SECONDS);
        assertTrue(expected.remove(m.getKey()));
    }
}
 
Example #2
Source File: PersistentTopicE2ETest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateProducerWithSameName() throws Exception {
    String topic = "persistent://prop/ns-abc/testCreateProducerWithSameName";

    ProducerBuilder<byte[]> producerBuilder = pulsarClient.newProducer()
        .topic(topic)
        .producerName("test-producer-a")
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition);
    Producer<byte[]> p1 = producerBuilder.create();

    try {
        producerBuilder.create();
        fail("Should have thrown ProducerBusyException");
    } catch (ProducerBusyException e) {
        // Expected
    }

    p1.close();

    // Now p2 should succeed
    Producer<byte[]> p2 = producerBuilder.create();

    p2.close();
}
 
Example #3
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleTermination() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    /* MessageId msgId1 = */producer.send("test-msg-1".getBytes());
    /* MessageId msgId2 = */producer.send("test-msg-2".getBytes());
    MessageId msgId3 = producer.send("test-msg-3".getBytes());

    MessageId lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);

    try {
        producer.send("test-msg-4".getBytes());
        fail("Should have thrown exception");
    } catch (PulsarClientException.TopicTerminatedException e) {
        // Expected
    }
}
 
Example #4
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateProducerOnTerminatedTopic() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    /* MessageId msgId1 = */producer.send("test-msg-1".getBytes());
    /* MessageId msgId2 = */producer.send("test-msg-2".getBytes());
    MessageId msgId3 = producer.send("test-msg-3".getBytes());

    MessageId lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);

    try {
        pulsarClient.newProducer().topic(topicName).create();
        fail("Should have thrown exception");
    } catch (PulsarClientException.TopicTerminatedException e) {
        // Expected
    }
}
 
Example #5
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoubleTerminate() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    /* MessageId msgId1 = */producer.send("test-msg-1".getBytes());
    /* MessageId msgId2 = */producer.send("test-msg-2".getBytes());
    MessageId msgId3 = producer.send("test-msg-3".getBytes());

    MessageId lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);

    // Terminate it again
    lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);
}
 
Example #6
Source File: RawReaderTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private Set<String> publishMessages(String topic, int count, boolean batching) throws Exception {
    Set<String> keys = new HashSet<>();

    try (Producer<byte[]> producer = pulsarClient.newProducer()
        .enableBatching(batching)
        // easier to create enough batches with a small batch size
        .batchingMaxMessages(10)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .maxPendingMessages(count)
        .topic(topic)
        .create()) {
        Future<?> lastFuture = null;
        for (int i = 0; i < count; i++) {
            String key = "key"+i;
            byte[] data = ("my-message-" + i).getBytes();
            lastFuture = producer.newMessage().key(key).value(data).sendAsync();
            keys.add(key);
        }
        lastFuture.get();
    }
    return keys;
}
 
Example #7
Source File: V1_AdminApiTest2.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testTenantNameWithUnderscore() throws Exception {
    TenantInfo tenantInfo = new TenantInfo(Sets.newHashSet("role1", "role2"), Sets.newHashSet("use"));
    admin.tenants().createTenant("prop_xyz", tenantInfo);

    admin.namespaces().createNamespace("prop_xyz/use/my-namespace");

    String topic = "persistent://prop_xyz/use/my-namespace/my-topic";

    Producer<byte[]> producer = pulsarClient.newProducer()
        .topic(topic)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    TopicStats stats = admin.topics().getStats(topic);
    assertEquals(stats.publishers.size(), 1);
    producer.close();
}
 
Example #8
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(timeOut = 20000)
public void testSubscribeOnTerminatedTopic() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();
    /* MessageId msgId1 = */ producer.send("test-msg-1".getBytes());
    MessageId msgId2 = producer.send("test-msg-2".getBytes());

    MessageId lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId2);

    org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName)
            .subscriptionName("my-sub").subscribe();

    Thread.sleep(200);
    assertTrue(consumer.hasReachedEndOfTopic());
}
 
Example #9
Source File: CompactionTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(timeOut = 20000, dataProvider = "lastDeletedBatching")
public void testEmptyCompactionLedger(boolean batching) throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-topic1";

    Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).enableBatching(batching)
            .messageRoutingMode(MessageRoutingMode.SinglePartition).create();

    pulsarClient.newConsumer().topic(topic).subscriptionName("sub1").readCompacted(true).subscribe().close();

    producer.newMessage().key("1").value("1".getBytes()).send();
    producer.newMessage().key("2").value("2".getBytes()).send();
    producer.newMessage().key("1").value("".getBytes()).send();
    producer.newMessage().key("2").value("".getBytes()).send();

    Compactor compactor = new TwoPhaseCompactor(conf, pulsarClient, bk, compactionScheduler);
    compactor.compact(topic).get();

    // consumer with readCompacted enabled only get compacted entries
    try (Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topic).subscriptionName("sub1")
            .readCompacted(true).subscribe()) {
        Message<byte[]> m = consumer.receive(2, TimeUnit.SECONDS);
        assertNull(m);
    }
}
 
Example #10
Source File: AdminApiTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
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 #11
Source File: PartitionedProducerImplTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetStats() throws Exception {
    String topicName = "test-stats";
    ClientConfigurationData conf = new ClientConfigurationData();
    conf.setServiceUrl("pulsar://localhost:6650");
    conf.setStatsIntervalSeconds(100);

    ThreadFactory threadFactory = new DefaultThreadFactory("client-test-stats", Thread.currentThread().isDaemon());
    EventLoopGroup eventLoopGroup = EventLoopUtil.newEventLoopGroup(conf.getNumIoThreads(), threadFactory);

    PulsarClientImpl clientImpl = new PulsarClientImpl(conf, eventLoopGroup);

    ProducerConfigurationData producerConfData = new ProducerConfigurationData();
    producerConfData.setMessageRoutingMode(MessageRoutingMode.CustomPartition);
    producerConfData.setCustomMessageRouter(new CustomMessageRouter());

    assertEquals(Long.parseLong("100"), clientImpl.getConfiguration().getStatsIntervalSeconds());

    PartitionedProducerImpl impl = new PartitionedProducerImpl(
        clientImpl, topicName, producerConfData,
        1, null, null, null);

    impl.getStats();
}
 
Example #12
Source File: ReplicatorGlobalNSTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testForcefullyTopicDeletion() throws Exception {
    log.info("--- Starting ReplicatorTest::testForcefullyTopicDeletion ---");

    final String namespace = "pulsar/removeClusterTest";
    admin1.namespaces().createNamespace(namespace);
    admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1"));

    final String topicName = "persistent://" + namespace + "/topic";

    PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS)
            .build();

    ProducerImpl<byte[]> producer1 = (ProducerImpl<byte[]>) client1.newProducer().topic(topicName)
            .enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    producer1.close();

    admin1.topics().delete(topicName, true);

    MockedPulsarServiceBaseTest
            .retryStrategically((test) -> !pulsar1.getBrokerService().getTopics().containsKey(topicName), 50, 150);

    Assert.assertFalse(pulsar1.getBrokerService().getTopics().containsKey(topicName));

    client1.close();
}
 
Example #13
Source File: PersistentTopicE2ETest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleClientMultipleSubscriptions() throws Exception {
    final String topicName = "persistent://prop/ns-abc/topic6";
    final String subName = "sub6";

    pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
    pulsarClient.newProducer()
        .topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();
    try {
        pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
        fail("Should have thrown an exception since one consumer is already connected");
    } catch (PulsarClientException cce) {
        Assert.assertTrue(cce.getMessage().contains("Exclusive consumer is already connected"));
    }
}
 
Example #14
Source File: CompactorTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompaction() throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-topic1";
    final int numMessages = 1000;
    final int maxKeys = 10;

    Producer<byte[]> producer = pulsarClient.newProducer().topic(topic)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    Map<String, byte[]> expected = new HashMap<>();
    Random r = new Random(0);

    for (int j = 0; j < numMessages; j++) {
        int keyIndex = r.nextInt(maxKeys);
        String key = "key"+keyIndex;
        byte[] data = ("my-message-" + key + "-" + j).getBytes();
        producer.newMessage()
                .key(key)
                .value(data)
                .send();
        expected.put(key, data);
    }
    compactAndVerify(topic, expected);
}
 
Example #15
Source File: AbstractReplicator.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public AbstractReplicator(String topicName, String replicatorPrefix, String localCluster, String remoteCluster,
        BrokerService brokerService) throws NamingException {
    validatePartitionedTopic(topicName, brokerService);
    this.brokerService = brokerService;
    this.topicName = topicName;
    this.replicatorPrefix = replicatorPrefix;
    this.localCluster = localCluster.intern();
    this.remoteCluster = remoteCluster.intern();
    this.client = (PulsarClientImpl) brokerService.getReplicationClient(remoteCluster);
    this.producer = null;
    this.producerQueueSize = brokerService.pulsar().getConfiguration().getReplicationProducerQueueSize();

    this.producerBuilder = client.newProducer() //
            .topic(topicName)
            .messageRoutingMode(MessageRoutingMode.SinglePartition)
            .enableBatching(false)
            .sendTimeout(0, TimeUnit.SECONDS) //
            .maxPendingMessages(producerQueueSize) //
            .producerName(getReplicatorName(replicatorPrefix, localCluster));
    STATE_UPDATER.set(this, State.Stopped);
}
 
Example #16
Source File: PulsarSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public Producer<T> createProducer(PulsarClient client, String topic, String producerName, Schema<T> schema)
        throws PulsarClientException {
    ProducerBuilder<T> builder = client.newProducer(schema)
            .blockIfQueueFull(true)
            .enableBatching(true)
            .batchingMaxPublishDelay(10, TimeUnit.MILLISECONDS)
            .compressionType(CompressionType.LZ4)
            .hashingScheme(HashingScheme.Murmur3_32Hash) //
            .messageRoutingMode(MessageRoutingMode.CustomPartition)
            .messageRouter(FunctionResultRouter.of())
            // set send timeout to be infinity to prevent potential deadlock with consumer
            // that might happen when consumer is blocked due to unacked messages
            .sendTimeout(0, TimeUnit.SECONDS)
            .topic(topic);
    if (producerName != null) {
        builder.producerName(producerName);
    }

    return builder.properties(properties).create();
}
 
Example #17
Source File: PulsarKafkaProducer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private org.apache.pulsar.client.api.Producer<byte[]> createNewProducer(String topic) {
    try {
        pulsarProducerBuilder.messageRoutingMode(MessageRoutingMode.CustomPartition);
        pulsarProducerBuilder.messageRouter(new MessageRouter() {
            private static final long serialVersionUID = 1L;

            @Override
            public int choosePartition(Message<?> msg, TopicMetadata metadata) {
                // https://kafka.apache.org/08/documentation.html#producerapi
                // The default partitioner is based on the hash of the key.
                return partitioner.partition(msg.getKey(), metadata.numPartitions());
            }
        });
        log.info("Creating producer for topic {} with config {}", topic, pulsarProducerBuilder.toString());
        return pulsarProducerBuilder.clone().topic(topic).create();
    } catch (PulsarClientException e) {
        throw new RuntimeException(e);
    }
}
 
Example #18
Source File: PersistentTopicE2ETest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleProducerEvents() throws Exception {
    final String topicName = "persistent://prop/ns-abc/topic0";

    // 1. producer connect
    Producer<byte[]> producer = pulsarClient.newProducer()
        .topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get();
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);

    // 2. producer publish messages
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }

    rolloverPerIntervalStats();
    assertTrue(topicRef.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);

    // 3. producer disconnect
    producer.close();

    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(topicRef.getProducers().size(), 0);
}
 
Example #19
Source File: ReplicatorGlobalNSTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * If local cluster is removed from the global namespace then all topics under that namespace should be deleted from
 * the cluster.
 *
 * @throws Exception
 */
@Test
public void testRemoveLocalClusterOnGlobalNamespace() throws Exception {
    log.info("--- Starting ReplicatorTest::testRemoveLocalClusterOnGlobalNamespace ---");

    final String namespace = "pulsar/global/removeClusterTest";
    admin1.namespaces().createNamespace(namespace);
    admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2", "r3"));

    final String topicName = "persistent://" + namespace + "/topic";

    PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS)
            .build();
    PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()).statsInterval(0, TimeUnit.SECONDS)
            .build();

    ProducerImpl<byte[]> producer1 = (ProducerImpl<byte[]>) client1.newProducer().topic(topicName)
            .enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    ConsumerImpl<byte[]> consumer1 = (ConsumerImpl<byte[]>) client1.newConsumer().topic(topicName)
            .subscriptionName("sub1").subscribe();
    ConsumerImpl<byte[]> consumer2 = (ConsumerImpl<byte[]>) client2.newConsumer().topic(topicName)
            .subscriptionName("sub1").subscribe();

    admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r2", "r3"));

    MockedPulsarServiceBaseTest
            .retryStrategically((test) -> !pulsar1.getBrokerService().getTopics().containsKey(topicName), 50, 150);

    Assert.assertFalse(pulsar1.getBrokerService().getTopics().containsKey(topicName));
    Assert.assertFalse(producer1.isConnected());
    Assert.assertFalse(consumer1.isConnected());
    Assert.assertTrue(consumer2.isConnected());

    client1.close();
    client2.close();
}
 
Example #20
Source File: ReplicatorTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 30000)
public void verifyChecksumAfterReplication() throws Exception {
    final String topicName = "persistent://pulsar/ns/checksumAfterReplication-" + System.currentTimeMillis() + "-";

    PulsarClient c1 = PulsarClient.builder().serviceUrl(url1.toString()).build();
    Producer<byte[]> p1 = c1.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    PulsarClient c2 = PulsarClient.builder().serviceUrl(url2.toString()).build();
    RawReader reader2 = RawReader.create(c2, topicName, "sub").get();

    p1.send("Hello".getBytes());

    RawMessage msg = reader2.readNextAsync().get();

    ByteBuf b = msg.getHeadersAndPayload();

    assertTrue(Commands.hasChecksum(b));
    int parsedChecksum = Commands.readChecksum(b);
    int computedChecksum = Crc32cIntChecksum.computeChecksum(b);

    assertEquals(parsedChecksum, computedChecksum);

    p1.close();
    reader2.closeAsync().get();
}
 
Example #21
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 20000)
public void testSimpleTerminationReader() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    MessageId msgId1 = producer.send("test-msg-1".getBytes());
    MessageId msgId2 = producer.send("test-msg-2".getBytes());
    MessageId msgId3 = producer.send("test-msg-3".getBytes());

    MessageId lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);

    Reader<byte[]> reader = pulsarClient.newReader().topic(topicName).startMessageId(MessageId.earliest).create();

    Message<byte[]> msg1 = reader.readNext();
    assertEquals(msg1.getMessageId(), msgId1);

    Message<byte[]> msg2 = reader.readNext();
    assertEquals(msg2.getMessageId(), msgId2);

    Message<byte[]> msg3 = reader.readNext();
    assertEquals(msg3.getMessageId(), msgId3);

    Message<byte[]> msg4 = reader.readNext(100, TimeUnit.MILLISECONDS);
    assertNull(msg4);

    Thread.sleep(100);
    assertTrue(reader.hasReachedEndOfTopic());
}
 
Example #22
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 20000)
public void testSubscribeOnTerminatedTopicWithNoMessages() throws Exception {
    pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();
    admin.topics().terminateTopicAsync(topicName).get();

    org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName)
            .subscriptionName("my-sub").subscribe();

    Thread.sleep(200);
    assertTrue(consumer.hasReachedEndOfTopic());
}
 
Example #23
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 20000)
public void testSimpleTerminationConsumer() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();
    org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName)
            .subscriptionName("my-sub").subscribe();

    MessageId msgId1 = producer.send("test-msg-1".getBytes());
    MessageId msgId2 = producer.send("test-msg-2".getBytes());

    Message<byte[]> msg1 = consumer.receive();
    assertEquals(msg1.getMessageId(), msgId1);
    consumer.acknowledge(msg1);

    MessageId msgId3 = producer.send("test-msg-3".getBytes());

    assertFalse(consumer.hasReachedEndOfTopic());

    MessageId lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);

    Message<byte[]> msg2 = consumer.receive();
    assertEquals(msg2.getMessageId(), msgId2);
    consumer.acknowledge(msg2);

    Message<byte[]> msg3 = consumer.receive();
    assertEquals(msg3.getMessageId(), msgId3);
    consumer.acknowledge(msg3);

    Message<byte[]> msg4 = consumer.receive(100, TimeUnit.MILLISECONDS);
    assertNull(msg4);

    Thread.sleep(100);
    assertTrue(consumer.hasReachedEndOfTopic());
}
 
Example #24
Source File: AdminApiTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamespaceSplitBundleWithTopicCountEquallyDivideAlgorithm() throws Exception {
    // Force to create a topic
    final String namespace = "prop-xyz/ns1";
    List<String> topicNames = Lists.newArrayList(
        (new StringBuilder("persistent://")).append(namespace).append("/topicCountEquallyDivideAlgorithum-1").toString(),
        (new StringBuilder("persistent://")).append(namespace).append("/topicCountEquallyDivideAlgorithum-2").toString());

    List<Producer<byte[]>> producers = new ArrayList<>(2);
    for (String topicName : topicNames) {
        Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES)
            .topic(topicName)
            .enableBatching(false)
            .messageRoutingMode(MessageRoutingMode.SinglePartition)
            .create();
        producers.add(producer);
        producer.send("message".getBytes());
    }

    assertTrue(admin.topics().getList(namespace).containsAll(topicNames));

    try {
        admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0xffffffff", true,
            NamespaceBundleSplitAlgorithm.topicCountEquallyDivideName);
    } catch (Exception e) {
        fail("split bundle shouldn't have thrown exception");
    }
    NamespaceBundles bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
    NamespaceBundle bundle1 = pulsar.getNamespaceService().getBundle(TopicName.get(topicNames.get(0)));
    NamespaceBundle bundle2 = pulsar.getNamespaceService().getBundle(TopicName.get(topicNames.get(1)));
    assertNotEquals(bundle1, bundle2);
    String[] splitRange = { namespace + "/0x00000000_0x7fffffff", namespace + "/0x7fffffff_0xffffffff" };
    for (int i = 0; i < bundles.getBundles().size(); i++) {
        assertNotEquals(bundles.getBundles().get(i).toString(), splitRange[i]);
    }
    producers.forEach(Producer::closeAsync);
}
 
Example #25
Source File: ReplicatorTestBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
MessageProducer(URL url, final TopicName dest) throws Exception {
    this.url = url;
    this.namespace = dest.getNamespace();
    this.topicName = dest.toString();
    client = PulsarClient.builder().serviceUrl(url.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    producer = client.newProducer()
        .topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

}
 
Example #26
Source File: AdminApiTest2.java    From pulsar with Apache License 2.0 5 votes vote down vote up
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 #27
Source File: AdminApiTest2.java    From pulsar with Apache License 2.0 5 votes vote down vote up
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 #28
Source File: ProxyTlsTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitions() throws Exception {
    PulsarClient client = PulsarClient.builder()
            .serviceUrl(proxyService.getServiceUrlTls())
            .allowTlsInsecureConnection(false).tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).build();
    TenantInfo tenantInfo = createDefaultTenantInfo();
    admin.tenants().createTenant("sample", tenantInfo);
    admin.topics().createPartitionedTopic("persistent://sample/test/local/partitioned-topic", 2);

    Producer<byte[]> producer = client.newProducer(Schema.BYTES).topic("persistent://sample/test/local/partitioned-topic")
            .messageRoutingMode(MessageRoutingMode.RoundRobinPartition).create();

    // Create a consumer directly attached to broker
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://sample/test/local/partitioned-topic")
            .subscriptionName("my-sub").subscribe();

    for (int i = 0; i < 10; i++) {
        producer.send("test".getBytes());
    }

    for (int i = 0; i < 10; i++) {
        Message<byte[]> msg = consumer.receive(1, TimeUnit.SECONDS);
        checkNotNull(msg);
    }

    client.close();
}
 
Example #29
Source File: ProxyKeyStoreTlsTestWithoutAuth.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitions() throws Exception {
    @Cleanup
    PulsarClient client = internalSetUpForClient(true, proxyService.getServiceUrlTls());
    String topicName = "persistent://sample/test/local/partitioned-topic" + System.currentTimeMillis();
    TenantInfo tenantInfo = createDefaultTenantInfo();
    admin.tenants().createTenant("sample", tenantInfo);
    admin.topics().createPartitionedTopic(topicName, 2);

    @Cleanup
    Producer<byte[]> producer = client.newProducer(Schema.BYTES).topic(topicName)
            .messageRoutingMode(MessageRoutingMode.RoundRobinPartition).create();

    // Create a consumer directly attached to broker
    @Cleanup
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName)
            .subscriptionName("my-sub").subscribe();

    for (int i = 0; i < 10; i++) {
        producer.send("test".getBytes());
    }

    for (int i = 0; i < 10; i++) {
        Message<byte[]> msg = consumer.receive(1, TimeUnit.SECONDS);
        checkNotNull(msg);
    }
}
 
Example #30
Source File: ProxyStatsTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Validates proxy connection stats api.
 * 
 * @throws Exception
 */
@Test
public void testConnectionsStats() throws Exception {
    final String topicName1 = "persistent://sample/test/local/connections-stats";
    PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()).build();
    Producer<byte[]> producer = client.newProducer(Schema.BYTES).topic(topicName1).enableBatching(false)
            .messageRoutingMode(MessageRoutingMode.SinglePartition).create();

    // Create a consumer directly attached to broker
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName1).subscriptionName("my-sub").subscribe();

    int totalMessages = 10;
    for (int i = 0; i < totalMessages; i++) {
        producer.send("test".getBytes());
    }

    for (int i = 0; i < totalMessages; i++) {
        Message<byte[]> msg = consumer.receive(1, TimeUnit.SECONDS);
        checkNotNull(msg);
        consumer.acknowledge(msg);
    }

    Client httpClient = ClientBuilder.newClient(new ClientConfig().register(LoggingFeature.class));
    Response r = httpClient.target(proxyWebServer.getServiceUri()).path("/proxy-stats/connections").request()
            .get();
    Assert.assertEquals(r.getStatus(), Response.Status.OK.getStatusCode());
    String response = r.readEntity(String.class).trim();
    List<ConnectionStats> connectionStats = new Gson().fromJson(response, new TypeToken<List<ConnectionStats>>() {
    }.getType());

    assertNotNull(connectionStats);

    consumer.close();
    client.close();
}