Search in sources :

Example 6 with TypedMessageBuilder

use of org.apache.pulsar.client.api.TypedMessageBuilder in project pulsar by apache.

the class TypedMessageBuilderPublish method process.

@Override
public Void process(String input, Context context) {
    String publishTopic = (String) context.getUserConfigValueOrDefault("publish-topic", "publishtopic");
    String output = String.format("%s!", input);
    Map<String, String> properties = new HashMap<>();
    properties.put("input_topic", context.getCurrentRecord().getTopicName().get());
    properties.putAll(context.getCurrentRecord().getProperties());
    try {
        TypedMessageBuilder messageBuilder = context.newOutputMessage(publishTopic, Schema.STRING).value(output).properties(properties);
        if (context.getCurrentRecord().getKey().isPresent()) {
            messageBuilder.key(context.getCurrentRecord().getKey().get());
        }
        messageBuilder.eventTime(System.currentTimeMillis()).sendAsync();
    } catch (PulsarClientException e) {
        context.getLogger().error(e.toString());
    }
    return null;
}
Also used : TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) HashMap(java.util.HashMap) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Example 7 with TypedMessageBuilder

use of org.apache.pulsar.client.api.TypedMessageBuilder in project pulsar by apache.

the class FunctionMetaDataManagerTest method mockPulsarClient.

private static PulsarClient mockPulsarClient() throws PulsarClientException {
    ProducerBuilder<byte[]> builder = mock(ProducerBuilder.class);
    when(builder.topic(anyString())).thenReturn(builder);
    when(builder.producerName(anyString())).thenReturn(builder);
    when(builder.enableBatching(anyBoolean())).thenReturn(builder);
    when(builder.blockIfQueueFull(anyBoolean())).thenReturn(builder);
    when(builder.compressionType(any(CompressionType.class))).thenReturn(builder);
    when(builder.sendTimeout(anyInt(), any(TimeUnit.class))).thenReturn(builder);
    when(builder.accessMode(any())).thenReturn(builder);
    Producer producer = mock(Producer.class);
    TypedMessageBuilder messageBuilder = mock(TypedMessageBuilder.class);
    when(messageBuilder.key(anyString())).thenReturn(messageBuilder);
    doAnswer(invocation -> {
        Object arg0 = invocation.getArgument(0);
        FunctionMetaDataManagerTest.producerByteArray = (byte[]) arg0;
        return messageBuilder;
    }).when(messageBuilder).value(any());
    when(messageBuilder.property(anyString(), anyString())).thenReturn(messageBuilder);
    when(producer.newMessage()).thenReturn(messageBuilder);
    when(builder.create()).thenReturn(producer);
    when(builder.createAsync()).thenReturn(CompletableFuture.completedFuture(producer));
    PulsarClient client = mock(PulsarClient.class);
    when(client.newProducer()).thenReturn(builder);
    return client;
}
Also used : TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) Producer(org.apache.pulsar.client.api.Producer) TimeUnit(java.util.concurrent.TimeUnit) PulsarClient(org.apache.pulsar.client.api.PulsarClient) CompressionType(org.apache.pulsar.client.api.CompressionType)

Example 8 with TypedMessageBuilder

use of org.apache.pulsar.client.api.TypedMessageBuilder in project pulsar-adapters by apache.

the class PulsarKafkaProducer method send.

@Override
public Future<RecordMetadata> send(ProducerRecord<K, V> producerRecord, Callback callback) {
    org.apache.pulsar.client.api.Producer<byte[]> producer;
    try {
        producer = producers.computeIfAbsent(producerRecord.topic(), topic -> createNewProducer(topic));
    } catch (Exception e) {
        if (callback != null) {
            callback.onCompletion(null, e);
        }
        CompletableFuture<RecordMetadata> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;
    }
    TypedMessageBuilder<byte[]> messageBuilder = buildMessage(producer, producerRecord);
    CompletableFuture<RecordMetadata> future = new CompletableFuture<>();
    messageBuilder.sendAsync().thenAccept((messageId) -> {
        future.complete(getRecordMetadata(producerRecord.topic(), messageId));
    }).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 : LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarClientKafkaConfig) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) Future(java.util.concurrent.Future) Cluster(org.apache.kafka.common.Cluster) Map(java.util.Map) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarProducerKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarProducerKafkaConfig) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TopicPartition(org.apache.kafka.common.TopicPartition) Properties(java.util.Properties) MessageIdUtils(org.apache.pulsar.client.util.MessageIdUtils) Logger(org.slf4j.Logger) CompressionType(org.apache.pulsar.client.api.CompressionType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) PartitionInfo(org.apache.kafka.common.PartitionInfo) KafkaMessageRouter(org.apache.pulsar.client.kafka.compat.KafkaMessageRouter) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Schema(org.apache.pulsar.client.api.Schema) 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) PulsarKafkaSchema(org.apache.pulsar.client.kafka.compat.PulsarKafkaSchema) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with TypedMessageBuilder

