• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java ApiFutureCallback类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.google.api.core.ApiFutureCallback的典型用法代码示例。如果您正苦于以下问题:Java ApiFutureCallback类的具体用法?Java ApiFutureCallback怎么用?Java ApiFutureCallback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ApiFutureCallback类属于com.google.api.core包,在下文中一共展示了ApiFutureCallback类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: flush

import com.google.api.core.ApiFutureCallback; //导入依赖的package包/类
@Override
public void flush() {
  List<Span> spans = new ArrayList<>();
  queue.drain(spans::add);
  if (spans.isEmpty()) {
    return;
  }
  PatchTracesRequest request =
      PatchTracesRequest.newBuilder()
          .setProjectId(projectId)
          .setTraces(Traces.newBuilder().addAllTraces(translator.translateSpans(spans)))
          .build();
  ApiFutures.addCallback(
      traceServiceClient.patchTracesCallable().futureCall(request),
      new ApiFutureCallback<Empty>() {
        @Override
        public void onFailure(Throwable t) {
          logger.warn("Error reporting traces.", t);
        }

        @Override
        public void onSuccess(Empty result) {
          logger.info("Successfully reported traces.");
        }
      });
}
 
开发者ID:curioswitch,项目名称:curiostack,代码行数:27,代码来源:StackdriverReporter.java


示例2: doWrite

import com.google.api.core.ApiFutureCallback; //导入依赖的package包/类
private static void doWrite(
    DatabaseReference ref, final boolean shouldSucceed, final boolean shouldTimeout)
    throws InterruptedException {
  final CountDownLatch lock = new CountDownLatch(1);
  final AtomicBoolean success = new AtomicBoolean(false);
  ApiFutures.addCallback(ref.setValueAsync("wrote something"), new ApiFutureCallback<Void>() {
    @Override
    public void onFailure(Throwable throwable) {
      success.compareAndSet(false, false);
      lock.countDown();
    }

    @Override
    public void onSuccess(Void result) {
      success.compareAndSet(false, true);
      lock.countDown();
    }
  });
  boolean finished = lock.await(TestUtils.TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  if (shouldTimeout) {
    assertTrue("Write finished (expected to timeout).", !finished);
  } else if (shouldSucceed) {
    assertTrue("Write timed out (expected to succeed)", finished);
    assertTrue("Write failed (expected to succeed).", success.get());
  } else {
    assertTrue("Write timed out (expected to fail).", finished);
    assertTrue("Write successful (expected to fail).", !success.get());
  }
}
 
开发者ID:firebase,项目名称:firebase-admin-java,代码行数:30,代码来源:FirebaseDatabaseAuthTestIT.java


示例3: publish

import com.google.api.core.ApiFutureCallback; //导入依赖的package包/类
@Override
public ListenableFuture<String> publish(final String topic, PubsubMessage pubsubMessage) {
	ApiFuture<String> publishFuture =
			this.publisherFactory.createPublisher(topic).publish(pubsubMessage);

	final SettableListenableFuture<String> settableFuture = new SettableListenableFuture<>();
	ApiFutures.addCallback(publishFuture, new ApiFutureCallback<String>() {

		@Override
		public void onFailure(Throwable throwable) {
			LOGGER.warn("Publishing to " + topic + " topic failed.", throwable);
			settableFuture.setException(throwable);
		}

		@Override
		public void onSuccess(String result) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(
						"Publishing to " + topic + " was successful. Message ID: " + result);
			}
			settableFuture.set(result);
		}

	});

	return settableFuture;
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-gcp,代码行数:28,代码来源:PubSubTemplate.java


示例4: doRun

import com.google.api.core.ApiFutureCallback; //导入依赖的package包/类
@Override
public ListenableFuture<RunResult> doRun() {
  AtomicInteger numPending = new AtomicInteger(batchSize);
  final SettableFuture<RunResult> done = SettableFuture.create();
  String sendTime = String.valueOf(System.currentTimeMillis());
  if (!outstandingBytes.tryAcquire(batchSize * messageSize)) {
    return Futures.immediateFailedFuture(new Exception("Flow control limits reached."));
  }
  for (int i = 0; i < batchSize; i++) {
    ApiFutures.addCallback(publisher
        .publish(
            PubsubMessage.newBuilder()
            .setData(payload)
            .putAttributes("sendTime", sendTime)
            .putAttributes("clientId", id.toString())
            .putAttributes(
                "sequenceNumber", Integer.toString(sequenceNumber.getAndIncrement()))
            .build()), new ApiFutureCallback<String>() {
          @Override
          public void onSuccess(String messageId) {
            outstandingBytes.release(messageSize);
            if (numPending.decrementAndGet() == 0) {
              done.set(RunResult.fromBatchSize(batchSize));
            }
          }

          @Override
          public void onFailure(Throwable t) {
            outstandingBytes.release(messageSize);
            done.setException(t);
          }
        });
  }
  return done;
}
 
开发者ID:GoogleCloudPlatform,项目名称:pubsub,代码行数:36,代码来源:CPSPublisherTask.java


示例5: main

import com.google.api.core.ApiFutureCallback; //导入依赖的package包/类
/** Publish messages to a topic.
 * @param args topic name, number of messages
 */
public static void main(String... args) throws Exception {
  // topic id, eg. "my-topic"
  String topicId = args[0];
  int messageCount = Integer.parseInt(args[1]);
  TopicName topicName = TopicName.of(PROJECT_ID, topicId);
  Publisher publisher = null;
  try {
    // Create a publisher instance with default settings bound to the topic
    publisher = Publisher.newBuilder(topicName).build();

    for (int i = 0; i < messageCount; i++) {
      String message = "message-" + i;

      // convert message to bytes
      ByteString data = ByteString.copyFromUtf8(message);
      PubsubMessage pubsubMessage = PubsubMessage.newBuilder()
          .setData(data)
          .build();

      //schedule a message to be published, messages are automatically batched
      ApiFuture<String> future = publisher.publish(pubsubMessage);

      // add an asynchronous callback to handle success / failure
      ApiFutures.addCallback(future, new ApiFutureCallback<String>() {

        @Override
        public void onFailure(Throwable throwable) {
          if (throwable instanceof ApiException) {
            ApiException apiException = ((ApiException) throwable);
            // details on the API exception
            System.out.println(apiException.getStatusCode().getCode());
            System.out.println(apiException.isRetryable());
          }
          System.out.println("Error publishing message : " + message);
        }

        @Override
        public void onSuccess(String messageId) {
          // Once published, returns server-assigned message ids (unique within the topic)
          System.out.println(messageId);
        }
      });
    }
  } finally {
    if (publisher != null) {
      // When finished with the publisher, shutdown to free up resources.
      publisher.shutdown();
    }
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:54,代码来源:PublisherExample.java



注:本文中的com.google.api.core.ApiFutureCallback类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java StringUtils类代码示例发布时间:2022-05-23
下一篇:
Java AxmlReader类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap