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

The following examples show how to use org.apache.pulsar.client.api.PulsarClient. 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: ClientBuilderImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public PulsarClient build() throws PulsarClientException {
    if (StringUtils.isBlank(conf.getServiceUrl()) && conf.getServiceUrlProvider() == null) {
        throw new IllegalArgumentException("service URL or service URL provider needs to be specified on the ClientBuilder object.");
    }
    if (StringUtils.isNotBlank(conf.getServiceUrl()) && conf.getServiceUrlProvider() != null) {
        throw new IllegalArgumentException("Can only chose one way service URL or service URL provider.");
    }
    if (conf.getServiceUrlProvider() != null) {
        if (StringUtils.isBlank(conf.getServiceUrlProvider().getServiceUrl())) {
            throw new IllegalArgumentException("Cannot get service url from service url provider.");
        } else {
            conf.setServiceUrl(conf.getServiceUrlProvider().getServiceUrl());
        }
    }
    PulsarClient client = new PulsarClientImpl(conf);
    if (conf.getServiceUrlProvider() != null) {
        conf.getServiceUrlProvider().initialize(client);
    }
    return client;
}
 
Example #2
Source File: SampleConsumerWithSchema.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws PulsarClientException, JsonProcessingException {

        PulsarClient pulsarClient = PulsarClient.builder().serviceUrl("http://localhost:8080").build();

        Consumer<JsonPojo> consumer = pulsarClient.newConsumer(JSONSchema.of
                (SchemaDefinition.<JsonPojo>builder().withPojo(JsonPojo.class).build())) //
                .topic("persistent://my-property/use/my-ns/my-topic") //
                .subscriptionName("my-subscription-name").subscribe();

        Message<JsonPojo> msg = null;

        for (int i = 0; i < 100; i++) {
            msg = consumer.receive();
            // do something
            System.out.println("Received: " + msg.getValue().content);
        }

        // Acknowledge the consumption of all messages at once
        consumer.acknowledgeCumulative(msg);
        pulsarClient.close();
    }
 
Example #3
Source File: PulsarRecordsStorageConfiguration.java    From liiklus with MIT License 6 votes vote down vote up
@SneakyThrows
PulsarClient createClient(PulsarProperties pulsarProperties) {
    var clientBuilder = PulsarClient.builder()
            .serviceUrl(pulsarProperties.getServiceUrl());

    pulsarProperties.getTlsTrustCertsFilePath().ifPresent(clientBuilder::tlsTrustCertsFilePath);
    pulsarProperties.getAuthPluginClassName().ifPresent(authClass -> {
        try {
            clientBuilder.authentication(authClass, pulsarProperties.getAuthPluginParams());
        } catch (PulsarClientException.UnsupportedAuthenticationException e) {
            throw new IllegalStateException(e);
        }
    });

    return clientBuilder.build();
}
 
Example #4
Source File: ProducerSparkReceiverData.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length < 2) {
    System.err.println("Missing parameters!");
    System.err.println("Usage: <pulsar-service-url> <topic>");
    return;
  }

  System.out.println("Parameters:");
  System.out.println("\tServiceUrl:\t" + args[0]);
  System.out.println("\tTopic:\t" + args[1]);

  try (PulsarClient client = PulsarClient.builder().serviceUrl(args[0]).build()) {
    try (Producer<byte[]> producer = client.newProducer().topic(args[1]).create()) {
      for (int i = 0; i < 100; i++) {
        producer.send(("producer spark streaming msg").getBytes(StandardCharsets.UTF_8));
      }
    }
  }

  System.out.println("producer spark streaming msg end ...");
}
 