use of org.apache.pulsar.client.api.TypedMessageBuilder in project pulsar by yahoo.

the class FunctionMetaDataManager method updateFunctionOnLeader.

/**
 * Called by the worker when we are in the leader mode.  In this state, we update our in-memory
 * data structures and then write to the metadata topic.
 * @param functionMetaData The function metadata in question
 * @param delete Is this a delete operation
 * @throws IllegalStateException if we are not the leader
 * @throws IllegalArgumentException if the request is out of date.
 */
public synchronized void updateFunctionOnLeader(FunctionMetaData functionMetaData, boolean delete) throws IllegalStateException, IllegalArgumentException {
    boolean needsScheduling;
    if (exclusiveLeaderProducer == null) {
        throw new IllegalStateException("Not the leader");
    }
    // Check first to avoid local cache update failure
    checkRequestOutDated(functionMetaData, delete);
    byte[] toWrite;
    if (workerConfig.getUseCompactedMetadataTopic()) {
        if (delete) {
            toWrite = "".getBytes();
        } else {
            toWrite = functionMetaData.toByteArray();
        }
    } else {
        Request.ServiceRequest serviceRequest = Request.ServiceRequest.newBuilder().setServiceRequestType(delete ? Request.ServiceRequest.ServiceRequestType.DELETE : Request.ServiceRequest.ServiceRequestType.UPDATE).setFunctionMetaData(functionMetaData).setWorkerId(workerConfig.getWorkerId()).setRequestId(UUID.randomUUID().toString()).build();
        toWrite = serviceRequest.toByteArray();
    }
    try {
        TypedMessageBuilder builder = exclusiveLeaderProducer.newMessage().value(toWrite).property(versionTag, Long.toString(functionMetaData.getVersion()));
        if (workerConfig.getUseCompactedMetadataTopic()) {
            builder = builder.key(FunctionCommon.getFullyQualifiedName(functionMetaData.getFunctionDetails()));
        }
        lastMessageSeen = builder.send();
        if (delete) {
            needsScheduling = processDeregister(functionMetaData);
        } else {
            needsScheduling = processUpdate(functionMetaData);
        }
    } catch (Exception e) {
        log.error("Could not write into Function Metadata topic", e);
        throw new IllegalStateException("Internal Error updating function at the leader", e);
    }
    if (needsScheduling) {
        this.schedulerManager.schedule();
    }
}
Also used : TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) Request(org.apache.pulsar.functions.proto.Request) IOException(java.io.IOException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException)

Example 10 with TypedMessageBuilder

use of org.apache.pulsar.client.api.TypedMessageBuilder in project pulsar by yahoo.

the class SchemaTest method generateDataByDifferentSchema.

private void generateDataByDifferentSchema(String ns, String baseTopic, Schema schema, Object data, Producer<?> autoProducer, AtomicInteger totalMsgCnt) throws PulsarClientException {
    String topic = getTopicName(ns, baseTopic);
    Producer<Object> producer = pulsarClient.newProducer(schema).topic(topic).create();
    producer.newMessage().value(data).property("baseTopic", baseTopic).send();
    totalMsgCnt.incrementAndGet();
    Consumer<GenericRecord> consumer = pulsarClient.newConsumer(Schema.AUTO_CONSUME()).topic(topic).subscriptionName("test").subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
    Message<GenericRecord> message = consumer.receive(5, TimeUnit.SECONDS);
    if (message == null) {
        Assert.fail("Failed to receive message for topic " + topic);
    }
    if (!message.getReaderSchema().isPresent()) {
        Assert.fail("Failed to get reader schema for topic " + topic);
    }
    message.getValue();
    TypedMessageBuilder messageBuilder = autoProducer.newMessage(Schema.AUTO_PRODUCE_BYTES(message.getReaderSchema().get())).value(message.getData()).properties(message.getProperties());
    if (message.getKeyBytes() != null) {
        messageBuilder.keyBytes(message.getKeyBytes());
    }
    messageBuilder.send();
    producer.close();
    consumer.close();
}
Also used : TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord)

Aggregations

TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)41 Producer (org.apache.pulsar.client.api.Producer)19 PulsarClient (org.apache.pulsar.client.api.PulsarClient)15 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)14 HashMap (java.util.HashMap)12 TimeUnit (java.util.concurrent.TimeUnit)12 ProducerBuilder (org.apache.pulsar.client.api.ProducerBuilder)12 List (java.util.List)11 CompletableFuture (java.util.concurrent.CompletableFuture)11 MessageId (org.apache.pulsar.client.api.MessageId)11 Logger (org.slf4j.Logger)10 Test (org.testng.annotations.Test)10 IOException (java.io.IOException)9 Collections (java.util.Collections)9 ArrayList (java.util.ArrayList)8 Consumer (org.apache.pulsar.client.api.Consumer)7 Schema (org.apache.pulsar.client.api.Schema)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)5 AtomicLongFieldUpdater (java.util.concurrent.atomic.AtomicLongFieldUpdater)5