Search in sources :

Example 46 with Message

use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.

the class PulsarBoltTest method testBasic.

@Test
public void testBasic() throws Exception {
    String msgContent = "hello world";
    Tuple tuple = getMockTuple(msgContent);
    bolt.execute(tuple);
    for (int i = 0; i < NO_OF_RETRIES; i++) {
        Thread.sleep(1000);
        if (mockCollector.acked()) {
            break;
        }
    }
    Assert.assertTrue(mockCollector.acked());
    Assert.assertFalse(mockCollector.failed());
    Assert.assertNull(mockCollector.getLastError());
    Assert.assertEquals(tuple, mockCollector.getAckedTuple());
    Message msg = consumer.receive(5, TimeUnit.SECONDS);
    consumer.acknowledge(msg);
    Assert.assertEquals(msgContent, new String(msg.getData()));
}
Also used : Message(org.apache.pulsar.client.api.Message) Tuple(org.apache.storm.tuple.Tuple) Test(org.testng.annotations.Test)

Example 47 with Message

use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.

the class StormExample method main.

public static void main(String[] args) throws PulsarClientException {
    ClientConfiguration clientConf = new ClientConfiguration();
    // String authPluginClassName = "org.apache.pulsar.client.impl.auth.MyAuthentication";
    // String authParams = "key1:val1,key2:val2";
    // clientConf.setAuthentication(authPluginClassName, authParams);
    String topic1 = "persistent://my-property/use/my-ns/my-topic1";
    String topic2 = "persistent://my-property/use/my-ns/my-topic2";
    String subscriptionName1 = "my-subscriber-name1";
    String subscriptionName2 = "my-subscriber-name2";
    // create spout
    PulsarSpoutConfiguration spoutConf = new PulsarSpoutConfiguration();
    spoutConf.setServiceUrl(serviceUrl);
    spoutConf.setTopic(topic1);
    spoutConf.setSubscriptionName(subscriptionName1);
    spoutConf.setMessageToValuesMapper(messageToValuesMapper);
    PulsarSpout spout = new PulsarSpout(spoutConf, clientConf);
    // create bolt
    PulsarBoltConfiguration boltConf = new PulsarBoltConfiguration();
    boltConf.setServiceUrl(serviceUrl);
    boltConf.setTopic(topic2);
    boltConf.setTupleToMessageMapper(tupleToMessageMapper);
    PulsarBolt bolt = new PulsarBolt(boltConf, clientConf);
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("testSpout", spout);
    builder.setBolt("testBolt", bolt).shuffleGrouping("testSpout");
    Config conf = new Config();
    conf.setNumWorkers(2);
    conf.setDebug(true);
    conf.registerMetricsConsumer(PulsarMetricsConsumer.class);
    LocalCluster cluster = new LocalCluster();
    cluster.submitTopology("test", conf, builder.createTopology());
    Utils.sleep(10000);
    PulsarClient pulsarClient = PulsarClient.create(serviceUrl, clientConf);
    // create a consumer on topic2 to receive messages from the bolt when the processing is done
    Consumer consumer = pulsarClient.subscribe(topic2, subscriptionName2);
    // create a producer on topic1 to send messages that will be received by the spout
    Producer producer = pulsarClient.createProducer(topic1);
    for (int i = 0; i < 10; i++) {
        String msg = "msg-" + i;
        producer.send(msg.getBytes());
        LOG.info("Message {} sent", msg);
    }
    Message msg = null;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(1, TimeUnit.SECONDS);
        LOG.info("Message {} received", new String(msg.getData()));
    }
    cluster.killTopology("test");
    cluster.shutdown();
}
Also used : LocalCluster(org.apache.storm.LocalCluster) PulsarBolt(org.apache.pulsar.storm.PulsarBolt) Message(org.apache.pulsar.client.api.Message) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) Config(org.apache.storm.Config) PulsarBoltConfiguration(org.apache.pulsar.storm.PulsarBoltConfiguration) PulsarSpout(org.apache.pulsar.storm.PulsarSpout) PulsarSpoutConfiguration(org.apache.pulsar.storm.PulsarSpoutConfiguration) IMetricsConsumer(org.apache.storm.metric.api.IMetricsConsumer) Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration)