Example #5
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 #6
Source File: KeyStoreTlsProducerConsumerTestWithoutAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example #7
Source File: AdminApiTlsAuthTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteNamespace() throws Exception {
    try (PulsarAdmin admin = buildAdminClient("admin")) {
        log.info("Creating tenant");
        admin.tenants().createTenant("tenant1",
                                     new TenantInfo(ImmutableSet.of("admin"), ImmutableSet.of("test")));
        log.info("Creating namespace, and granting perms to user1");
        admin.namespaces().createNamespace("tenant1/ns1", ImmutableSet.of("test"));
        admin.namespaces().grantPermissionOnNamespace("tenant1/ns1", "user1", ImmutableSet.of(AuthAction.produce));

        log.info("user1 produces some messages");
        try (PulsarClient client = buildClient("user1");
             Producer<String> producer = client.newProducer(Schema.STRING).topic("tenant1/ns1/foobar").create()) {
            producer.send("foobar");
        }

        log.info("Deleting the topic");
        admin.topics().delete("tenant1/ns1/foobar", true);

        log.info("Deleting namespace");
        admin.namespaces().deleteNamespace("tenant1/ns1");
    }
}
 
Example #8
Source File: PulsarBoltTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
protected void setup() throws Exception {
    super.internalSetup();
    super.producerBaseSetup();

    serviceUrl = pulsar.getWebServiceAddress();

    pulsarBoltConf = new PulsarBoltConfiguration();
    pulsarBoltConf.setServiceUrl(serviceUrl);
    pulsarBoltConf.setTopic(topic);
    pulsarBoltConf.setTupleToMessageMapper(tupleToMessageMapper);
    pulsarBoltConf.setMetricsTimeIntervalInSecs(60);
    bolt = new PulsarBolt(pulsarBoltConf, PulsarClient.builder());
    mockCollector = new MockOutputCollector();
    OutputCollector collector = new OutputCollector(mockCollector);
    TopologyContext context = mock(TopologyContext.class);
    when(context.getThisComponentId()).thenReturn("test-bolt-" + methodName);
    when(context.getThisTaskId()).thenReturn(0);
    bolt.prepare(Maps.newHashMap(), context, collector);
    consumer = pulsarClient.newConsumer().topic(topic).subscriptionName(subscriptionName).subscribe();
}
 
Example #9
Source File: ProxyKeyStoreTlsTestWithoutAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected PulsarClient internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    ClientBuilder clientBuilder = PulsarClient.builder()
            .serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    return clientBuilder.build();
}
 
Example #10
Source File: PulsarSpoutTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoSharedConsumer() throws Exception {
    TopicStats topicStats = admin.topics().getStats(topic);
    assertEquals(topicStats.subscriptions.get(subscriptionName).consumers.size(), 1);
    pulsarSpoutConf.setSharedConsumerEnabled(false);
    PulsarSpout otherSpout = new PulsarSpout(pulsarSpoutConf, PulsarClient.builder());
    MockSpoutOutputCollector otherMockCollector = new MockSpoutOutputCollector();
    SpoutOutputCollector collector = new SpoutOutputCollector(otherMockCollector);
    TopologyContext context = mock(TopologyContext.class);
    when(context.getThisComponentId()).thenReturn("test-spout-" + methodName);
    when(context.getThisTaskId()).thenReturn(1);
    otherSpout.open(Maps.newHashMap(), context, collector);

    topicStats = admin.topics().getStats(topic);
    assertEquals(topicStats.subscriptions.get(subscriptionName).consumers.size(), 2);

    otherSpout.close();

    topicStats = admin.topics().getStats(topic);
    assertEquals(topicStats.subscriptions.get(subscriptionName).consumers.size(), 1);
}
 
