本文整理汇总了Java中com.google.cloud.pubsub.v1.Subscriber.addListener方法的典型用法代码示例。如果您正苦于以下问题:Java Subscriber.addListener方法的具体用法?Java Subscriber.addListener怎么用?Java Subscriber.addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.cloud.pubsub.v1.Subscriber
的用法示例。
在下文中一共展示了Subscriber.addListener方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: produce
import com.google.cloud.pubsub.v1.Subscriber; //导入方法依赖的package包/类
@Override
public void produce(Map<String, String> lastOffsets, int maxBatchSize) throws StageException {
SynchronousQueue<MessageReplyConsumerBundle> workQueue = new SynchronousQueue<>();
SubscriptionName subscriptionName = SubscriptionName.create(conf.credentials.projectId, conf.subscriptionId);
executor = Executors.newFixedThreadPool(getNumberOfThreads());
for (int i = 0; i < conf.maxThreads; i++) {
MessageProcessor messageProcessor = new MessageProcessorImpl(
getContext(),
Math.min(maxBatchSize, conf.basic.maxBatchSize),
conf.basic.maxWaitTime,
parserFactory,
workQueue
);
executor.submit(messageProcessor);
messageProcessors.add(messageProcessor);
}
ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder()
.setExecutorThreadCount(conf.advanced.numThreadsPerSubscriber)
.build();
ChannelProvider channelProvider = getChannelProvider();
FlowControlSettings flowControlSettings = getFlowControlSettings();
for (int i = 0; i < conf.advanced.numSubscribers; i++) {
Subscriber s = Subscriber.defaultBuilder(subscriptionName, new MessageReceiverImpl(workQueue))
.setCredentialsProvider(credentialsProvider)
.setExecutorProvider(executorProvider)
.setChannelProvider(channelProvider)
.setFlowControlSettings(flowControlSettings)
.build();
s.addListener(new Subscriber.Listener() {
@Override
public void failed(Subscriber.State from, Throwable failure) {
LOG.error("Exception thrown in Subscriber: {}", failure.toString(), failure);
LOG.error("Subscriber state: {}", from.toString());
Throwables.propagate(failure);
}
}, MoreExecutors.directExecutor());
subscribers.add(s);
}
try {
subscribers.forEach(Subscriber::startAsync);
} finally {
LOG.info("Started {} subscribers.", conf.maxThreads);
}
while (!getContext().isStopped()) {
ThreadUtil.sleep(1000);
}
}