Example 48 with Message

use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.

the class ProducerHandler method onWebSocketText.

@Override
public void onWebSocketText(String message) {
    ProducerMessage sendRequest;
    byte[] rawPayload = null;
    String requestContext = null;
    try {
        sendRequest = ObjectMapperFactory.getThreadLocal().readValue(message, ProducerMessage.class);
        requestContext = sendRequest.context;
        rawPayload = Base64.getDecoder().decode(sendRequest.payload);
    } catch (IOException e) {
        sendAckResponse(new ProducerAck(FailedToDeserializeFromJSON, e.getMessage(), null, null));
        return;
    } catch (IllegalArgumentException e) {
        String msg = format("Invalid Base64 message-payload error=%s", e.getMessage());
        sendAckResponse(new ProducerAck(PayloadEncodingError, msg, null, requestContext));
        return;
    }
    final long msgSize = rawPayload.length;
    MessageBuilder builder = MessageBuilder.create().setContent(rawPayload);
    if (sendRequest.properties != null) {
        builder.setProperties(sendRequest.properties);
    }
    if (sendRequest.key != null) {
        builder.setKey(sendRequest.key);
    }
    if (sendRequest.replicationClusters != null) {
        builder.setReplicationClusters(sendRequest.replicationClusters);
    }
    Message<byte[]> msg = builder.build();
    final long now = System.nanoTime();
    producer.sendAsync(msg).thenAccept(msgId -> {
        updateSentMsgStats(msgSize, TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - now));
        if (isConnected()) {
            String messageId = Base64.getEncoder().encodeToString(msgId.toByteArray());
            sendAckResponse(new ProducerAck(messageId, sendRequest.context));
        }
    }).exceptionally(exception -> {
        numMsgsFailed.increment();
        sendAckResponse(new ProducerAck(UnknownError, exception.getMessage(), null, sendRequest.context));
        return null;
    });
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Producer(org.apache.pulsar.client.api.Producer) LoggerFactory(org.slf4j.LoggerFactory) Message(org.apache.pulsar.client.api.Message) Enums(com.google.common.base.Enums) ProducerAck(org.apache.pulsar.websocket.data.ProducerAck) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HttpServletRequest(javax.servlet.http.HttpServletRequest) MessageRoutingMode(org.apache.pulsar.client.api.ProducerConfiguration.MessageRoutingMode) StatsBuckets(org.apache.pulsar.websocket.stats.StatsBuckets) ProducerBusyException(org.apache.pulsar.client.api.PulsarClientException.ProducerBusyException) ProducerBlockedQuotaExceededException(org.apache.pulsar.client.api.PulsarClientException.ProducerBlockedQuotaExceededException) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Logger(org.slf4j.Logger) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) CompressionType(org.apache.pulsar.client.api.CompressionType) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashingScheme(org.apache.pulsar.client.api.ProducerConfiguration.HashingScheme) IOException(java.io.IOException) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) String.format(java.lang.String.format) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) TimeUnit(java.util.concurrent.TimeUnit) PayloadEncodingError(org.apache.pulsar.websocket.WebSocketError.PayloadEncodingError) UnknownError(org.apache.pulsar.websocket.WebSocketError.UnknownError) Base64(java.util.Base64) ProducerBlockedQuotaExceededError(org.apache.pulsar.client.api.PulsarClientException.ProducerBlockedQuotaExceededError) FailedToDeserializeFromJSON(org.apache.pulsar.websocket.WebSocketError.FailedToDeserializeFromJSON) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) IOException(java.io.IOException) ProducerAck(org.apache.pulsar.websocket.data.ProducerAck)

Example 49 with Message

use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.

the class PulsarKafkaConsumer method poll.