Example #11
Source File: ThreadRuntimeFactory.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void initialize(String threadGroupName, PulsarClient pulsarClient, String storageServiceUrl,
                        SecretsProviderConfigurator secretsProviderConfigurator, SecretsProvider secretsProvider,
                        CollectorRegistry collectorRegistry,  String narExtractionDirectory, ClassLoader rootClassLoader) {
    if (rootClassLoader == null) {
        rootClassLoader = Thread.currentThread().getContextClassLoader();
    }

    this.rootClassLoader = rootClassLoader;
    this.secretsProviderConfigurator = secretsProviderConfigurator;
    this.defaultSecretsProvider = secretsProvider;
    this.fnCache = new FunctionCacheManagerImpl(rootClassLoader);
    this.threadGroup = new ThreadGroup(threadGroupName);
    this.pulsarClient = pulsarClient;
    this.storageServiceUrl = storageServiceUrl;
    this.collectorRegistry = collectorRegistry;
    this.narExtractionDirectory = narExtractionDirectory;
}
 
Example #12
Source File: KeyStoreTlsProducerConsumerTestWithAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    Set<String> tlsProtocols = Sets.newConcurrentHashSet();
    tlsProtocols.add("TLSv1.2");

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .tlsProtocols(tlsProtocols)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example #13
Source File: SampleConsumer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws PulsarClientException, InterruptedException {

        PulsarClient pulsarClient = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();

        Consumer<byte[]> consumer = pulsarClient.newConsumer() //
                .topic("persistent://my-tenant/my-ns/my-topic") //
                .subscriptionName("my-subscription-name").subscribe();

        Message<byte[]> msg = null;

        for (int i = 0; i < 100; i++) {
            msg = consumer.receive();
            // do something
            System.out.println("Received: " + new String(msg.getData()));
        }

        // Acknowledge the consumption of all messages at once
        try {
            consumer.acknowledgeCumulative(msg);
        } catch (Exception e) {
            consumer.reconsumeLater(msg, 10, TimeUnit.SECONDS);
        }
       
        pulsarClient.close();
    }
 
Example #14
Source File: PulsarSinkBuilder.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
private PulsarSinkContext(
        @Nonnull ILogger logger,
        @Nonnull String topic,
        @Nonnull PulsarClient client,
        @Nonnull Map<String, Object> producerConfig,
        @Nonnull SupplierEx<Schema<M>> schemaSupplier,
        @Nonnull FunctionEx<? super E, M> extractValueFn,
        @Nullable FunctionEx<? super E, String> extractKeyFn,
        @Nullable FunctionEx<? super E, Map<String, String>> extractPropertiesFn,
        @Nullable FunctionEx<? super E, Long> extractTimestampFn
) throws PulsarClientException {
    this.logger = logger;
    this.client = client;
    this.producer = client.newProducer(schemaSupplier.get())
                          .topic(topic)
                          .loadConf(producerConfig)
                          .create();
    this.extractKeyFn = extractKeyFn;
    this.extractValueFn = extractValueFn;
    this.extractPropertiesFn = extractPropertiesFn;
    this.extractTimestampFn = extractTimestampFn;
}
 
Example #15
Source File: BuildersTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void enableTlsTest() {
    ClientBuilderImpl builder = (ClientBuilderImpl)PulsarClient.builder().serviceUrl("pulsar://service:6650");
    assertFalse(builder.conf.isUseTls());
    assertEquals(builder.conf.getServiceUrl(), "pulsar://service:6650");

    builder = (ClientBuilderImpl)PulsarClient.builder().serviceUrl("http://service:6650");
    assertFalse(builder.conf.isUseTls());
    assertEquals(builder.conf.getServiceUrl(), "http://service:6650");

    builder = (ClientBuilderImpl)PulsarClient.builder().serviceUrl("pulsar+ssl://service:6650");
    assertTrue(builder.conf.isUseTls());
    assertEquals(builder.conf.getServiceUrl(), "pulsar+ssl://service:6650");

    builder = (ClientBuilderImpl)PulsarClient.builder().serviceUrl("https://service:6650");
    assertTrue(builder.conf.isUseTls());
    assertEquals(builder.conf.getServiceUrl(), "https://service:6650");

    builder = (ClientBuilderImpl)PulsarClient.builder().serviceUrl("pulsar://service:6650").enableTls(true);
    assertTrue(builder.conf.isUseTls());
    assertEquals(builder.conf.getServiceUrl(), "pulsar://service:6650");

    builder = (ClientBuilderImpl)PulsarClient.builder().serviceUrl("pulsar+ssl://service:6650").enableTls(false);
    assertTrue(builder.conf.isUseTls());
    assertEquals(builder.conf.getServiceUrl(), "pulsar+ssl://service:6650");
}
 
