Java Code Examples for org.apache.pulsar.common.naming.TopicName#getPartition()

The following examples show how to use org.apache.pulsar.common.naming.TopicName#getPartition() . 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: PersistentTopicsTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGrantPartitionedTopic() {
    final String partitionedTopicName = "partitioned-topic";
    final int numPartitions = 5;
    AsyncResponse response = mock(AsyncResponse.class);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    persistentTopics.createPartitionedTopic(response, testTenant, testNamespace, partitionedTopicName, numPartitions);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.NO_CONTENT.getStatusCode());

    String role = "role";
    Set<AuthAction> expectActions = new HashSet<>();
    expectActions.add(AuthAction.produce);
    persistentTopics.grantPermissionsOnTopic(testTenant, testNamespace, partitionedTopicName, role, expectActions);
    Map<String, Set<AuthAction>> permissions = persistentTopics.getPermissionsOnTopic(testTenant, testNamespace,
            partitionedTopicName);
    Assert.assertEquals(permissions.get(role), expectActions);
    TopicName topicName = TopicName.get(TopicDomain.persistent.value(), testTenant, testNamespace,
            partitionedTopicName);
    for (int i = 0; i < numPartitions; i++) {
        TopicName partition = topicName.getPartition(i);
        Map<String, Set<AuthAction>> partitionPermissions = persistentTopics.getPermissionsOnTopic(testTenant,
                testNamespace, partition.getEncodedLocalName());
        Assert.assertEquals(partitionPermissions.get(role), expectActions);
    }
}
 
Example 2
Source File: PersistentTopicsTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testRevokePartitionedTopic() {
    final String partitionedTopicName = "partitioned-topic";
    final int numPartitions = 5;
    AsyncResponse response = mock(AsyncResponse.class);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    persistentTopics.createPartitionedTopic(response, testTenant, testNamespace, partitionedTopicName, numPartitions);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.NO_CONTENT.getStatusCode());
    String role = "role";
    Set<AuthAction> expectActions = new HashSet<>();
    expectActions.add(AuthAction.produce);
    persistentTopics.grantPermissionsOnTopic(testTenant, testNamespace, partitionedTopicName, role, expectActions);
    persistentTopics.revokePermissionsOnTopic(testTenant, testNamespace, partitionedTopicName, role);
    Map<String, Set<AuthAction>> permissions = persistentTopics.getPermissionsOnTopic(testTenant, testNamespace,
            partitionedTopicName);
    Assert.assertEquals(permissions.get(role), null);
    TopicName topicName = TopicName.get(TopicDomain.persistent.value(), testTenant, testNamespace,
            partitionedTopicName);
    for (int i = 0; i < numPartitions; i++) {
        TopicName partition = topicName.getPartition(i);
        Map<String, Set<AuthAction>> partitionPermissions = persistentTopics.getPermissionsOnTopic(testTenant,
                testNamespace, partition.getEncodedLocalName());
        Assert.assertEquals(partitionPermissions.get(role), null);
    }
}
 
Example 3
Source File: PartitionedProducerConsumerTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageIdForSubscribeToSinglePartition() throws Exception {
    PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection
    TopicName topicName = null;
    TopicName partition2TopicName = null;
    Producer<byte[]> producer = null;
    Consumer<byte[]> consumer1 = null;
    Consumer<byte[]> consumer2 = null;
    final int numPartitions = 4;
    final int totalMessages = 30;

    try {
        log.info("-- Starting {} test --", methodName);

        topicName = TopicName.get("persistent://my-property/my-ns/my-topic-" + System.currentTimeMillis());
        partition2TopicName = topicName.getPartition(2);

        admin.topics().createPartitionedTopic(topicName.toString(), numPartitions);

        producer = pulsarClient.newProducer().topic(topicName.toString())
            .messageRouter(new AlwaysTwoMessageRouter())
            .create();

        consumer1 = pulsarClient.newConsumer().topic(topicName.toString())
            .subscriptionName("subscriber-partitioned")
            .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
            .subscriptionType(SubscriptionType.Exclusive)
            .subscribe();

        consumer2 = pulsarClient.newConsumer().topic(partition2TopicName.toString())
            .subscriptionName("subscriber-single")
            .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
            .subscriptionType(SubscriptionType.Exclusive)
            .subscribe();

        for (int i = 0; i < totalMessages; i++) {
            String message = "my-message-" + i;
            producer.newMessage().key(String.valueOf(i)).value(message.getBytes()).send();
        }
        producer.flush();

        Message<byte[]> msg;

        for (int i = 0; i < totalMessages; i ++) {
            msg = consumer1.receive(5, TimeUnit.SECONDS);
            Assert.assertEquals(((MessageIdImpl)((TopicMessageIdImpl)msg.getMessageId()).getInnerMessageId()).getPartitionIndex(), 2);
            consumer1.acknowledge(msg);
        }

        for (int i = 0; i < totalMessages; i ++) {
            msg = consumer2.receive(5, TimeUnit.SECONDS);
            Assert.assertEquals(((MessageIdImpl)msg.getMessageId()).getPartitionIndex(), 2);
            consumer2.acknowledge(msg);
        }

    } finally {
        producer.close();
        consumer1.unsubscribe();
        consumer1.close();
        consumer2.unsubscribe();
        consumer2.close();
        pulsarClient.close();
        admin.topics().deletePartitionedTopic(topicName.toString());

        log.info("-- Exiting {} test --", methodName);
    }
}