我正在尝试实现Spring Cloud Pub/Sub,我遵循了指南,但如何降低异常,这使它开始消耗高级别的处理,我研究了很多,他们说要将参数设置为awaitTerminated,但我找不到如何定义这个参数。
版本:
<spring-cloud-gcp.version>2.0.4</spring-cloud-gcp.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
豆类
@Bean
public DefaultPublisherFactory defaultPublisherFactory(GcpProjectIdProvider gcpProjectIdProvider) {
DefaultPublisherFactory factory = new DefaultPublisherFactory(gcpProjectIdProvider);
factory.setEnableMessageOrdering(true);
factory.setEndpoint("us-east1-pubsub.googleapis.com:443");
return factory;
}
@Bean
@ServiceActivator(inputChannel = "pubSubOutputChannel")
public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
PubSubMessageHandler adapter = new PubSubMessageHandler(pubsubTemplate, "noMatter");
adapter.setFailureCallback((cause, message) ->
System.err.println("Fail to send message " + message)
);
return adapter;
}
@MessagingGateway(defaultRequestChannel = "pubSubOutputChannel")
public interface PubSubOutboundGateway {
void sendToPubSub(@Header(GcpPubSubHeaders.TOPIC) String topic, String payload);
}
异常
2021-12-22 10:01:08.246 ERROR 60276 --- [LoopGroup-80-34] i.g.i.ManagedChannelOrphanWrapper : *~*~*~ Channel ManagedChannelImpl{logId=869, target=us-east1-pubsub.googleapis.com:443} was not shutdown properly!!! ~*~*~*
Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
java.lang.RuntimeException: ManagedChannel allocation site
at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.<init>(ManagedChannelOrphanWrapper.java:93) ~[grpc-core-1.39.0.jar:1.39.0]
at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:53) ~[grpc-core-1.39.0.jar:1.39.0]
at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:44) ~[grpc-core-1.39.0.jar:1.39.0]
at io.grpc.internal.ManagedChannelImplBuilder.build(ManagedChannelImplBuilder.java:634) ~[grpc-core-1.39.0.jar:1.39.0]
at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:264) ~[grpc-core-1.39.0.jar:1.39.0]
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:360) ~[gax-grpc-1.66.0.jar:1.66.0]
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access$1800(InstantiatingGrpcChannelProvider.java:81) ~[gax-grpc-1.66.0.jar:1.66.0]
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider$1.createSingleChannel(InstantiatingGrpcChannelProvider.java:231) ~[gax-grpc-1.66.0.jar:1.66.0]
at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72) ~[gax-grpc-1.66.0.jar:1.66.0]
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:241) ~[gax-grpc-1.66.0.jar:1.66.0]
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:219) ~[gax-grpc-1.66.0.jar:1.66.0]
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:199) ~[gax-1.66.0.jar:1.66.0]
at com.google.cloud.pubsub.v1.stub.GrpcPublisherStub.create(GrpcPublisherStub.java:195) ~[google-cloud-pubsub-1.113.5.jar:1.113.5]
at com.google.cloud.pubsub.v1.Publisher.<init>(Publisher.java:188) ~[google-cloud-pubsub-1.113.5.jar:1.113.5]
at com.google.cloud.pubsub.v1.Publisher.<init>(Publisher.java:88) ~[google-cloud-pubsub-1.113.5.jar:1.113.5]
at com.google.cloud.pubsub.v1.Publisher$Builder.build(Publisher.java:829) ~[google-cloud-pubsub-1.113.5.jar:1.113.5]
我尽可能简单地解决了这个问题,最终删除了BeanDefaultPublisherFactory并解决了问题。
我收到这个错误是因为我没有关闭SubscriptionAdminClient。我通过使用try-with-resources
块中的对象来解决问题:
try (SubscriptionAdminClient subscriptionAdminClient = createSubscriptionAdminClient()) {
subscriptionAdminClient.createSubscription(...);
...
} catch (IOException e) {
throw new RuntimeException("Failure...", e);
}