本文整理汇总了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();
}
示例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();
}
示例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();
}
示例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();
}
示例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);
}