Gcloud PubSub Java实现 - java.util.concurrent.RejectedExecutionException

时间:2017-07-13 00:07:39

标签: gcloud google-cloud-pubsub gcloud-java

我使用GCloud文档中的示例代码段作为订阅者接收消息。我的pubsub gcloud jar版本是0.19.0-alpha

问题是我可以收到带有属性映射的msg,但我一直有这个例外:

2017-07-12 16:52:25,219 [grpc-default-worker-ELG-1-16] WARN io.netty.util.concurrent.DefaultPromise - io.grpc.netty.NettyClientHandler $ 3抛出异常.operationComplete() java.util.concurrent.RejectedExecutionException:java.util.concurrent.ScheduledThreadPoolExecutor@25cbe860中的任务java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@fbf4a6d [已终止,池大小= 35,活动线程= 0,排队任务= 0,已完成tasks = 2403]     at java.util.concurrent.ThreadPoolExecutor $ AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)     at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)     at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:326)     at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:533)     at java.util.concurrent.ScheduledThreadPoolExecutor.execute(ScheduledThreadPoolExecutor.java:622)     at java.util.concurrent.Executors $ DelegatedExecutorService.execute(Executors.java:668)     在io.grpc.internal.SerializingExecutor.execute(SerializingExecutor.java:110)     在io.grpc.internal.ClientCallImpl $ ClientStreamListenerImpl.onReady(ClientCallImpl.java:573)     at io.grpc.internal.DelayedStream $ DelayedStreamListener.onReady(DelayedStream.java:398)     在io.grpc.internal.AbstractStream2 $ TransportState.notifyIfReady(AbstractStream2.java:305)     在io.grpc.internal.AbstractStream2 $ TransportState.onStreamAllocated(AbstractStream2.java:248)     at io.grpc.netty.NettyClientStream $ TransportState.setHttp2Stream(NettyClientStream.java:227)     在io.grpc.netty.NettyClientHandler $ 3.operationComplete(NettyClientHandler.java:429)     在io.grpc.netty.NettyClientHandler $ 3.operationComplete(NettyClientHandler.java:417)     at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:507)     at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:481)     在io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:420)     在io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)

之后,程序关闭并停止监听并获取消息。如何解决这个中断,我甚至摆脱了具有subscriber.stopAsync()。

的finally子句

1 个答案:

答案 0 :(得分:0)

他们提供的代码段中有一个错误。您需要在messaegeIdFuture上调用get()。以下代码解决了该问题:

Publisher publisher = null;
String projectId = ServiceOptions.getDefaultProjectId();
ProjectTopicName topic = ProjectTopicName.of(projectId, "test");
ApiFuture<String> messageIdFuture = null;
try {
       publisher = Publisher.newBuilder(topic).build();
       ByteString data = ByteString.copyFromUtf8("my-message");
       PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
       messageIdFuture = publisher.publish(pubsubMessage);
    } catch (IOException e) {
       e.printStackTrace();
    } finally {
        messageIdFuture.get();    //This resolves this issue.
        // Wait on any pending requests
        if (publisher != null) {
            publisher.shutdown();
            //publisher.awaitTermination(1, TimeUnit.SECONDS);
        }
    }