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

The following examples show how to use org.apache.pulsar.client.api.Producer. 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: 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 #2
Source File: PulsarTestSupport.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
private static Producer<byte[]> getProducer(String topicName) throws PulsarClientException {
    // If there exists a producer with same name returns it.
    if (!producerMap.containsKey(topicName)) {
        Producer<byte[]> newProducer = getClient()
                .newProducer()
                .topic(topicName)
                .batchingMaxPublishDelay(10, TimeUnit.MILLISECONDS)
                .sendTimeout(10, TimeUnit.SECONDS)
                .blockIfQueueFull(true)
                .create();
        producerMap.put(topicName, newProducer);
        return newProducer;
    } else {
        return producerMap.get(topicName);
    }
}
 
Example #3
Source File: PulsarStateTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static void publishAndConsumeMessages(String inputTopic,
                                              String outputTopic,
                                              int numMessages) throws Exception {
    @Cleanup PulsarClient client = PulsarClient.builder()
        .serviceUrl(container.getPlainTextServiceUrl())
        .build();
    @Cleanup Consumer<byte[]> consumer = client.newConsumer(Schema.BYTES)
        .topic(outputTopic)
        .subscriptionType(SubscriptionType.Exclusive)
        .subscriptionName("test-sub")
        .subscribe();
    @Cleanup Producer<byte[]> producer = client.newProducer(Schema.BYTES)
        .topic(inputTopic)
        .create();

    for (int i = 0; i < numMessages; i++) {
        producer.send(("hello test message-" + i).getBytes(UTF_8));
    }

    for (int i = 0; i < numMessages; i++) {
        Message<byte[]> msg = consumer.receive();
        assertEquals("hello test message-" + i + "!", new String(msg.getValue(), UTF_8));
    }
}
 
Example #4
Source File: AdminApiTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testBacklogSizeShouldBeZeroWhenConsumerAckedAllMessages() throws Exception {
    final String topic = "persistent://prop-xyz/ns1/testBacklogSizeShouldBeZeroWhenConsumerAckedAllMessages";
    Consumer<byte[]> consumer = pulsarClient.newConsumer()
            .topic(topic)
            .subscriptionName("sub-1")
            .subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer()
            .topic(topic)
            .create();

    final int messages = 33;
    for (int i = 0; i < messages; i++) {
        producer.send(new byte[1024 * i * 5]);
    }

    for (int i = 0; i < messages; i++) {
        consumer.acknowledgeCumulative(consumer.receive());
    }

    // Wait ack send
    Thread.sleep(1000);

    TopicStats topicStats = admin.topics().getStats(topic);
    assertEquals(topicStats.backlogSize, 0);
}
 
Example #5
Source File: InactiveTopicDeleteTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testMaxInactiveDuration() throws Exception {
    conf.setBrokerDeleteInactiveTopicsMode(InactiveTopicDeleteMode.delete_when_subscriptions_caught_up);
    conf.setBrokerDeleteInactiveTopicsFrequencySeconds(1);
    conf.setBrokerDeleteInactiveTopicsMaxInactiveDurationSeconds(5);
    super.baseSetup();

    final String topic = "persistent://prop/ns-abc/testMaxInactiveDuration";

    Producer<byte[]> producer = pulsarClient.newProducer()
        .topic(topic)
        .create();

    producer.close();
    Thread.sleep(2000);
    Assert.assertTrue(admin.topics().getList("prop/ns-abc")
        .contains(topic));

    Thread.sleep(4000);
    Assert.assertFalse(admin.topics().getList("prop/ns-abc")
        .contains(topic));

    super.internalCleanup();
}
 
