Java Code Examples for org.apache.pulsar.client.api.Message#getData()

The following examples show how to use org.apache.pulsar.client.api.Message#getData() . 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: PulsarMessageConverterImpl.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Override
public int convert(BatchMaker batchMaker, Source.Context context, String messageId, Message message)
    throws StageException {
  byte[] payload = message.getData();
  int count = 0;
  if (payload.length > 0) {
    try {
      for (Record record : ServicesUtil.parseAll(context, context, messageConfig.produceSingleRecordPerMessage,
          messageId, payload)) {
        Map<String, String> messageProperties = message.getProperties();
        messageProperties.forEach((key, value) -> record.getHeader().setAttribute(key, value == null ? "" : value));
        batchMaker.addRecord(record);
        ++count;
      }
    } catch (StageException e) {
      handleException(context, messageId, e);
    }
  }
  return count;
}
 
Example 2
Source File: ReplicatorTestBase.java    From pulsar with Apache License 2.0 6 votes vote down vote up
void receive(int messages) throws Exception {
    log.info("Start receiving messages");
    Message<byte[]> msg;

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

    int i = 0;
    while (i < messages) {
        msg = consumer.receive(10, TimeUnit.SECONDS);
        assertNotNull(msg);
        consumer.acknowledge(msg);

        String msgData = new String(msg.getData());
        log.info("Received message {}", msgData);

        boolean added = receivedMessages.add(msgData);
        if (added) {
            assertEquals(msgData, "test-" + i);
            i++;
        } else {
            log.info("Ignoring duplicate {}", msgData);
        }
    }
}
 
Example 3
Source File: FunctionRuntimeManager.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public synchronized void processAssignmentMessage(Message<byte[]> msg) {

        if(msg.getData()==null || (msg.getData().length==0)) {
            log.info("Received assignment delete: {}", msg.getKey());
            deleteAssignment(msg.getKey());
        } else {
            Assignment assignment;
            try {
                assignment = Assignment.parseFrom(msg.getData());
            } catch (IOException e) {
                log.error("[{}] Received bad assignment update at message {}", msg.getMessageId(), e);
                throw new RuntimeException(e);
            }
            log.info("Received assignment update: {}", assignment);
            processAssignment(assignment);
        }
    }
 
Example 4
Source File: ConsumerUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
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();

    //Configure consumer specific settings.
    Consumer<byte[]> consumer = client.newConsumer()
            .topic(TOPIC_NAME)
            // Allow multiple consumers to attach to the same subscription
            // and get messages dispatched as a queue
            .subscriptionType(SubscriptionType.Shared)
            .subscriptionName(SUBSCRIPTION_NAME)
            .subscribe();


    // Once the consumer is created, it can be used for the entire application lifecycle
    System.out.println("Created consumer for the topic "+ TOPIC_NAME);

    do {
        // Wait until a message is available
        Message<byte[]> msg = consumer.receive();

        // Extract the message as a printable string and then log
        String content = new String(msg.getData());
        System.out.println("Received message '"+content+"' with ID "+msg.getMessageId());

        // Acknowledge processing of the message so that it can be deleted
        consumer.acknowledge(msg);
    } while (true);
}
 
