Java Code Examples for org.apache.pulsar.client.api.PulsarClient#close()

The following examples show how to use org.apache.pulsar.client.api.PulsarClient#close() . These examples are extracted from open source projects. 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
/**
 * It verifies that if broker fails to complete producer/consumer operation then client times out rather waiting
 * forever.
 *
 * @throws PulsarClientException
 */
@Test(expectedExceptions = PulsarClientException.TimeoutException.class)
public void testOperationTimeout() throws PulsarClientException {
    final String topicName = "persistent://my-property/my-ns/my-topic1";
    ConcurrentOpenHashMap<String, CompletableFuture<Optional<Topic>>> topics = pulsar.getBrokerService()
            .getTopics();
    // non-complete topic future so, create topic should timeout
    topics.put(topicName, new CompletableFuture<>());
    PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString())
            .operationTimeout(2, TimeUnit.SECONDS).statsInterval(0, TimeUnit.SECONDS).build();
    try {
        Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    } finally {
        topics.clear();
        pulsarClient.close();
    }
}
 
Example 2
Source Project: pulsar   File: ReplicatorGlobalNSTest.java    License: 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 3
Source Project: pulsar   File: SampleProducer.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
    PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();

    Producer<byte[]> producer = client.newProducer().topic("persistent://my-tenant/my-ns/my-topic").create();

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

    client.close();
}
 
Example 4
Source Project: Groza   File: producers.java    License: Apache License 2.0 5 votes vote down vote up
public static void PulsarClient() throws PulsarClientException {
    PulsarClient client = PulsarClient.builder()
            .serviceUrl("pulsar://localhost:6650")
            .build();
    Producer<byte[]> producer = client.newProducer()
            .topic("my-topic")
            .create();
    producer.send("My message".getBytes());
    client.close();
    producer.close();
}
 
Example 5
Source Project: pulsar   File: ProxyParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitions() throws Exception {
    TenantInfo tenantInfo = createDefaultTenantInfo();
    admin.tenants().createTenant("sample", tenantInfo);
    PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl())
            .build();
    admin.topics().createPartitionedTopic("persistent://sample/test/local/partitioned-topic", 2);

    Producer<byte[]> producer = client.newProducer(Schema.BYTES)
        .topic("persistent://sample/test/local/partitioned-topic")
        .enableBatching(false)
        .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 6
Source Project: pulsar   File: BuildersTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = {PulsarClientException.class}, expectedExceptionsMessageRegExp = ".* must be specified but they cannot be specified at the same time.*")
public void shouldSetOneStartOpt() throws Exception {
    PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();
    try (Reader reader = client.newReader().topic("abc").create()) {
        // no-op
    } finally {
        client.close();
    }
}
 
Example 7
public static void main(String[] args) throws IOException {
    // Create a Pulsar client instance. A single instance can be shared across many
    // producers and consumer within the same application
    PulsarClient client = PulsarClient.builder()
            .serviceUrl(SERVICE_URL)
            .build();

    // Here you get the chance to configure producer specific settings
    Producer<byte[]> producer = client.newProducer()
            // Set the topic
            .topic(TOPIC_NAME)
            // Enable compression
            .compressionType(CompressionType.LZ4)
            .create();

    // Once the producer is created, it can be used for the entire application life-cycle
    log.info("Created producer for the topic {}", TOPIC_NAME);

    // Send 10 test messages
    IntStream.range(1, 11).forEach(i -> {
        String content = String.format("hello-pulsar-%d", i);

        // Build a message object
        Message<byte[]> msg = MessageBuilder.create()
                .setContent(content.getBytes())
                .build();

        // Send each message and log message content and ID when successfully received
        try {
            MessageId msgId = producer.send(msg);

            log.info("Published message '{}' with the ID {}", content, msgId);
        } catch (PulsarClientException e) {
            log.error(e.getMessage());
        }
    });

    client.close();
}
 
Example 8
Source Project: pulsar   File: ReplicatorGlobalNSTest.java    License: 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 9
Source Project: pulsar   File: ProxyTlsTest.java    License: 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 10
Source Project: pulsar   File: BuildersTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = {PulsarClientException.class}, expectedExceptionsMessageRegExp = ".* must be specified but they cannot be specified at the same time.*")
public void shouldNotSetTwoOptAtTheSameTime() throws Exception {
    PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();
    try (Reader reader = client.newReader().topic("abc").startMessageId(MessageId.earliest).startMessageFromRollbackDuration(10, TimeUnit.HOURS).create()) {
        // no-op
    } finally {
        client.close();
    }
}
 