Example #6
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 #7
Source File: LoadSimulationClient.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public void start() throws Exception {
    Producer<byte[]> producer = getNewProducer();
    final Consumer<byte[]> consumer = consumerFuture.get();
    while (!stop.get()) {
        final MutableBoolean wellnessFlag = new MutableBoolean();
        final Function<Throwable, ? extends MessageId> exceptionHandler = e -> {
            // Unset the well flag in the case of an exception so we can
            // try to get a new Producer.
            wellnessFlag.value = false;
            return null;
        };
        while (!stop.get() && wellnessFlag.value) {
            producer.sendAsync(payload.get()).exceptionally(exceptionHandler);
            rateLimiter.acquire();
        }
        producer.closeAsync();
        if (!stop.get()) {
            // The Producer failed due to an exception: attempt to get
            // another producer.
            producer = getNewProducer();
        } else {
            // We are finished: close the consumer.
            consumer.closeAsync();
        }
    }
}
 
Example #8
Source File: NonPersistentReplicator.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
protected void readEntries(Producer<byte[]> producer) {
    this.producer = (ProducerImpl) producer;

    if (STATE_UPDATER.compareAndSet(this, State.Starting, State.Started)) {
        log.info("[{}][{} -> {}] Created replicator producer", topicName, localCluster, remoteCluster);
        backOff.reset();
    } else {
        log.info(
                "[{}][{} -> {}] Replicator was stopped while creating the producer. Closing it. Replicator state: {}",
                topicName, localCluster, remoteCluster, STATE_UPDATER.get(this));
        STATE_UPDATER.set(this, State.Stopping);
        closeProducerAsync();
        return;
    }
}
 
Example #9
Source File: FlinkPulsarSinkTest.java    From pulsar-flink with Apache License 2.0 6 votes vote down vote up
/**
 * This test is meant to assure that testAtLeastOnceProducer is valid by testing that if flushing is disabled,
 * the snapshot method does indeed finishes without waiting for pending records;
 * we set a timeout because the test will not finish if the logic is broken.
 */
@SuppressWarnings("unchecked")
@Test//(timeout = 5000)
public void testDoesNotWaitForPendingRecordsIfFlushingDisabled() throws Throwable {
    Properties props = dummyProperties();
    props.setProperty("flushoncheckpoint", "false");

    final DummyFlinkPulsarSink<String> sink = new DummyFlinkPulsarSink<>(dummyClientConf(), props, mock(TopicKeyExtractor.class), null);

    final Producer mockProducer = sink.getProducer("tp");

    final OneInputStreamOperatorTestHarness<String, Object> testHarness =
            new OneInputStreamOperatorTestHarness<>(new StreamSink<>(sink));

    testHarness.open();

    testHarness.processElement(new StreamRecord<>("msg"));

    // make sure that all callbacks have not been completed
    verify(mockProducer, times(1)).newMessage();

    // should return even if there are pending records
    testHarness.snapshot(123L, 123L);

    testHarness.close();
}
 
Example #10
Source File: AdminApiSchemaValidationEnforced.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnableSchemaValidationEnforcedNoSchema() throws Exception {
    admin.namespaces().createNamespace("schema-validation-enforced/enable-no-schema");
    String namespace = "schema-validation-enforced/enable-no-schema";
    String topicName = "persistent://schema-validation-enforced/enable-no-schema/test";
    assertFalse(admin.namespaces().getSchemaValidationEnforced(namespace));
    admin.namespaces().setSchemaValidationEnforced(namespace,true);
    try {
        admin.schemas().getSchemaInfo(topicName);
    } catch (PulsarAdminException.NotFoundException e) {
        assertTrue(e.getMessage().contains("HTTP 404 Not Found"));
    }
    try (Producer p = pulsarClient.newProducer().topic(topicName).create()) {
        p.send("test schemaValidationEnforced".getBytes());
    }
}
 