Example #16
Source File: PulsarContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
protected void testPulsarFunctionality(String pulsarBrokerUrl) throws Exception {

        try (
            PulsarClient client = PulsarClient.builder()
                .serviceUrl(pulsarBrokerUrl)
                .build();
            Consumer consumer = client.newConsumer()
                .topic(TEST_TOPIC)
                .subscriptionName("test-subs")
                .subscribe();
            Producer<byte[]> producer = client.newProducer()
                .topic(TEST_TOPIC)
                .create()
        ) {

            producer.send("test containers".getBytes());
            CompletableFuture<Message> future = consumer.receiveAsync();
            Message message = future.get(5, TimeUnit.SECONDS);

            assertThat(new String(message.getData()))
                .isEqualTo("test containers");
        }
    }
 
Example #17
Source File: CaseController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/healthCheck")
@ResponseBody
public String healthCheck() throws InterruptedException {
    try (PulsarClient pulsarClient = PulsarClient.builder()
                                                 .serviceUrl(PULSAR_DOMAIN + serviceUrl)
                                                 .build(); Producer<byte[]> producer = pulsarClient.newProducer()
                                                                                                   .topic("healthCheck")
                                                                                                   .create()) {
        if (producer.isConnected()) {
            return "Success";
        } else {
            throw new RuntimeException("Health check error, the reason is test producer is disconnected!");
        }
    } catch (PulsarClientException e) {
        if (e instanceof PulsarClientException.BrokerMetadataException) {
            // Broker is not ready, retry here
            Thread.sleep(1000);
            return healthCheck();
        } else {
            throw new RuntimeException(e);
        }
    }
}
 
Example #18
Source File: TopicDoesNotExistsTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateConsumerOnNotExistsTopic() throws PulsarClientException, InterruptedException {
    PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 1);
    try {
        pulsarClient.newConsumer()
                .topic("persistent://public/default/" + UUID.randomUUID().toString())
                .subscriptionName("test")
                .subscribe();
        Assert.fail("Create consumer should failed while topic does not exists.");
    } catch (PulsarClientException ignore) {
    }
    Thread.sleep(2000);
    HashedWheelTimer timer = (HashedWheelTimer) ((PulsarClientImpl) pulsarClient).timer();
    Assert.assertEquals(timer.pendingTimeouts(), 0);
    Assert.assertEquals(((PulsarClientImpl) pulsarClient).consumersCount(), 0);
}
 
Example #19
Source File: BrokerClientIntegrationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testProducerConsumerWithSpecifiedReaderAndWriter() throws PulsarClientException {
    final String topicName = "persistent://my-property/my-ns/my-topic1";
    ObjectMapper mapper = new ObjectMapper();
    SchemaReader<TestMessageObject> reader = Mockito.spy(new JacksonJsonReader<>(mapper, TestMessageObject.class));
    SchemaWriter<TestMessageObject> writer = Mockito.spy(new JacksonJsonWriter<>(mapper));

    SchemaDefinition<TestMessageObject> schemaDefinition = new SchemaDefinitionBuilderImpl<TestMessageObject>()
            .withPojo(TestMessageObject.class)
            .withSchemaReader(reader)
            .withSchemaWriter(writer)
            .build();
    Schema<TestMessageObject> schema = Schema.JSON(schemaDefinition);
    PulsarClient client =  PulsarClient.builder()
            .serviceUrl(lookupUrl.toString())
            .build();

    try(Producer<TestMessageObject> producer = client.newProducer(schema).topic(topicName).create();
        Consumer<TestMessageObject> consumer = client.newConsumer(schema).topic(topicName).subscriptionName("my-subscriber-name").subscribe()) {
        assertNotNull(producer);
        assertNotNull(consumer);

        TestMessageObject object = new TestMessageObject();
        object.setValue("fooooo");
        producer.newMessage().value(object).send();

        TestMessageObject testObject = consumer.receive().getValue();
        Assert.assertEquals(object.getValue(), testObject.getValue());

        Mockito.verify(writer, Mockito.times(1)).write(Mockito.any());
        Mockito.verify(reader, Mockito.times(1)).read(Mockito.any(byte[].class));
    }
}
 