Example 11
Source Project: pulsar   File: ProxyStatsTest.java    License: 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();
}
 
Example 12
Source Project: pulsar   File: ProxyParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProducer() throws Exception {
    PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl())
            .build();
    Producer<byte[]> producer = client.newProducer(Schema.BYTES).topic("persistent://sample/test/local/producer-topic")
            .create();

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

    client.close();
}
 
Example 13
Source Project: pulsar   File: BrokerBkEnsemblesTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test(timeOut=20000)
public void testTopicWithWildCardChar() throws Exception {
    PulsarClient client = PulsarClient.builder()
            .serviceUrl(pulsar.getWebServiceAddress())
            .statsInterval(0, TimeUnit.SECONDS)
            .build();

    final String ns1 = "prop/usc/topicWithSpecialChar";
    try {
        admin.namespaces().createNamespace(ns1);
    } catch (Exception e) {

    }

    final String topic1 = "persistent://"+ns1+"/`[email protected]#$%^&*()-_+=[]://{}|\\;:'\"<>,./?-30e04524";
    final String subName1 = "c1";
    final byte[] content = "test".getBytes();

    Consumer<byte[]> consumer = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
    org.apache.pulsar.client.api.Producer<byte[]> producer = client.newProducer().topic(topic1).create();

    producer.send(content);
    Message<byte[]> msg = consumer.receive();
    Assert.assertEquals(msg.getData(), content);
    consumer.close();
    producer.close();
    client.close();
}
 
Example 14
@Test
void testAuthentication() throws Exception {
	log.info("-- Starting {} test --", methodName);

	// Step 1: Create Admin Client

	// create a client which connects to proxy and pass authData
	String topicName = "persistent://my-property/my-ns/my-topic1";

	ProxyConfiguration proxyConfig = new ProxyConfiguration();
	proxyConfig.setAuthenticationEnabled(true);
	proxyConfig.setServicePort(Optional.of(0));
	proxyConfig.setWebServicePort(Optional.of(0));
	proxyConfig.setBrokerServiceURL(pulsar.getBrokerServiceUrl());
	proxyConfig.setSaslJaasClientAllowedIds(".*" + localHostname + ".*");
	proxyConfig.setSaslJaasServerSectionName("PulsarProxy");

	// proxy connect to broker
	proxyConfig.setBrokerClientAuthenticationPlugin(AuthenticationSasl.class.getName());
	proxyConfig.setBrokerClientAuthenticationParameters(
		"{\"saslJaasClientSectionName\": " + "\"PulsarProxy\"," +
			"\"serverType\": " + "\"broker\"}");

	// proxy as a server, it will use sasl to authn
	Set<String> providers = new HashSet<>();
	providers.add(AuthenticationProviderSasl.class.getName());
	proxyConfig.setAuthenticationProviders(providers);

	proxyConfig.setForwardAuthorizationCredentials(true);
	AuthenticationService authenticationService = new AuthenticationService(
                       PulsarConfigurationLoader.convertFrom(proxyConfig));
	ProxyService proxyService = new ProxyService(proxyConfig, authenticationService);

	proxyService.start();
	final String proxyServiceUrl = "pulsar://localhost:" + proxyService.getListenPort().get();
	log.info("1 proxy service started {}", proxyService);

	// Step 3: Pass correct client params
	PulsarClient proxyClient = createProxyClient(proxyServiceUrl, 1);
	log.info("2 create proxy client {}, {}", proxyServiceUrl, proxyClient);

	Producer<byte[]> producer = proxyClient.newProducer(Schema.BYTES).topic(topicName).create();
	log.info("3 created producer.");

	Consumer<byte[]> consumer = proxyClient.newConsumer(Schema.BYTES).topic(topicName).subscriptionName("test-sub").subscribe();
	log.info("4 created consumer.");

	for (int i = 0; i < 10; i++) {
		String message = "my-message-" + i;
		producer.send(message.getBytes());
		log.info("Produced message: [{}]", message);
	}

	Message<byte[]> msg = null;
	Set<String> messageSet = Sets.newHashSet();
	for (int i = 0; i < 10; 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);
	consumer.close();

	proxyClient.close();
	proxyService.close();
}
 