Example #11
Source File: AdminApiSchemaValidationEnforced.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnableSchemaValidationEnforcedHasSchemaMatch() throws Exception {
    admin.namespaces().createNamespace("schema-validation-enforced/enable-has-schema-match");
    String namespace = "schema-validation-enforced/enable-has-schema-match";
    String topicName = "persistent://schema-validation-enforced/enable-has-schema-match/test";
    assertFalse(admin.namespaces().getSchemaValidationEnforced(namespace));
    try {
        admin.schemas().getSchemaInfo(topicName);
    } catch (PulsarAdminException.NotFoundException e) {
        assertTrue(e.getMessage().contains("HTTP 404 Not Found"));
    }
    admin.namespaces().setSchemaValidationEnforced(namespace,true);
    Map<String, String> properties = Maps.newHashMap();
    SchemaInfo schemaInfo = new SchemaInfo();
    schemaInfo.setType(SchemaType.STRING);
    schemaInfo.setProperties(properties);
    schemaInfo.setName("test");
    schemaInfo.setSchema("".getBytes());
    PostSchemaPayload postSchemaPayload = new PostSchemaPayload("STRING", "", properties);
    admin.schemas().createSchema(topicName, postSchemaPayload);
    try (Producer<String> p = pulsarClient.newProducer(Schema.STRING).topic(topicName).create()) {
        p.send("test schemaValidationEnforced");
    }
    assertEquals(admin.schemas().getSchemaInfo(topicName).getName(), schemaInfo.getName());
    assertEquals(admin.schemas().getSchemaInfo(topicName).getType(), schemaInfo.getType());
}
 
Example #12
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 #13
Source File: PartitionedProducerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public PartitionedProducerImpl(PulsarClientImpl client, String topic, ProducerConfigurationData conf, int numPartitions,
        CompletableFuture<Producer<T>> producerCreatedFuture, Schema<T> schema, ProducerInterceptors interceptors) {
    super(client, topic, conf, producerCreatedFuture, schema, interceptors);
    this.producers = Lists.newArrayListWithCapacity(numPartitions);
    this.topicMetadata = new TopicMetadataImpl(numPartitions);
    this.routerPolicy = getMessageRouter();
    stats = client.getConfiguration().getStatsIntervalSeconds() > 0 ? new ProducerStatsRecorderImpl() : null;

    int maxPendingMessages = Math.min(conf.getMaxPendingMessages(),
            conf.getMaxPendingMessagesAcrossPartitions() / numPartitions);
    conf.setMaxPendingMessages(maxPendingMessages);
    start();

    // start track and auto subscribe partition increasement
    if (conf.isAutoUpdatePartitions()) {
        topicsPartitionChangedListener = new TopicsPartitionChangedListener();
        partitionsAutoUpdateTimeout = client.timer()
            .newTimeout(partitionsAutoUpdateTimerTask, 1, TimeUnit.MINUTES);
    }
}
 
Example #14
Source File: V1_AdminApiTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamespaceSplitBundle() throws Exception {
    // Force to create a topic
    final String namespace = "prop-xyz/use/ns1";
    final String topicName = (new StringBuilder("persistent://")).append(namespace).append("/ds2").toString();
    Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES)
        .topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();
    producer.send("message".getBytes());
    publishMessagesOnPersistentTopic(topicName, 0);
    assertEquals(admin.topics().getList(namespace), Lists.newArrayList(topicName));

    try {
        admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0xffffffff", true, null);
    } catch (Exception e) {
        fail("split bundle shouldn't have thrown exception");
    }

    // bundle-factory cache must have updated split bundles
    NamespaceBundles bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
    String[] splitRange = { namespace + "/0x00000000_0x7fffffff", namespace + "/0x7fffffff_0xffffffff" };
    for (int i = 0; i < bundles.getBundles().size(); i++) {
        assertEquals(bundles.getBundles().get(i).toString(), splitRange[i]);
    }

    producer.close();
}
 
Example #15
Source File: BasePulsarOutputFormat.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private Producer<byte[]> createPulsarProducer()
        throws PulsarClientException {
    try {
        PulsarClientImpl client = new PulsarClientImpl(clientConf);
        return client.createProducerAsync(producerConf).get();
    } catch (PulsarClientException | InterruptedException | ExecutionException e) {
        LOG.error("Pulsar producer cannot be created.", e);
        throw new PulsarClientException(e);
    }
}
 
