当前位置: 首页>>代码示例>>Java>>正文


Java PubsubMessage.setAttributes方法代码示例

本文整理汇总了Java中com.google.api.services.pubsub.model.PubsubMessage.setAttributes方法的典型用法代码示例。如果您正苦于以下问题:Java PubsubMessage.setAttributes方法的具体用法?Java PubsubMessage.setAttributes怎么用?Java PubsubMessage.setAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.api.services.pubsub.model.PubsubMessage的用法示例。


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

示例1: publishDataToPubSub

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
/**
 * Publish 'numMessages' arbitrary events from live users with the provided delay, to a
 * PubSub topic.
 */
public static void publishDataToPubSub(int numMessages, int delayInMillis)
    throws IOException {
  List<PubsubMessage> pubsubMessages = new ArrayList<>();

  for (int i = 0; i < Math.max(1, numMessages); i++) {
    Long currTime = System.currentTimeMillis();
    String message = generateEvent(currTime, delayInMillis);
    PubsubMessage pubsubMessage = new PubsubMessage()
        .encodeData(message.getBytes("UTF-8"));
    pubsubMessage.setAttributes(
        ImmutableMap.of(TIMESTAMP_ATTRIBUTE,
            Long.toString((currTime - delayInMillis) / 1000 * 1000)));
    if (delayInMillis != 0) {
      System.out.println(pubsubMessage.getAttributes());
      System.out.println("late data for: " + message);
    }
    pubsubMessages.add(pubsubMessage);
  }

  PublishRequest publishRequest = new PublishRequest();
  publishRequest.setMessages(pubsubMessages);
  pubsub.projects().topics().publish(topic, publishRequest).execute();
}
 
开发者ID:davorbonaci,项目名称:beam-portability-demo,代码行数:28,代码来源:Injector.java

示例2: publishData

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
/**
 * Publish 'numMessages' arbitrary events from live users with the provided delay, to a
 * PubSub topic.
 */
public static void publishData(int numMessages, int delayInMillis)
    throws IOException {
  List<PubsubMessage> pubsubMessages = new ArrayList<>();

  for (int i = 0; i < Math.max(1, numMessages); i++) {
    Long currTime = System.currentTimeMillis();
    String message = generateEvent(currTime, delayInMillis);
    PubsubMessage pubsubMessage = new PubsubMessage()
            .encodeData(message.getBytes("UTF-8"));
    pubsubMessage.setAttributes(
        ImmutableMap.of(TIMESTAMP_ATTRIBUTE,
            Long.toString((currTime - delayInMillis) / 1000 * 1000)));
    if (delayInMillis != 0) {
      System.out.println(pubsubMessage.getAttributes());
      System.out.println("late data for: " + message);
    }
    pubsubMessages.add(pubsubMessage);
  }

  PublishRequest publishRequest = new PublishRequest();
  publishRequest.setMessages(pubsubMessages);
  pubsub.projects().topics().publish(topic, publishRequest).execute();
}
 
开发者ID:apache,项目名称:beam,代码行数:28,代码来源:Injector.java

示例3: publish

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
@Override
public int publish(TopicPath topic, List<OutgoingMessage> outgoingMessages)
    throws IOException {
  List<PubsubMessage> pubsubMessages = new ArrayList<>(outgoingMessages.size());
  for (OutgoingMessage outgoingMessage : outgoingMessages) {
    PubsubMessage pubsubMessage = new PubsubMessage().encodeData(outgoingMessage.elementBytes);
    pubsubMessage.setAttributes(getMessageAttributes(outgoingMessage));
    pubsubMessages.add(pubsubMessage);
  }
  PublishRequest request = new PublishRequest().setMessages(pubsubMessages);
  PublishResponse response = pubsub.projects()
                                   .topics()
                                   .publish(topic.getPath(), request)
                                   .execute();
  return response.getMessageIds().size();
}
 
开发者ID:apache,项目名称:beam,代码行数:17,代码来源:PubsubJsonClient.java

示例4: processElement

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
@Override
public void processElement(ProcessContext c) throws IOException {
  if (c.element().isEmpty()) {
    return;
  }
  PubsubMessage pubsubMessage = new PubsubMessage();
  pubsubMessage.encodeData(c.element().getBytes());
  if (timestampLabelKey != null) {
    pubsubMessage.setAttributes(
        ImmutableMap.of(timestampLabelKey, Long.toString(c.timestamp().getMillis())));
  }
  PublishRequest publishRequest = new PublishRequest();
  publishRequest.setMessages(Arrays.asList(pubsubMessage));
  this.pubsub.projects().topics().publish(outputTopic, publishRequest).execute();
}
 
开发者ID:sinmetal,项目名称:iron-hippo,代码行数:16,代码来源:PubsubFileInjector.java

示例5: publish

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
/**
 * Publishes a message to Google Cloud Pub/Sub.
 * TODO(rshanky) - Provide config option for automatic topic creation on publish/subscribe through
 * Google Cloud Pub/Sub.
 *
 * @param msg the message and attributes to be published is contained in this object.
 * @throws IOException is thrown on Google Cloud Pub/Sub publish(or createTopic) API failure.
 */
@Override
public void publish(PublishMessage msg) throws IOException {
  String publishTopic = createFullGcloudPubsubTopic(createPubSubTopic(msg.getMqttTopic()));
  PubsubMessage pubsubMessage = new PubsubMessage();
  byte[] payload = convertMqttPayloadToGcloudPayload(msg.getMqttPaylaod());
  pubsubMessage.setData(new String(payload));
  // create attributes for the message
  Map<String, String> attributes = ImmutableMap.of(MQTT_CLIENT_ID, msg.getMqttClientId(),
      MQTT_TOPIC_NAME, msg.getMqttTopic(),
      MQTT_MESSAGE_ID, msg.getMqttMessageId() == null ? "" : msg.getMqttMessageId().toString(),
      MQTT_RETAIN, msg.isMqttMessageRetained().toString(),
      PROXY_SERVER_ID, serverName);
  pubsubMessage.setAttributes(attributes);
  // publish message
  List<PubsubMessage> messages = ImmutableList.of(pubsubMessage);
  PublishRequest publishRequest = new PublishRequest().setMessages(messages);
  try {
    pubsub.projects().topics().publish(publishTopic, publishRequest).execute();
  } catch (GoogleJsonResponseException e) {
    if (e.getStatusCode() == RESOURCE_NOT_FOUND) {
      logger.info("Cloud PubSub Topic Not Found");
      createTopic(publishTopic);
      pubsub.projects().topics().publish(publishTopic, publishRequest).execute();
    } else {
      // re-throw the exception so that we do not send a PUBACK
      throw e;
    }
  }
  logger.info("Google Cloud Pub/Sub publish SUCCESS for topic " + publishTopic);
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-mqtt-proxy,代码行数:39,代码来源:GcloudPubsub.java


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