Example 15
Source Project: pulsar   File: CmdConsume.java    License: Apache License 2.0 4 votes vote down vote up
private int consume(String topic) {
    int numMessagesConsumed = 0;
    int returnCode = 0;

    try {
        PulsarClient client = clientBuilder.build();
        ConsumerBuilder<byte[]> builder = client.newConsumer()
                .subscriptionName(this.subscriptionName)
                .subscriptionType(subscriptionType)
                .subscriptionInitialPosition(subscriptionInitialPosition);

        if (isRegex) {
            builder.topicsPattern(Pattern.compile(topic));
        } else {
            builder.topic(topic);
        }

        ConsumerBuilder<byte[]> consumerBuilder = client.newConsumer().topic(topic);
        if (this.maxPendingChuckedMessage > 0) {
            consumerBuilder.maxPendingChuckedMessage(this.maxPendingChuckedMessage);
        }
        if (this.receiverQueueSize > 0) {
            consumerBuilder.maxPendingChuckedMessage(this.receiverQueueSize);
        }
        Consumer<byte[]> consumer = consumerBuilder.subscriptionName(this.subscriptionName)
                .autoAckOldestChunkedMessageOnQueueFull(this.autoAckOldestChunkedMessageOnQueueFull)
                .subscriptionType(subscriptionType).subscribe();

        RateLimiter limiter = (this.consumeRate > 0) ? RateLimiter.create(this.consumeRate) : null;
        while (this.numMessagesToConsume == 0 || numMessagesConsumed < this.numMessagesToConsume) {
            if (limiter != null) {
                limiter.acquire();
            }

            Message<byte[]> msg = consumer.receive(5, TimeUnit.SECONDS);
            if (msg == null) {
                LOG.debug("No message to consume after waiting for 5 seconds.");
            } else {
                numMessagesConsumed += 1;
                System.out.println(MESSAGE_BOUNDARY);
                String output = this.interpretMessage(msg, displayHex);
                System.out.println(output);
                consumer.acknowledge(msg);
            }
        }
        client.close();
    } catch (Exception e) {
        LOG.error("Error while consuming messages");
        LOG.error(e.getMessage(), e);
        returnCode = -1;
    } finally {
        LOG.info("{} messages successfully consumed", numMessagesConsumed);
    }

    return returnCode;

}
 
Example 16
/**
 * Test that sequence id from a producer is correct when there are send errors
 */
@Test
public void partitionedTopicWithSchema() throws Exception {
    admin.namespaces().createNamespace("prop/my-test", Collections.singleton("usc"));

    String topicName = "prop/my-test/my-topic";

    admin.topics().createPartitionedTopic(topicName, 16);

    int N = 10;

    PulsarClient client = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).build();

    CompletableFuture<Producer<String>> producerFuture = client.newProducer(Schema.STRING)
        .topic(topicName)
        .createAsync();
    CompletableFuture<Consumer<String>> consumerFuture = client.newConsumer(Schema.STRING)
        .topic(topicName)
        .subscriptionName("sub")
        .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
        .subscribeAsync();

    CompletableFuture.allOf(producerFuture, consumerFuture).get();

    Producer<String> producer = producerFuture.get();
    Consumer<String> consumer = consumerFuture.get();

    for (int i = 0; i < N; i++) {
        producer.send("Hello-" + i);
    }

    consumer.close();
    producer.close();

    // Force topic reloading to re-open the schema multiple times in parallel
    admin.namespaces().unload("prop/my-test");

    producerFuture = client.newProducer(Schema.STRING)
        .topic(topicName)
        .createAsync();
    consumerFuture = client.newConsumer(Schema.STRING)
        .topic(topicName)
        .subscriptionName("sub")
        .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
        .subscribeAsync();

    // Re-opening the topic should succeed
    CompletableFuture.allOf(producerFuture, consumerFuture).get();

    consumer = consumerFuture.get();

    Set<String> messages = new TreeSet<>();

    for (int i = 0; i < N; i++) {
        Message<String> msg = consumer.receive();
        messages.add(msg.getValue());
        consumer.acknowledge(msg);
    }

    assertEquals(messages.size(), N);
    for (int i = 0; i < N; i++) {
        assertTrue(messages.contains("Hello-" + i));
    }

    client.close();
}
 
