gpt4 book ai didi

java - 使用异步拉取持续从 Google PubSub 接收消息

转载 作者:行者123 更新时间:2023-12-02 10:04:45 28 4
gpt4 key购买 nike

借助这个link ,我成功创建了一个小型 Java 应用程序,可以在一分钟内提取已发布的消息。我的实现看起来像这样。

    public static void eventListener() throws InterruptedException {

MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
System.out.println("Received message: " + message.getData().toStringUtf8());
consumer.ack();
}
};
//Subscriber subscriber = null;
try {
subscriber = Subscriber.newBuilder(subscription, receiver)
.setCredentialsProvider(FixedCredentialsProvider.create(creds)).build();
subscriber.addListener(new Subscriber.Listener() {
@Override
public void failed(Subscriber.State from, Throwable failure) {
// Handle failure. This is called when the Subscriber encountered a fatal error
// and is
// shutting down.
System.err.println(failure);
}
}, MoreExecutors.directExecutor());
subscriber.startAsync().awaitRunning();

// In this example, we will pull messages for one minute (60,000ms) then stop.
// In a real application, this sleep-then-stop is not necessary.
// Simply call stopAsync().awaitTerminated() when the server is shutting down,
// etc.
Thread.sleep(60000);
} finally {
if (subscriber != null) {
subscriber.stopAsync().awaitTerminated();
}
}
}

当我在main中调用这个方法时

public static void main(String[] args) throws InterruptedException {
eventListener();

}

并将对象上传到我的 Google 云存储,程序会打印发布者发送的消息,如下所示

Received message: {
"kind": "storage#object",
"id": "roshanbucket/stones.jpg/1553765105996166",
"selfLink": "https://www.googleapis.com/storage/v1/b/roshanbucket/o/stones.jpg",
"name": "stones.jpg",
"bucket": "roshanbucket",
"generation": "1553765105996166",
"metageneration": "1",
"contentType": "image/jpeg",
"timeCreated": "2019-03-28T09:25:05.995Z",
"updated": "2019-03-28T09:25:05.995Z",
"storageClass": "STANDARD",
"timeStorageClassUpdated": "2019-03-28T09:25:05.995Z",
"size": "137256",
"md5Hash": "1GmpUnGeiW+/KU+0U8c8Wg==",
"mediaLink": "https://www.googleapis.com/download/storage/v1/b/roshanbucket/o/stones.jpg?generation=1553765105996166&alt=media",
"crc32c": "FMaEGg==",
"etag": "CIaj1InCpOECEAE="
}

程序执行后的一分钟内,它会打印对象上传帐户上收到的所有消息,然后停止。要在一分钟后接收事件消息,我需要重新启动应用程序。现在,我想做的是连续运行监听器,因此,我尝试在 main 方法内的无限循环内运行方法 eventListener() ,如下所示

    public static void main(String[] args) throws InterruptedException {
while(true) {
eventListener();
}
}

有了这个,我似乎能够在每次上传后立即收到事件消息,无论我何时上传对象。但是,每隔一段时间,它就会抛出这个堆栈跟踪。

    Mar 28, 2019 12:56:34 PM io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference cleanQueue
SEVERE: *~*~*~ Channel ManagedChannelImpl{logId=6, target=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:103)
at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:53)
at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:44)
at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:440)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:223)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:164)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:156)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:157)
at com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub.create(GrpcSubscriberStub.java:260)
at com.google.cloud.pubsub.v1.Subscriber.doStart(Subscriber.java:268)
at com.google.api.core.AbstractApiService$InnerService.doStart(AbstractApiService.java:148)
at com.google.common.util.concurrent.AbstractService.startAsync(AbstractService.java:225)
at com.google.api.core.AbstractApiService.startAsync(AbstractApiService.java:120)
at com.google.cloud.pubsub.v1.Subscriber.startAsync(Subscriber.java:260)
at listener.AsynchronousPull.eventListener(AsynchronousPull.java:57)
at listener.AsynchronousPull.main(AsynchronousPull.java:74)

但是,它仍然在每次上传后打印消息,同时偶尔抛出堆栈跟踪。我对线程没有太多经验,非常感谢您提供解决此问题的帮助。

最佳答案

在紧密循环中调用 eventListener() 并不是您想要在此处执行的操作。这将创建许多新的订阅者实例,这些实例接收消息,每个实例的生存时间为 60 秒。您想要的是让您创建的订阅者的单个实例一直存在,直到您想要将其关闭为止。通常,您可以通过创建订阅者并通过 awaitTermulated() 等待其终止来完成此操作。

上面的代码将被修改为这样:

public static void eventListener() throws InterruptedException {
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
System.out.println("Received message: " + message.getData().toStringUtf8());
consumer.ack();
}
};
Subscriber subscriber = null;
try {
subscriber = Subscriber.newBuilder(subscription, receiver)
.setCredentialsProvider(FixedCredentialsProvider.create(creds)).build();
subscriber.addListener(new Subscriber.Listener() {
@Override
public void failed(Subscriber.State from, Throwable failure) {
// Handle failure. This is called when the Subscriber encountered a fatal error
// and is
// shutting down.
System.err.println(failure);
}
}, MoreExecutors.directExecutor());
subscriber.startAsync().awaitRunning();
subscriber.awaitTerminated();
} finally {
if (subscriber != null) {
subscriber.stopAsync().awaitTerminated();
}
}
}

public static void main(String[] args) throws InterruptedException {
eventListener();
}

如果您只是希望订阅者在应用程序终止时停止,并且不想进行任何额外的清理,那么上面的代码将起作用,允许订阅者运行并接收消息,直到发生错误或应用程序停止运行。关闭。如果您想在应用程序的干净终止时进行一些清理,例如,您想确保已由 receiveMessage 处理的任何消息运行完成,那么您可以 attach a shutdown hook捕获此类终止(尽管它不会在所有情况下运行)。在此 Hook 中,您将调用 stopAsync()。例如,您可以在 try block 之前插入以下内容:

Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
subscriber.stopAsync().awaitTerminated();
}
});

关于java - 使用异步拉取持续从 Google PubSub 接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55395972/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com