sijie commented on a change in pull request #4093: [issue#4042] improve java
functions API
URL: https://github.com/apache/pulsar/pull/4093#discussion_r277127575
##########
File path:
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/ContextImpl.java
##########
@@ -346,60 +346,32 @@ public ByteBuffer getState(String key) {
return publish(topicName, object, (Schema<O>)
topicSchema.getSchema(topicName, object, schemaOrSerdeClassName, false),
messageConf);
}
+ @Override
+ public <O> TypedMessageBuilder<O> newOutputMessage(String topicName,
Schema<O> schema) throws PulsarClientException {
+ Producer<O> producer = getProducer(topicName, schema);
+ return producer.newMessage();
+ }
+
@SuppressWarnings("unchecked")
public <O> CompletableFuture<Void> publish(String topicName, O object,
Schema<O> schema, Map<String, Object> messageConf) {
- Producer<O> producer = (Producer<O>) publishProducers.get(topicName);
-
- if (producer == null) {
- try {
- Producer<O> newProducer = ((ProducerBuilderImpl<O>)
producerBuilder.clone())
- .schema(schema)
- .blockIfQueueFull(true)
- .enableBatching(true)
- .batchingMaxPublishDelay(10, TimeUnit.MILLISECONDS)
- .compressionType(CompressionType.LZ4)
- .hashingScheme(HashingScheme.Murmur3_32Hash) //
- .messageRoutingMode(MessageRoutingMode.CustomPartition)
- .messageRouter(FunctionResultRouter.of())
- // set send timeout to be infinity to prevent
potential deadlock with consumer
- // that might happen when consumer is blocked due to
unacked messages
- .sendTimeout(0, TimeUnit.SECONDS)
- .topic(topicName)
- .properties(InstanceUtils.getProperties(componentType,
- FunctionCommon.getFullyQualifiedName(
-
this.config.getFunctionDetails().getTenant(),
-
this.config.getFunctionDetails().getNamespace(),
-
this.config.getFunctionDetails().getName()),
- this.config.getInstanceId()))
- .create();
-
- Producer<O> existingProducer = (Producer<O>)
publishProducers.putIfAbsent(topicName, newProducer);
-
- if (existingProducer != null) {
- // The value in the map was not updated after the
concurrent put
- newProducer.close();
- producer = existingProducer;
- } else {
- producer = newProducer;
- }
-
- } catch (PulsarClientException e) {
- logger.error("Failed to create Producer while doing user
publish", e);
- return FutureUtil.failedFuture(e);
+ Producer<O> producer;
+ try {
+ producer = getProducer(topicName, schema);
Review comment:
I think you should change `publish` methods to use newOutputMessage. so that
newOutputMessage can be covered by existing tests and you don't need to write
new tests.
```
TypedMessageBuilder<O> messageBuilder = newOutputMessage(topicName, schema);
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services