我已经使用了来自Google文档的Google PubSub订阅者示例 我所做的唯一修改是注释掉了对消息的确认。
订阅者不再将消息添加到队列中,而是应根据google云控制台中设置的时间间隔重新发送消息。
为什么会这样?或者我缺少什么?
public class SubscriberExample {
use the default project id
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
private static final BlockingQueue<PubsubMessage> messages = new LinkedBlockingDeque<>();
static class MessageReceiverExample implements MessageReceiver {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
messages.offer(message);
//consumer.ack();
}
}
/** Receive messages over a subscription. */
public static void main(String[] args) throws Exception {
// set subscriber id, eg. my-sub
String subscriptionId = args[0];
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(
PROJECT_ID, subscriptionId);
Subscriber subscriber = null;
try {
// create a subscriber bound to the asynchronous message receiver
subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build();
subscriber.startAsync().awaitRunning();
// Continue to listen to messages
while (true) {
PubsubMessage message = messages.take();
System.out.println("Message Id: " + message.getMessageId());
System.out.println("Data: " + message.getData().toStringUtf8());
}
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
}
答案 0 :(得分:1)
当您不确认消息时,Java客户端库将在消息上调用modifyAckDeadline,直到通过maxAckExtensionPeriod。默认情况下,此值为一小时。因此,如果您不确认/不确认消息或更改此值,则很可能在一个小时内不会重新发送该消息。如果要更改最大确认扩展期,请在构建器上进行设置:
subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample())
.setMaxAckExtensionPeriod(Duration.ofSeconds(60))
.build();
还值得注意的是,当您不确认或拒绝消息时,flow control可能会阻止传递更多消息。默认情况下,Java客户端库最多允许处理1000条待处理的邮件,即等待ack或nack或最长ack扩展时间过去。