Example 5
Source File: V1_ProducerConsumerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnabledChecksumClient() throws Exception {
    log.info("-- Starting {} test --", methodName);

    final int totalMsg = 10;

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

    final int batchMessageDelayMs = 300;
    Producer<byte[]> producer = pulsarClient.newProducer()
            .topic("persistent://my-property/use/my-ns/my-topic1")
            .enableBatching(true)
            .batchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS)
            .batchingMaxMessages(5)
            .create();

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

    Message<byte[]>msg = null;
    Set<String> messageSet = Sets.newHashSet();
    for (int i = 0; i < totalMsg; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("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();
    log.info("-- Exiting {} test --", methodName);
}
 
Example 6
Source File: MessagesImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected void add(Message<T> message) {
    if (message == null) {
        return;
    }
    Preconditions.checkArgument(canAdd(message), "No more space to add messages.");
    currentNumberOfMessages ++;
    currentSizeOfMessages += message.getData().length;
    messageList.add(message);
}
 
Example 7
Source File: MessagesImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected boolean canAdd(Message<T> message) {
    if (maxNumberOfMessages > 0 && currentNumberOfMessages + 1 > maxNumberOfMessages) {
        return false;
    }

    if (maxSizeOfMessages > 0 && currentSizeOfMessages + message.getData().length > maxSizeOfMessages) {
        return false;
    }

    return true;
}
 
Example 8
Source File: SaslAuthenticateTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testProducerAndConsumerPassed() throws Exception {
    log.info("-- {} -- start", methodName);

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

    ProducerBuilder<byte[]> producerBuilder = pulsarClient.newProducer()
        .topic("persistent://my-property/my-ns/my-topic")
        .enableBatching(false);

    Producer<byte[]> producer = producerBuilder.create();
    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();

    log.info("-- {} -- end", methodName);
}
 
Example 9
Source File: CmdConsume.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Interprets the message to create a string representation
 *
 * @param message
 *            The message to interpret
 * @param displayHex
 *            Whether to display BytesMessages in hexdump style, ignored for simple text messages
 * @return String representation of the message
 */
private String interpretMessage(Message<byte[]> message, boolean displayHex) throws IOException {
    StringBuilder sb = new StringBuilder();

    String properties = Arrays.toString(message.getProperties().entrySet().toArray());

    String data;
    byte[] msgData = message.getData();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    if (!displayHex) {
        data = new String(msgData);
    } else {
        HexDump.dump(msgData, 0, out, 0);
        data = new String(out.toByteArray());
    }

    String key = null;
    if (message.hasKey()) {
        key = message.getKey();
    }

    sb.append("key:[").append(key).append("], ");
    sb.append("properties:").append(properties).append(", ");
    sb.append("content:").append(data);

    return sb.toString();
}
 
Example 10
Source File: ConsumerTutorial.java    From pulsar-java-tutorial with Apache License 2.0 5 votes vote down vote up
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 consumer specific settings. eg:
    Consumer<byte[]> consumer = client.newConsumer()
            .topic(TOPIC_NAME)
            // Allow multiple consumers to attach to the same subscription
            // and get messages dispatched as a queue
            .subscriptionType(SubscriptionType.Shared)
            .subscriptionName(SUBSCRIPTION_NAME)
            .subscribe();


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

    do {
        // Wait until a message is available
        Message<byte[]> msg = consumer.receive();

        // Extract the message as a printable string and then log
        String content = new String(msg.getData());
        log.info("Received message '{}' with ID {}", content, msg.getMessageId());

        // Acknowledge processing of the message so that it can be deleted
        consumer.acknowledge(msg);
    } while (true);
}
 
Example 11
Source File: PulsarSpout.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * It makes sure that it emits next available non-tuple to topology unless consumer queue doesn't have any message
 * available. It receives message from consumer queue and converts it to tuple and emits to topology. if the
 * converted tuple is null then it tries to receives next message and perform the same until it finds non-tuple to
 * emit.
 */
public void emitNextAvailableTuple() {
    // check if there are any failed messages to re-emit in the topology
    if(emitFailedMessage()) {
        return;
    }

    Message<byte[]> msg;
    // receive from consumer if no failed messages
    if (consumer != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("[{}] Receiving the next message from pulsar consumer to emit to the collector", spoutId);
        }
        try {
            boolean done = false;
            while (!done) {
                msg = consumer.receive(100, TimeUnit.MILLISECONDS);
                if (msg != null) {
                    ++messagesReceived;
                    messageSizeReceived += msg.getData().length;
                    done = mapToValueAndEmit(msg);
                } else {
                    // queue is empty and nothing to emit
                    done = true;
                    messageNotAvailableCount++;
                }
            }
        } catch (PulsarClientException e) {
            LOG.error("[{}] Error receiving message from pulsar consumer", spoutId, e);
        }
    }
}
 
Example 12
Source File: PulsarSpoutTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public Values toValues(Message msg) {
    if ("message to be dropped".equals(new String(msg.getData()))) {
        return null;
    }
    return new Values(new String(msg.getData()));
}
 
