linlinnn commented on a change in pull request #10544:
URL: https://github.com/apache/pulsar/pull/10544#discussion_r645986900
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ZeroQueueConsumerImpl.java
##########
@@ -56,6 +62,45 @@ public ZeroQueueConsumerImpl(PulsarClientImpl client, String
topic, ConsumerConf
super(client, topic, conf, executorProvider, partitionIndex,
hasParentConsumer, subscribeFuture,
startMessageId, 0 /* startMessageRollbackDurationInSec */,
schema, interceptors,
createTopicIfDoesNotExist);
+ //Actually,i want to achieve to
+ this.messageScheduledFuture =
Executors.newSingleThreadScheduledExecutor(
+ new
DefaultThreadFactory(getClass().getSimpleName()+"-receive-message"));
+ }
+
+
+ @Override
+ protected Message<T> internalReceive(int timeout, TimeUnit unit) throws
PulsarClientException {
+ Message<T> tMessage = null;
+
+ if (Objects.nonNull(conf.getReceiveThreads())){
+ messageScheduledFuture = Executors.newScheduledThreadPool(
Review comment:
in this way, every call will create a new instance.
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ZeroQueueConsumerImpl.java
##########
@@ -81,6 +126,17 @@ public ZeroQueueConsumerImpl(PulsarClientImpl client,
String topic, ConsumerConf
return future;
}
+ private Message<T> doZeroQueue() {
+ Message<T> result = null;
+ try {
+ result = internalReceive();
Review comment:
internalReceive will hold a lock, we cannot call it directly when
receiving message with timeout.
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ZeroQueueConsumerImpl.java
##########
@@ -56,6 +62,45 @@ public ZeroQueueConsumerImpl(PulsarClientImpl client, String
topic, ConsumerConf
super(client, topic, conf, executorProvider, partitionIndex,
hasParentConsumer, subscribeFuture,
startMessageId, 0 /* startMessageRollbackDurationInSec */,
schema, interceptors,
createTopicIfDoesNotExist);
+ //Actually,i want to achieve to
+ this.messageScheduledFuture =
Executors.newSingleThreadScheduledExecutor(
+ new
DefaultThreadFactory(getClass().getSimpleName()+"-receive-message"));
+ }
+
+
+ @Override
+ protected Message<T> internalReceive(int timeout, TimeUnit unit) throws
PulsarClientException {
+ Message<T> tMessage = null;
+
+ if (Objects.nonNull(conf.getReceiveThreads())){
+ messageScheduledFuture = Executors.newScheduledThreadPool(
+ conf.getReceiveThreads(),new
DefaultThreadFactory(getClass().getSimpleName()+"-receive-message")
+ );
+ }
+
+ long time1 = System.nanoTime();
+
+ Supplier<Message<T>> asyncTask = this::doZeroQueue;
+ CompletableFuture<Message<T>> future = new CompletableFuture<>();
+ try {
+ future = FutureUtil.schedule(messageScheduledFuture, asyncTask,
+ conf.getReceiveInterval(), TimeUnit.SECONDS);
+ //Determine whether the task is over, and then get the result
+ if (future.isDone()) {
+
+ tMessage = future.get();
+ }
+ }catch (ExecutionException | InterruptedException ignore){
+ long time2 = System.nanoTime();
Review comment:
we need to interrupt current thread when catch InterruptedException
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ZeroQueueConsumerImpl.java
##########
@@ -56,6 +62,45 @@ public ZeroQueueConsumerImpl(PulsarClientImpl client, String
topic, ConsumerConf
super(client, topic, conf, executorProvider, partitionIndex,
hasParentConsumer, subscribeFuture,
startMessageId, 0 /* startMessageRollbackDurationInSec */,
schema, interceptors,
createTopicIfDoesNotExist);
+ //Actually,i want to achieve to
+ this.messageScheduledFuture =
Executors.newSingleThreadScheduledExecutor(
Review comment:
we need add some shutdown logic when we close the consumer.
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ZeroQueueConsumerImpl.java
##########
@@ -56,6 +62,45 @@ public ZeroQueueConsumerImpl(PulsarClientImpl client, String
topic, ConsumerConf
super(client, topic, conf, executorProvider, partitionIndex,
hasParentConsumer, subscribeFuture,
startMessageId, 0 /* startMessageRollbackDurationInSec */,
schema, interceptors,
createTopicIfDoesNotExist);
+ //Actually,i want to achieve to
+ this.messageScheduledFuture =
Executors.newSingleThreadScheduledExecutor(
+ new
DefaultThreadFactory(getClass().getSimpleName()+"-receive-message"));
+ }
+
+
+ @Override
+ protected Message<T> internalReceive(int timeout, TimeUnit unit) throws
PulsarClientException {
+ Message<T> tMessage = null;
+
+ if (Objects.nonNull(conf.getReceiveThreads())){
+ messageScheduledFuture = Executors.newScheduledThreadPool(
+ conf.getReceiveThreads(),new
DefaultThreadFactory(getClass().getSimpleName()+"-receive-message")
+ );
+ }
+
+ long time1 = System.nanoTime();
Review comment:
please rename it like startTime or beginTime.
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ZeroQueueConsumerImpl.java
##########
@@ -56,6 +62,45 @@ public ZeroQueueConsumerImpl(PulsarClientImpl client, String
topic, ConsumerConf
super(client, topic, conf, executorProvider, partitionIndex,
hasParentConsumer, subscribeFuture,
startMessageId, 0 /* startMessageRollbackDurationInSec */,
schema, interceptors,
createTopicIfDoesNotExist);
+ //Actually,i want to achieve to
+ this.messageScheduledFuture =
Executors.newSingleThreadScheduledExecutor(
+ new
DefaultThreadFactory(getClass().getSimpleName()+"-receive-message"));
+ }
+
+
+ @Override
+ protected Message<T> internalReceive(int timeout, TimeUnit unit) throws
PulsarClientException {
+ Message<T> tMessage = null;
+
+ if (Objects.nonNull(conf.getReceiveThreads())){
+ messageScheduledFuture = Executors.newScheduledThreadPool(
+ conf.getReceiveThreads(),new
DefaultThreadFactory(getClass().getSimpleName()+"-receive-message")
+ );
+ }
+
+ long time1 = System.nanoTime();
+
+ Supplier<Message<T>> asyncTask = this::doZeroQueue;
+ CompletableFuture<Message<T>> future = new CompletableFuture<>();
+ try {
+ future = FutureUtil.schedule(messageScheduledFuture, asyncTask,
+ conf.getReceiveInterval(), TimeUnit.SECONDS);
+ //Determine whether the task is over, and then get the result
+ if (future.isDone()) {
+
+ tMessage = future.get();
+ }
+ }catch (ExecutionException | InterruptedException ignore){
+ long time2 = System.nanoTime();
+ System.out.println("No timeout after " +
+ (time2-time1)/1000000000.0 + " seconds");
+ }finally {
+ //When the captured futrue throws an exception, the worker thread
clears the interruption status in time
+ future.cancel(true);
Review comment:
finally means that we will always close the future.
--
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]