Example #16
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 #17
Source File: PulsarClientTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void test(final MockTracer tracer, final boolean async) throws Exception {
  if (!isJdkSupported) {
    logger.warning("jdk" + System.getProperty("java.version") + " is not supported by Pulsar");
    return;
  }

  try (
    final PulsarClient client = PulsarClient.builder().serviceUrl(pulsarService.getBrokerServiceUrl()).build();
    final Consumer<byte[]> consumer = client.newConsumer().topic("my-topic").subscriptionName("my-subscription").subscribe();
    final Producer<byte[]> producer = client.newProducer().topic("my-topic").create();
  ) {
    if (async)
      producer.sendAsync("My message".getBytes()).get(15, TimeUnit.SECONDS);
    else
      producer.send("My message".getBytes());

    final Message<byte[]> message;
    if (async)
      message = consumer.receiveAsync().get(15, TimeUnit.SECONDS);
    else
      message = consumer.receive();

    System.out.println("Message received: " + new String(message.getData()));
    consumer.acknowledge(message);
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
  assertNull(tracer.activeSpan());
  for (final MockSpan span : spans)
    assertEquals(PulsarClientAgentIntercept.COMPONENT_NAME, span.tags().get(Tags.COMPONENT.getKey()));

  assertEquals(spans.get(0).context().traceId(), spans.get(1).context().traceId());
}
 
Example #18
Source File: BacklogQuotaManagerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAheadProducerOnHold() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/quotahold"),
            ConfigHelper.backlogQuotaMap(config));
    admin.namespaces().setBacklogQuota("prop/quotahold",
            new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
    final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString())
            .statsInterval(0, TimeUnit.SECONDS).build();
    final String topic1 = "persistent://prop/quotahold/hold";
    final String subName1 = "c1hold";
    final int numMsgs = 10;

    Consumer<byte[]> consumer = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();

    byte[] content = new byte[1024];
    Producer<byte[]> producer = client.newProducer().topic(topic1).sendTimeout(2, TimeUnit.SECONDS).create();
    for (int i = 0; i <= numMsgs; i++) {
        try {
            producer.send(content);
            LOG.info("sent [{}]", i);
        } catch (PulsarClientException.TimeoutException cte) {
            // producer close may cause a timeout on send
            LOG.info("timeout on [{}]", i);
        }
    }

    for (int i = 0; i < numMsgs; i++) {
        consumer.receive();
        LOG.info("received [{}]", i);
    }

    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    rolloverStats();
    TopicStats stats = admin.topics().getStats(topic1);
    assertEquals(stats.publishers.size(), 0,
            "Number of producers on topic " + topic1 + " are [" + stats.publishers.size() + "]");
    client.close();
}
 
Example #19
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 #20
Source File: PrometheusMetricsTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testManagedLedgerCacheStats() throws Exception {
    Producer<byte[]> p1 = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic1").create();
    Producer<byte[]> p2 = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic2").create();
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        p1.send(message.getBytes());
        p2.send(message.getBytes());
    }

    ByteArrayOutputStream statsOut = new ByteArrayOutputStream();
    PrometheusMetricsGenerator.generate(pulsar, false, false, statsOut);
    String metricsStr = new String(statsOut.toByteArray());

    Multimap<String, Metric> metrics = parseMetrics(metricsStr);

    metrics.entries().forEach(e ->
            System.out.println(e.getKey() + ": " + e.getValue())
    );

    List<Metric> cm = (List<Metric>) metrics.get("pulsar_ml_cache_evictions");
    assertEquals(cm.size(), 1);
    assertEquals(cm.get(0).tags.get("cluster"), "test");

    cm = (List<Metric>) metrics.get("pulsar_ml_cache_hits_rate");
    assertEquals(cm.size(), 1);
    assertEquals(cm.get(0).tags.get("cluster"), "test");

    p1.close();
    p2.close();
}
 