Example #20
Source File: BrokerClientIntegrationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testCleanProducer() throws Exception {
    log.info("-- Starting {} test --", methodName);

    admin.clusters().createCluster("global", new ClusterData());
    admin.namespaces().createNamespace("my-property/global/lookup");

    final int operationTimeOut = 500;
    PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString())
            .statsInterval(0, TimeUnit.SECONDS).operationTimeout(operationTimeOut, TimeUnit.MILLISECONDS).build();
    CountDownLatch latch = new CountDownLatch(1);
    pulsarClient.newProducer().topic("persistent://my-property/global/lookup/my-topic1").createAsync()
            .handle((producer, e) -> {
                latch.countDown();
                return null;
            });

    latch.await(operationTimeOut + 1000, TimeUnit.MILLISECONDS);
    Field prodField = PulsarClientImpl.class.getDeclaredField("producers");
    prodField.setAccessible(true);
    @SuppressWarnings("unchecked")
    IdentityHashMap<ProducerBase<byte[]>, Boolean> producers = (IdentityHashMap<ProducerBase<byte[]>, Boolean>) prodField
            .get(pulsarClient);
    assertTrue(producers.isEmpty());
    pulsarClient.close();
    log.info("-- Exiting {} test --", methodName);
}
 
Example #21
Source File: BuildersTest.java    From pulsar with 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 #22
Source File: BrokerBkEnsemblesTests.java    From pulsar with 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+"/`~!@#$%^&*()-_+=[]://{}|\\;:'\"<>,./?-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 #23
Source File: ProxyKeyStoreTlsTestWithAuth.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testProducer() throws Exception {
    @Cleanup
    PulsarClient client = internalSetUpForClient(true, proxyService.getServiceUrlTls());
    @Cleanup
    Producer<byte[]> producer = client.newProducer(Schema.BYTES)
            .topic("persistent://sample/test/local/topic" + System.currentTimeMillis())
            .create();

    for (int i = 0; i < 10; i++) {
        producer.send("test".getBytes());
    }
}
 
Example #24
Source File: BrokerClientIntegrationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * It verifies that client closes the connection on internalSerevrError which is "ServiceNotReady" from Broker-side
 *
 * @throws Exception
 */
@Test
public void testCloseConnectionOnInternalServerError() throws Exception {

    final PulsarClient pulsarClient;

    final String topicName = "persistent://prop/usw/my-ns/newTopic";

    pulsarClient = PulsarClient.builder()
            .serviceUrl(pulsar.getBrokerServiceUrl())
            .statsInterval(0, TimeUnit.SECONDS)
            .operationTimeout(1000, TimeUnit.MILLISECONDS)
            .build();

    ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) pulsarClient.newProducer().topic(topicName).create();
    ClientCnx cnx = producer.cnx();
    assertTrue(cnx.channel().isActive());

    // Need broker to throw InternalServerError. so, make global-zk unavailable
    Field globalZkCacheField = PulsarService.class.getDeclaredField("globalZkCache");
    globalZkCacheField.setAccessible(true);
    GlobalZooKeeperCache oldZkCache = (GlobalZooKeeperCache) globalZkCacheField.get(pulsar);
    globalZkCacheField.set(pulsar, null);

    oldZkCache.close();

    try {
        pulsarClient.newProducer().topic(topicName).create();
        fail("it should have fail with lookup-exception:");
    } catch (Exception e) {
        // ok
    }
    // connection must be closed
    assertFalse(cnx.channel().isActive());
    pulsarClient.close();
}
 