@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 && ++numberOfRecords < MAX_RECORDS_IN_SINGLE_POLL) {
            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);
            K key = getKey(topic, msg);
            V value = valueDeserializer.deserialize(topic, 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);
            // Check if we have an item already available
            item = receivedMessages.poll(0, TimeUnit.MILLISECONDS);
        }
        if (isAutoCommit) {
            // Commit the offset of previously dequeued messages
            commitAsync();
        }
        return new ConsumerRecords<>(records);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : TopicName(org.apache.pulsar.common.naming.TopicName) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) PulsarConsumerKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarConsumerKafkaConfig) Message(org.apache.pulsar.client.api.Message) PulsarClientKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarClientKafkaConfig) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConsumerName(org.apache.pulsar.client.util.ConsumerName) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Map(java.util.Map) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) Deserializer(org.apache.kafka.common.serialization.Deserializer) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TimestampType(org.apache.kafka.common.record.TimestampType) TopicPartition(org.apache.kafka.common.TopicPartition) Properties(java.util.Properties) MessageIdUtils(org.apache.pulsar.client.kafka.compat.MessageIdUtils) Collection(java.util.Collection) MessageListener(org.apache.pulsar.client.api.MessageListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) PartitionInfo(org.apache.kafka.common.PartitionInfo) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Base64(java.util.Base64) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageId(org.apache.pulsar.client.api.MessageId) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) TopicName(org.apache.pulsar.common.naming.TopicName) TopicPartition(org.apache.kafka.common.TopicPartition) TimestampType(org.apache.kafka.common.record.TimestampType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 50 with Message

use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.

the class PulsarKafkaProducer method send.

@Override
public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callback) {
    org.apache.pulsar.client.api.Producer<byte[]> producer;
    try {
        producer = producers.computeIfAbsent(record.topic(), topic -> createNewProducer(topic));
    } catch (Exception e) {
        if (callback != null) {
            callback.onCompletion(null, e);
        }
        CompletableFuture<RecordMetadata> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;
    }
    Message<byte[]> msg = getMessage(record);
    int messageSize = msg.getData().length;
    CompletableFuture<RecordMetadata> future = new CompletableFuture<>();
    CompletableFuture<MessageId> sendFuture = producer.sendAsync(msg);
    lastSendFuture.put(record.topic(), sendFuture);
    sendFuture.thenAccept((messageId) -> {
        future.complete(getRecordMetadata(record.topic(), msg, messageId, messageSize));
    }).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    future.handle((recordMetadata, throwable) -> {
        if (callback != null) {
            Exception exception = throwable != null ? new Exception(throwable) : null;
            callback.onCompletion(recordMetadata, exception);
        }
        return null;
    });
    return future;
}
Also used : TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.pulsar.client.api.Message) PulsarClientKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarClientKafkaConfig) ConcurrentMap(java.util.concurrent.ConcurrentMap) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) Future(java.util.concurrent.Future) Map(java.util.Map) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarProducerKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarProducerKafkaConfig) TopicPartition(org.apache.kafka.common.TopicPartition) Properties(java.util.Properties) MessageIdUtils(org.apache.pulsar.client.kafka.compat.MessageIdUtils) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) CompressionType(org.apache.pulsar.client.api.CompressionType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PartitionInfo(org.apache.kafka.common.PartitionInfo) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) Base64(java.util.Base64) List(java.util.List) MessageId(org.apache.pulsar.client.api.MessageId) Serializer(org.apache.kafka.common.serialization.Serializer) Collections(java.util.Collections) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutionException(java.util.concurrent.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) MessageId(org.apache.pulsar.client.api.MessageId)

Aggregations

Message (org.apache.pulsar.client.api.Message)72 Test (org.testng.annotations.Test)59 Producer (org.apache.pulsar.client.api.Producer)38 Consumer (org.apache.pulsar.client.api.Consumer)35 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)28 MessageId (org.apache.pulsar.client.api.MessageId)27 ProducerConfiguration (org.apache.pulsar.client.api.ProducerConfiguration)27 ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)25 ExecutionException (java.util.concurrent.ExecutionException)20 IOException (java.io.IOException)19 CompletableFuture (java.util.concurrent.CompletableFuture)16 ExecutorService (java.util.concurrent.ExecutorService)15 MessageIdImpl (org.apache.pulsar.client.impl.MessageIdImpl)13 Map (java.util.Map)12 TimeUnit (java.util.concurrent.TimeUnit)12 List (java.util.List)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 PulsarClient (org.apache.pulsar.client.api.PulsarClient)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10