本文整理汇总了Java中com.google.cloud.pubsub.v1.Subscriber类的典型用法代码示例。如果您正苦于以下问题:Java Subscriber类的具体用法?Java Subscriber怎么用?Java Subscriber使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Subscriber类属于com.google.cloud.pubsub.v1包,在下文中一共展示了Subscriber类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doRun
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
@Override
public ListenableFuture<RunResult> doRun() {
synchronized (this) {
if (subscriber.isRunning()) {
return Futures.immediateFuture(RunResult.empty());
}
if (shuttingDown) {
return Futures.immediateFailedFuture(
new IllegalStateException("the task is shutting down"));
}
try {
subscriber.startAsync().awaitRunning();
} catch (Exception e) {
log.error("Fatal error from subscriber.", e);
subscriber = Subscriber.defaultBuilder(this.subscription, this).build();
return Futures.immediateFailedFuture(e);
}
return Futures.immediateFuture(RunResult.empty());
}
}
示例2: main
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
/** Receive messages over a subscription. */
public static void main(String... args) throws Exception {
// set subscriber id, eg. my-sub
String subscriptionId = args[0];
SubscriptionName subscriptionName = SubscriptionName.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();
}
}
}
示例3: createSubscriber
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
@Override
public Subscriber createSubscriber(String subscriptionName, MessageReceiver receiver) {
Subscriber.Builder subscriberBuilder = Subscriber.newBuilder(
SubscriptionName.of(this.projectId, subscriptionName), receiver);
if (this.channelProvider != null) {
subscriberBuilder.setChannelProvider(this.channelProvider);
}
if (this.executorProvider != null) {
subscriberBuilder.setExecutorProvider(this.executorProvider);
}
if (this.credentialsProvider != null) {
subscriberBuilder.setCredentialsProvider(this.credentialsProvider);
}
if (this.headerProvider != null) {
subscriberBuilder.setHeaderProvider(this.headerProvider);
}
if (this.systemExecutorProvider != null) {
subscriberBuilder.setSystemExecutorProvider(this.systemExecutorProvider);
}
if (this.flowControlSettings != null) {
subscriberBuilder.setFlowControlSettings(this.flowControlSettings);
}
if (this.maxAckDurationPeriod != null) {
subscriberBuilder.setMaxAckExtensionPeriod(this.maxAckDurationPeriod);
}
if (this.parallelPullCount != null) {
subscriberBuilder.setParallelPullCount(this.parallelPullCount);
}
return subscriberBuilder.build();
}
示例4: testNewSubscriber
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
@Test
public void testNewSubscriber() {
DefaultSubscriberFactory factory = new DefaultSubscriberFactory(() -> "angeldust");
factory.setCredentialsProvider(this.credentialsProvider);
Subscriber subscriber = factory.createSubscriber("midnight cowboy", (message, consumer) -> { });
assertEquals("midnight cowboy", subscriber.getSubscriptionName().getSubscription());
assertEquals("angeldust", subscriber.getSubscriptionName().getProject());
}
示例5: testSubscribe
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
@Test
public void testSubscribe() {
Subscriber subscriber = this.pubSubTemplate.subscribe("testSubscription",
(message, consumer) -> { });
assertEquals(this.mockSubscriber, subscriber);
verify(this.mockSubscriber, times(1)).startAsync();
}
示例6: CPSSubscriberTask
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
private CPSSubscriberTask(StartRequest request) {
super(request, "gcloud", MetricsHandler.MetricName.END_TO_END_LATENCY);
this.subscription =
SubscriptionName.create(request.getProject(), request.getPubsubOptions().getSubscription());
try {
this.subscriber =
Subscriber.defaultBuilder(this.subscription, this)
.setParallelPullCount(Runtime.getRuntime().availableProcessors() * 5)
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例7: shutdown
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
@Override
public void shutdown() {
Subscriber subscriber;
synchronized (this) {
if (shuttingDown) {
throw new IllegalStateException("the task is already shutting down");
}
shuttingDown = true;
subscriber = this.subscriber;
}
// We must stop out of the lock. Stopping waits for all messages to be processed,
// and processing the messages needs to lock.
subscriber.stopAsync().awaitTerminated();
}
示例8: destroy
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
@Override
public void destroy() {
try {
LOG.debug("Stopping subscribers");
subscribers.forEach(Subscriber::stopAsync);
subscribers.forEach(Subscriber::awaitTerminated);
LOG.debug("Stopping message processors");
messageProcessors.forEach(MessageProcessor::stop);
} finally {
LOG.info("Stopped {} processing threads", conf.maxThreads);
subscribers.clear();
messageProcessors.clear();
}
if (executor == null) {
return;
}
LOG.debug("Shutting down executor service");
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
LOG.warn("Orderly shutdown interrupted.");
} finally {
Thread.currentThread().interrupt();
}
}
示例9: subscribe
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
@Override
public Subscriber subscribe(String subscription, MessageReceiver messageHandler) {
Subscriber subscriber = this.subscriberFactory.createSubscriber(subscription, messageHandler);
subscriber.startAsync();
return subscriber;
}
示例10: 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);
}
}
示例11: createSubscriber
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
/**
* Create a {@link Subscriber} for the specified subscription name and wired it up to asynchronously
* deliver messages to the provided {@link MessageReceiver}.
* @param subscriptionName the name of the subscription
* @param receiver the callback for receiving messages asynchronously
* @return the {@link Subscriber} that was created to bind the receiver to the subscription
*/
Subscriber createSubscriber(String subscriptionName, MessageReceiver receiver);
示例12: subscribe
import com.google.cloud.pubsub.v1.Subscriber; //导入依赖的package包/类
/**
* Add a callback method to an existing subscription.
* <p>The created {@link Subscriber} is returned so it can be stopped.
* @param subscription the name of an existing subscription
* @param messageHandler the callback method triggered when new messages arrive
* @return subscriber listening to new messages
*/
Subscriber subscribe(String subscription, MessageReceiver messageHandler);