Example #25
Source File: SparkStreamingPulsarReceiver.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void onStart() {
    try {
        pulsarClient = PulsarClient.builder().serviceUrl(serviceUrl).authentication(authentication).build();
        consumer = ((PulsarClientImpl) pulsarClient).subscribeAsync(conf).join();
    } catch (Exception e) {
        LOG.error("Failed to start subscription : {}", e.getMessage());
        restart("Restart a consumer");
    }
}
 
Example #26
Source File: ConsumerUnsubscribeTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@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 #27
Source File: PulsarProducerKafkaConfig.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static ProducerBuilder<byte[]> getProducerBuilder(PulsarClient client, Properties properties) {
    ProducerBuilder<byte[]> producerBuilder = client.newProducer();

    if (properties.containsKey(PRODUCER_NAME)) {
        producerBuilder.producerName(properties.getProperty(PRODUCER_NAME));
    }

    if (properties.containsKey(INITIAL_SEQUENCE_ID)) {
        producerBuilder.initialSequenceId(Long.parseLong(properties.getProperty(INITIAL_SEQUENCE_ID)));
    }

    if (properties.containsKey(MAX_PENDING_MESSAGES)) {
        producerBuilder.maxPendingMessages(Integer.parseInt(properties.getProperty(MAX_PENDING_MESSAGES)));
    }

    if (properties.containsKey(MAX_PENDING_MESSAGES_ACROSS_PARTITIONS)) {
        producerBuilder.maxPendingMessagesAcrossPartitions(
                Integer.parseInt(properties.getProperty(MAX_PENDING_MESSAGES_ACROSS_PARTITIONS)));
    }

    producerBuilder.enableBatching(Boolean.parseBoolean(properties.getProperty(BATCHING_ENABLED, "true")));

    if (properties.containsKey(BATCHING_MAX_MESSAGES)) {
        producerBuilder.batchingMaxMessages(Integer.parseInt(properties.getProperty(BATCHING_MAX_MESSAGES)));
    }

    return producerBuilder;
}
 
Example #28
Source File: TopicMessagingBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected void nonPartitionedTopicSendAndReceiveWithExclusive(String serviceUrl, boolean isPersistent) throws Exception {
    log.info("-- Starting {} test --", methodName);
    final String topicName = getNonPartitionedTopic("test-non-partitioned-consume-exclusive", isPersistent);
    @Cleanup
    final PulsarClient client = PulsarClient.builder()
            .serviceUrl(serviceUrl)
            .build();
    @Cleanup
    final Consumer<String> consumer = client.newConsumer(Schema.STRING)
            .topic(topicName)
            .subscriptionName("test-sub")
            .subscriptionType(SubscriptionType.Exclusive)
            .subscribe();
    try {
        client.newConsumer(Schema.STRING)
                .topic(topicName)
                .subscriptionName("test-sub")
                .subscriptionType(SubscriptionType.Exclusive)
                .subscribe();
        fail("should be failed");
    } catch (PulsarClientException ignore) {
    }
    final int messagesToSend = 10;
    final String producerName = "producerForExclusive";
    @Cleanup
    final Producer<String> producer = client.newProducer(Schema.STRING)
            .topic(topicName)
            .enableBatching(false)
            .producerName(producerName)
            .create();
    for (int i = 0; i < messagesToSend; i++) {
        MessageId messageId = producer.newMessage().value(producer.getProducerName() + "-" + i).send();
        assertNotNull(messageId);
    }
    log.info("public messages complete.");
    receiveMessagesCheckOrderAndDuplicate(Collections.singletonList(consumer), messagesToSend);
    log.info("-- Exiting {} test --", methodName);
}
 
Example #29
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 #30
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);
    }
}