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;
}
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;
}
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;
}
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();
}
}
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();
}
Aggregations