Example #21
Source File: CompactorTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompactedInOrder() throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-topic1";

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

    producer.newMessage()
            .key("c")
            .value("C_1".getBytes()).send();
    producer.newMessage()
            .key("a")
            .value("A_1".getBytes()).send();
    producer.newMessage()
            .key("b")
            .value("B_1".getBytes()).send();
    producer.newMessage()
            .key("a")
            .value("A_2".getBytes()).send();
    Map<String, byte[]> expected = new HashMap<>();
    expected.put("a", "A_2".getBytes());
    expected.put("b", "B_1".getBytes());
    expected.put("c", "C_1".getBytes());

    List<String> keyOrder = compactAndVerify(topic, expected);

    Assert.assertEquals(keyOrder, Lists.newArrayList("c", "b", "a"));
}
 
Example #22
Source File: PersistentTopicE2ETest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * A topic that has retention policy set to non-0, should not be GCed until it has been inactive for at least the
 * retention time.
 */
@Test
public void testGcAndRetentionPolicy() throws Exception {

    // Retain data for at-least 10min
    admin.namespaces().setRetention("prop/ns-abc", new RetentionPolicies(10, 10));

    // 1. Simple successful GC
    String topicName = "persistent://prop/ns-abc/topic-10";
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    producer.close();

    assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));
    runGC();
    // Should not have been deleted, since we have retention
    assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));

    // Remove retention
    admin.namespaces().setRetention("prop/ns-abc", new RetentionPolicies(0, 10));
    Thread.sleep(300);

    // 2. Topic is not GCed with live connection
    String subName = "sub1";
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();

    runGC();
    assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));

    // 3. Topic with subscription is not GCed even with no connections
    consumer.close();

    runGC();
    assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));

    // 4. Topic can be GCed after unsubscribe
    admin.topics().deleteSubscription(topicName, subName);

    runGC();
    assertFalse(pulsar.getBrokerService().getTopicReference(topicName).isPresent());
}
 
Example #23
Source File: ProxyTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitions() throws Exception {
    TenantInfo tenantInfo = createDefaultTenantInfo();
    admin.tenants().createTenant("sample", tenantInfo);
    @Cleanup
    PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl())
            .build();
    admin.topics().createPartitionedTopic("persistent://sample/test/local/partitioned-topic", 2);

    @Cleanup
    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
    @Cleanup
    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);
    }
}
 
Example #24
Source File: CompactionTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testWholeBatchCompactedOut() throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-topic1";

    // subscribe before sending anything, so that we get all messages
    pulsarClient.newConsumer().topic(topic).subscriptionName("sub1")
        .readCompacted(true).subscribe().close();

    try (Producer<byte[]> producerNormal = pulsarClient.newProducer().topic(topic)
             .enableBatching(false)
             .messageRoutingMode(MessageRoutingMode.SinglePartition)
             .create();
         Producer<byte[]> producerBatch = pulsarClient.newProducer().topic(topic)
             .maxPendingMessages(3)
             .enableBatching(true)
             .batchingMaxMessages(3)
             .batchingMaxPublishDelay(1, TimeUnit.HOURS)
             .messageRoutingMode(MessageRoutingMode.SinglePartition)
             .create()) {
        producerBatch.newMessage().key("key1").value("my-message-1".getBytes()).sendAsync();
        producerBatch.newMessage().key("key1").value("my-message-2".getBytes()).sendAsync();
        producerBatch.newMessage().key("key1").value("my-message-3".getBytes()).sendAsync();
        producerNormal.newMessage().key("key1").value("my-message-4".getBytes()).send();
    }

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

    try (Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topic)
            .subscriptionName("sub1").readCompacted(true).subscribe()){
        Message<byte[]> message = consumer.receive();
        Assert.assertEquals(message.getKey(), "key1");
        Assert.assertEquals(new String(message.getData()), "my-message-4");
    }
}
 