Example 13
Source File: ProxyAuthenticatedProducerConsumerTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * <pre>
 * It verifies e2e tls + Authentication + Authorization (client -> proxy -> broker>
 *
 * 1. client connects to proxy over tls and pass auth-data
 * 2. proxy authenticate client and retrieve client-role
 *    and send it to broker as originalPrincipal over tls
 * 3. client creates producer/consumer via proxy
 * 4. broker authorize producer/consumer create request using originalPrincipal
 *
 * </pre>
 *
 * @throws Exception
 */
@SuppressWarnings("deprecation")
@Test
public void testTlsSyncProducerAndConsumer() throws Exception {
    log.info("-- Starting {} test --", methodName);

    final String proxyServiceUrl = proxyService.getServiceUrlTls();
    Map<String, String> authParams = Maps.newHashMap();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    Authentication authTls = new AuthenticationTls();
    authTls.configure(authParams);
    // create a client which connects to proxy over tls and pass authData
    PulsarClient proxyClient = createPulsarClient(authTls, proxyServiceUrl);

    admin.clusters().createCluster(configClusterName, new ClusterData(brokerUrl.toString(), brokerUrlTls.toString(),
            pulsar.getBrokerServiceUrl(), pulsar.getBrokerServiceUrlTls()));
    admin.tenants().createTenant("my-property",
            new TenantInfo(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet("test")));
    admin.namespaces().createNamespace("my-property/my-ns", Sets.newHashSet("test"));

    Consumer<byte[]> consumer = proxyClient.newConsumer().topic("persistent://my-property/my-ns/my-topic1")
            .subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = proxyClient.newProducer(Schema.BYTES).topic("persistent://my-property/my-ns/my-topic1")
            .create();
    final int msgs = 10;
    for (int i = 0; i < msgs; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }

    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    int count = 0;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
        count++;
    }
    // Acknowledge the consumption of all messages at once
    Assert.assertEquals(msgs, count);
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
 
Example 14
Source File: StormExample.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public Values toValues(Message msg) {
    return new Values(new String(msg.getData()));
}
 
Example 15
Source File: ProxyWithAuthorizationTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * <pre>
 * It verifies e2e tls + Authentication + Authorization (client -> proxy -> broker)
 *
 * 1. client connects to proxy over tls and pass auth-data
 * 2. proxy authenticate client and retrieve client-role
 *    and send it to broker as originalPrincipal over tls
 * 3. client creates producer/consumer via proxy
 * 4. broker authorize producer/consumer create request using originalPrincipal
 *
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testProxyAuthorization() throws Exception {
    log.info("-- Starting {} test --", methodName);

    startProxy();
    createAdminClient();
    // create a client which connects to proxy over tls and pass authData
    PulsarClient proxyClient = createPulsarClient(proxyService.getServiceUrlTls(), PulsarClient.builder());

    String namespaceName = "my-property/proxy-authorization/my-ns";

    admin.clusters().createCluster("proxy-authorization", new ClusterData(brokerUrl.toString()));

    admin.tenants().createTenant("my-property",
            new TenantInfo(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet("proxy-authorization")));
    admin.namespaces().createNamespace(namespaceName);

    admin.namespaces().grantPermissionOnNamespace(namespaceName, "Proxy",
            Sets.newHashSet(AuthAction.consume, AuthAction.produce));
    admin.namespaces().grantPermissionOnNamespace(namespaceName, "Client",
            Sets.newHashSet(AuthAction.consume, AuthAction.produce));

    Consumer<byte[]> consumer = proxyClient.newConsumer()
            .topic("persistent://my-property/proxy-authorization/my-ns/my-topic1")
            .subscriptionName("my-subscriber-name").subscribe();

    Producer<byte[]> producer = proxyClient.newProducer(Schema.BYTES)
            .topic("persistent://my-property/proxy-authorization/my-ns/my-topic1").create();
    final int msgs = 10;
    for (int i = 0; i < msgs; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }

    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    int count = 0;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
        count++;
    }
    // Acknowledge the consumption of all messages at once
    Assert.assertEquals(msgs, count);
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
 
Example 16
Source File: ProxyWithoutServiceDiscoveryTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * <pre>
 * It verifies e2e tls + Authentication + Authorization (client -> proxy -> broker>
 *
 * 1. client connects to proxy over tls and pass auth-data
 * 2. proxy authenticate client and retrieve client-role
 *    and send it to broker as originalPrincipal over tls
 * 3. client creates producer/consumer via proxy
 * 4. broker authorize producer/consumer create request using originalPrincipal
 *
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testDiscoveryService() throws Exception {
    log.info("-- Starting {} test --", methodName);

    Map<String, String> authParams = Maps.newHashMap();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    Authentication authTls = new AuthenticationTls();
    authTls.configure(authParams);
    // create a client which connects to proxy over tls and pass authData
    PulsarClient proxyClient = createPulsarClient(authTls, proxyService.getServiceUrlTls());

    admin.clusters().createCluster("without-service-discovery", new ClusterData(brokerUrl.toString()));

    admin.tenants().createTenant("my-property", new TenantInfo(Sets.newHashSet("appid1", "appid2"),
            Sets.newHashSet("without-service-discovery")));
    admin.namespaces().createNamespace("my-property/without-service-discovery/my-ns");

    Consumer<byte[]> consumer = proxyClient.newConsumer()
            .topic("persistent://my-property/without-service-discovery/my-ns/my-topic1")
            .subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = proxyClient.newProducer(Schema.BYTES)
            .topic("persistent://my-property/without-service-discovery/my-ns/my-topic1").create();
    final int msgs = 10;
    for (int i = 0; i < msgs; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }

    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    int count = 0;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
        count++;
    }
    // Acknowledge the consumption of all messages at once
    Assert.assertEquals(msgs, count);
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
 
Example 17
Source File: ProxySaslAuthenticationTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@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 18
Source File: PulsarMultiHostClientTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiHostUrlRetrySuccess() throws Exception {
    log.info("-- Starting {} test --", methodName);

    final String topicName = "persistent://my-property/my-ns/my-topic1";
    final String subscriptionName = "my-subscriber-name";

    // Multi hosts included an unreached port and the actual port for verify retry logic
    String urlsWithUnreached = "http://localhost:51000,localhost:" + new URI(pulsar.getWebServiceAddress()).getPort();
    if (isTcpLookup) {
        urlsWithUnreached = "pulsar://localhost:51000,localhost" + new URI(pulsar.getBrokerServiceUrl()).getPort();
    }
    PulsarClient client = newPulsarClient(urlsWithUnreached, 0);

    Consumer<byte[]> consumer = client.newConsumer().topic(topicName).subscriptionName(subscriptionName)
        .acknowledgmentGroupTime(0, TimeUnit.SECONDS).subscribe();
    Producer<byte[]> producer = client.newProducer().topic(topicName).create();

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

    Message<byte[]> msg = null;
    Set<String> messageSet = new HashSet();
    for (int i = 0; i < 5; 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();

    producer.close();
    client.close();

    log.info("-- Exiting {} test --", methodName);
}
 
Example 19
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public ConsumerRecords<K, V> poll(long timeoutMillis) {
    try {
        QueueItem item = receivedMessages.poll(timeoutMillis, TimeUnit.MILLISECONDS);
        if (item == null) {
            return (ConsumerRecords<K, V>) ConsumerRecords.EMPTY;
        }

        Map<TopicPartition, List<ConsumerRecord<K, V>>> records = new HashMap<>();

        int numberOfRecords = 0;

        while (item != null) {
            TopicName topicName = TopicName.get(item.consumer.getTopic());
            String topic = topicName.getPartitionedTopicName();
            int partition = topicName.isPartitioned() ? topicName.getPartitionIndex() : 0;
            Message<byte[]> msg = item.message;
            MessageIdImpl msgId = (MessageIdImpl) msg.getMessageId();
            long offset = MessageIdUtils.getOffset(msgId);

            TopicPartition tp = new TopicPartition(topic, partition);
            if (lastReceivedOffset.get(tp) == null && !unpolledPartitions.contains(tp)) {
            	log.info("When polling offsets, invalid offsets were detected. Resetting topic partition {}", tp);
            	resetOffsets(tp);
            }

            K key = getKey(topic, msg);
            if (valueSchema instanceof PulsarKafkaSchema) {
                ((PulsarKafkaSchema<V>) valueSchema).setTopic(topic);
            }
            V value = valueSchema.decode(msg.getData());

            TimestampType timestampType = TimestampType.LOG_APPEND_TIME;
            long timestamp = msg.getPublishTime();

            if (msg.getEventTime() > 0) {
                // If we have Event time, use that in preference
                timestamp = msg.getEventTime();
                timestampType = TimestampType.CREATE_TIME;
            }

            ConsumerRecord<K, V> consumerRecord = new ConsumerRecord<>(topic, partition, offset, timestamp,
                    timestampType, -1, msg.hasKey() ? msg.getKey().length() : 0, msg.getData().length, key, value);

            records.computeIfAbsent(tp, k -> new ArrayList<>()).add(consumerRecord);

            // Update last offset seen by application
            lastReceivedOffset.put(tp, offset);
            unpolledPartitions.remove(tp);

            if (++numberOfRecords >= maxRecordsInSinglePoll) {
                break;
            }

            // Check if we have an item already available
            item = receivedMessages.poll(0, TimeUnit.MILLISECONDS);
        }

        if (isAutoCommit && !records.isEmpty()) {
            // Commit the offset of previously dequeued messages
            commitAsync();
        }

        // If no interceptor is provided, interceptors list will an empty list, original ConsumerRecords will be return.
        return applyConsumerInterceptorsOnConsume(interceptors, new ConsumerRecords<>(records));
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
 
Example 20
Source File: AsyncConsumerTutorial.java    From pulsar-java-tutorial with Apache License 2.0 4 votes vote down vote up
private static void handleMessage(Consumer<byte[]> consumer, Message<byte[]> msg) {
    String msgContent = new String(msg.getData());
    String msgId = new String(msg.getMessageId().toByteArray());
    log.info("Received message '{}' with msg-id={}", msgContent, msgId);
    consumer.acknowledgeAsync(msg).thenRun(() -> {});
}