Example 17
Source Project: pulsar   File: V1_AdminApiTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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 18
Source Project: pulsar   File: AdminApiTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void persistentTopicsInvalidCursorReset() throws Exception {
    admin.namespaces().setRetention("prop-xyz/ns1", new RetentionPolicies(10, 10));

    assertEquals(admin.topics().getList("prop-xyz/ns1"), Lists.newArrayList());

    String topicName = "persistent://prop-xyz/ns1/invalidcursorreset";
    // Force to create a topic
    publishMessagesOnPersistentTopic(topicName, 0);
    assertEquals(admin.topics().getList("prop-xyz/ns1"), Lists.newArrayList(topicName));

    // create consumer and subscription
    PulsarClient client = PulsarClient.builder()
            .serviceUrl(pulsar.getWebServiceAddress())
            .statsInterval(0, TimeUnit.SECONDS)
            .build();
    Consumer<byte[]> consumer = client.newConsumer().topic(topicName).subscriptionName("my-sub")
            .subscriptionType(SubscriptionType.Exclusive).subscribe();

    assertEquals(admin.topics().getSubscriptions(topicName), Lists.newArrayList("my-sub"));

    publishMessagesOnPersistentTopic(topicName, 10);

    List<Message<byte[]>> messages = admin.topics().peekMessages(topicName, "my-sub", 10);
    assertEquals(messages.size(), 10);

    for (int i = 0; i < 10; i++) {
        Message<byte[]> message = consumer.receive();
        consumer.acknowledge(message);
    }
    // use invalid timestamp
    try {
        admin.topics().resetCursor(topicName, "my-sub", System.currentTimeMillis() - 190000);
    } catch (Exception e) {
        // fail the test
        throw e;
    }

    admin.topics().resetCursor(topicName, "my-sub", System.currentTimeMillis() + 90000);
    consumer = client.newConsumer().topic(topicName).subscriptionName("my-sub").subscribe();
    consumer.close();
    client.close();

    admin.topics().deleteSubscription(topicName, "my-sub");

    assertEquals(admin.topics().getSubscriptions(topicName), Lists.newArrayList());
    admin.topics().delete(topicName);
}
 
Example 19
Source Project: pulsar   File: CmdProduce.java    License: Apache License 2.0 4 votes vote down vote up
private int publish(String topic) {
    int numMessagesSent = 0;
    int returnCode = 0;

    try {
        PulsarClient client = clientBuilder.build();
        ProducerBuilder<byte[]> producerBuilder = client.newProducer().topic(topic);
        if (this.chunkingAllowed) {
            producerBuilder.enableChunking(true);
            producerBuilder.enableBatching(false);
        }
        Producer<byte[]> producer = producerBuilder.create();

        List<byte[]> messageBodies = generateMessageBodies(this.messages, this.messageFileNames);
        RateLimiter limiter = (this.publishRate > 0) ? RateLimiter.create(this.publishRate) : null;

        Map<String, String> kvMap = new HashMap<>();
        for (String property : properties) {
            String [] kv = property.split("=");
            kvMap.put(kv[0], kv[1]);
        }

        for (int i = 0; i < this.numTimesProduce; i++) {
            for (byte[] content : messageBodies) {
                if (limiter != null) {
                    limiter.acquire();
                }

                TypedMessageBuilder<byte[]> message = producer.newMessage();

                if (!kvMap.isEmpty()) {
                    message.properties(kvMap);
                }

                if (key != null && !key.isEmpty()) {
                    message.key(key);
                }

                message.value(content).send();

                numMessagesSent++;
            }
        }
        client.close();
    } catch (Exception e) {
        LOG.error("Error while producing messages");
        LOG.error(e.getMessage(), e);
        returnCode = -1;
    } finally {
        LOG.info("{} messages successfully produced", numMessagesSent);
    }

    return returnCode;
}
 
Example 20
Source Project: pulsar   File: PersistentTopicE2ETest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testProducerQueueFullNonBlocking() throws Exception {
    final String topicName = "persistent://prop/ns-abc/topic-xyzx";
    final int messages = 10;

    // 1. Producer connect
    PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) client.newProducer()
        .topic(topicName)
        .maxPendingMessages(messages)
        .blockIfQueueFull(false)
        .sendTimeout(1, TimeUnit.SECONDS)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    // 2. Stop broker
    super.internalCleanup();

    // 2. producer publish messages
    long startTime = System.nanoTime();
    for (int i = 0; i < messages; i++) {
        // Should never block
        producer.sendAsync("msg".getBytes());
    }

    // Verify thread was not blocked
    long delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
    assertEquals(producer.getPendingQueueSize(), messages);

    // Next send operation must fail and not block
    startTime = System.nanoTime();
    try {
        producer.send("msg".getBytes());
        fail("Send should have failed");
    } catch (PulsarClientException.ProducerQueueIsFullError e) {
        // Expected
    }
    delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
    assertEquals(producer.getPendingQueueSize(), messages);

    // 4. producer disconnect
    producer.close();
    client.close();

    // 5. Restart broker
    setup();
}