Example #25
Source File: PulsarDatabaseHistoryTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ParsingException.class)
public void shouldStopOnUnparseableSQL() throws Exception {
    try (final Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic(topicName).create()) {
        producer.send("{\"source\":{\"server\":\"my-server\"},\"position\":{\"filename\":\"my-txn-file.log\",\"position\":39},\"databaseName\":\"db1\",\"ddl\":\"xxxDROP TABLE foo;\"}");
    }

    testHistoryTopicContent(false);
}
 
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: ExclusiveSubscriptionTutorial.java    From pulsar-java-tutorial with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws PulsarClientException {
    PulsarClient client = PulsarClient.builder()
            .serviceUrl(SERVICE_URL)
            .build();

    Producer<byte[]> producer = client.newProducer()
            .topic(TOPIC_NAME)
            .create();

    ConsumerBuilder<byte[]> consumer1 = client.newConsumer()
            .topic(TOPIC_NAME)
            .subscriptionName(SUBSCRIPTION_NAME)
            .subscriptionType(SUBSCRIPTION_TYPE);

    ConsumerBuilder<byte[]> consumer2 = client.newConsumer()
            .topic(TOPIC_NAME)
            .subscriptionName(SUBSCRIPTION_NAME)
            .subscriptionType(SUBSCRIPTION_TYPE);

    IntStream.range(0, 999).forEach(i -> {
        Message<byte[]> msg = MessageBuilder.create()
                .setContent(String.format("message-%d", i).getBytes())
                .build();
        try {
            producer.send(msg);
        } catch (PulsarClientException e) {
            LOG.error(e.getMessage());
        }
    });

    // Consumer 1 can subscribe to the topic
    consumer1.subscribe();

    // But oops! Consumer 2 cannot due to the exclusive subscription held by consumer 1
    consumer2.subscribeAsync()
            .handle((consumer, exception) -> {
                LOG.error(exception.getMessage());
                return null;
            });
}
 
Example #28
Source File: BatchMessageTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "containerBuilder")
public void testRetrieveSequenceIdGenerated(BatcherBuilder builder) throws Exception {

    int numMsgs = 10;
    final String topicName = "persistent://prop/ns-abc/testRetrieveSequenceIdGenerated-" + UUID.randomUUID();
    final String subscriptionName = "sub-1";

    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName)
            .subscriptionType(SubscriptionType.Shared).subscribe();

    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
            .batchingMaxPublishDelay(5, TimeUnit.SECONDS).batchingMaxMessages(numMsgs).enableBatching(true)
            .batcherBuilder(builder)
            .create();

    List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
    for (int i = 0; i < numMsgs; i++) {
        byte[] message = ("my-message-" + i).getBytes();
        sendFutureList.add(producer.sendAsync(message));
    }
    FutureUtil.waitForAll(sendFutureList).get();

    for (int i = 0; i < numMsgs; i++) {
        Message<byte[]> received = consumer.receive();
        Assert.assertEquals(received.getSequenceId(), i);
        consumer.acknowledge(received);
    }

    producer.close();
    consumer.close();
}
 
Example #29
Source File: SampleProducer.java    From pulsar with 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 #30
Source File: BrokerServiceTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicLoadingOnDisableNamespaceBundle() throws Exception {
    final String namespace = "prop/disableBundle";
    admin.namespaces().createNamespace(namespace);
    admin.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("test"));

    // own namespace bundle
    final String topicName = "persistent://" + namespace + "/my-topic";
    TopicName topic = TopicName.get(topicName);
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    producer.close();

    // disable namespace-bundle
    NamespaceBundle bundle = pulsar.getNamespaceService().getBundle(topic);
    pulsar.getNamespaceService().getOwnershipCache().updateBundleState(bundle, false).join();

    // try to create topic which should fail as bundle is disable
    CompletableFuture<Optional<Topic>> futureResult = pulsar.getBrokerService()
            .loadOrCreatePersistentTopic(topicName, true);

    try {
        futureResult.get();
        fail("Topic creation should fail due to disable bundle");
    } catch (Exception e) {
        if (!(e.getCause() instanceof BrokerServiceException.ServiceUnitNotReadyException)) {
            fail("Topic creation should fail with ServiceUnitNotReadyException");
        }

    }
}