/** Example of publishing a message. */ // [TARGET publish(PubsubMessage)] // [VARIABLE "my_message"] public ApiFuture<String> publish(String message) { ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage); ApiFutures.addCallback( messageIdFuture, new ApiFutureCallback<String>() { public void onSuccess(String messageId) { System.out.println("published with message id: " + messageId); } public void onFailure(Throwable t) { System.out.println("failed to publish: " + t); } }, Executors.newSingleThreadExecutor()); return messageIdFuture; }
final ByteString flowFileContent = ByteString.copyFromUtf8(baos.toString()); PubsubMessage message = PubsubMessage.newBuilder().setData(flowFileContent) .setPublishTime(Timestamp.newBuilder().build()) .putAllAttributes(getDynamicAttributesMap(context, flowFile))
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
@Override public int publish(TopicPath topic, List<OutgoingMessage> outgoingMessages) throws IOException { PublishRequest.Builder request = PublishRequest.newBuilder().setTopic(topic.getPath()); for (OutgoingMessage outgoingMessage : outgoingMessages) { PubsubMessage.Builder message = PubsubMessage.newBuilder().setData(ByteString.copyFrom(outgoingMessage.elementBytes)); if (outgoingMessage.attributes != null) { message.putAllAttributes(outgoingMessage.attributes); } if (timestampAttribute != null) { message .getMutableAttributes() .put(timestampAttribute, String.valueOf(outgoingMessage.timestampMsSinceEpoch)); } if (idAttribute != null && !Strings.isNullOrEmpty(outgoingMessage.recordId)) { message.getMutableAttributes().put(idAttribute, outgoingMessage.recordId); } request.addMessages(message); } PublishResponse response = publisherStub().publish(request.build()); return response.getMessageIdsCount(); }
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom( other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } onChanged(); return this; }
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom(other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } this.mergeUnknownFields(other.unknownFields); onChanged(); return this; }
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom( other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } onChanged(); return this; }
@Override public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) { try { PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder() .setData(ByteString.copyFrom(this.objectMapper.writeValueAsBytes(payload))); if (headers != null) { pubsubMessageBuilder.putAllAttributes(headers); } return pubsubMessageBuilder.build(); } catch (JsonProcessingException ex) { throw new PubSubMessageConversionException("JSON serialization of an object of type " + payload.getClass().getName() + " failed.", ex); } }
@Override public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) { try { PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder() .setData(ByteString.copyFrom(this.objectMapper.writeValueAsBytes(payload))); if (headers != null) { pubsubMessageBuilder.putAllAttributes(headers); } return pubsubMessageBuilder.build(); } catch (JsonProcessingException ex) { throw new PubSubMessageConversionException("JSON serialization of an object of type " + payload.getClass().getName() + " failed.", ex); } }
public void publish(String jsonPayload, Map<String, String> attributes) { PubsubMessage message = PubsubMessage.newBuilder() .setData(ByteString.copyFromUtf8(jsonPayload)) .putAllAttributes(attributes) .build(); log.debug("Publishing message on Google Pubsub topic {}", this.getFullTopicName()); ApiFuture<String> future = publisher.publish(message); ApiFutures.addCallback(future, new PublishCallback(this.getFullTopicName())); }
private void publishPubSub(ByteString bytes) { final ApiFuture<String> publish = publisher.publish( PubsubMessage.newBuilder().setData(bytes).build()); ApiFutures.addCallback(publish, new ApiFutureCallback<String>() { @Override public void onFailure(Throwable t) { log.error("Failed sending metrics {}", t.getMessage()); } @Override public void onSuccess(String messageId) { } }, executorService); }
.setData(data) .build();
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
@Test public void testPublishByNumBatchedMessages() throws Exception { Publisher publisher = getTestPublisherBuilder() .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(2L) .setDelayThreshold(Duration.ofSeconds(100)) .build()) .build(); testPublisherServiceImpl .addPublishResponse(PublishResponse.newBuilder().addMessageIds("1").addMessageIds("2")) .addPublishResponse(PublishResponse.newBuilder().addMessageIds("3").addMessageIds("4")); ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A"); ApiFuture<String> publishFuture2 = sendTestMessage(publisher, "B"); ApiFuture<String> publishFuture3 = sendTestMessage(publisher, "C"); // Note we are not advancing time but message should still get published assertEquals("1", publishFuture1.get()); assertEquals("2", publishFuture2.get()); assertFalse(publishFuture3.isDone()); ApiFuture<String> publishFuture4 = publisher.publish(PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("D")).build()); assertEquals("3", publishFuture3.get()); assertEquals("4", publishFuture4.get()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(0).getMessagesCount()); assertEquals(2, testPublisherServiceImpl.getCapturedRequests().get(1).getMessagesCount()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
@Test @SuppressWarnings("all") public void publishTest() { String messageIdsElement = "messageIdsElement-744837059"; List<String> messageIds = Arrays.asList(messageIdsElement); PublishResponse expectedResponse = PublishResponse.newBuilder().addAllMessageIds(messageIds).build(); mockPublisher.addResponse(expectedResponse); ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); ByteString data = ByteString.copyFromUtf8("-86"); PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build(); List<PubsubMessage> messages = Arrays.asList(messagesElement); PublishResponse actualResponse = client.publish(topic, messages); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests(); Assert.assertEquals(1, actualRequests.size()); PublishRequest actualRequest = (PublishRequest) actualRequests.get(0); Assert.assertEquals(topic, ProjectTopicName.parse(actualRequest.getTopic())); Assert.assertEquals(messages, actualRequest.getMessagesList()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Override public void sendAsync(RowMap r, AbstractAsyncProducer.CallbackCompleter cc) throws Exception { String message = r.toJSON(outputConfig); ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); if ( r instanceof DDLMap ) { ApiFuture<String> apiFuture = ddlPubsub.publish(pubsubMessage); PubsubCallback callback = new PubsubCallback(cc, r.getNextPosition(), message, this.succeededMessageCount, this.failedMessageCount, this.succeededMessageMeter, this.failedMessageMeter, this.context); ApiFutures.addCallback(apiFuture, callback); } else { ApiFuture<String> apiFuture = pubsub.publish(pubsubMessage); PubsubCallback callback = new PubsubCallback(cc, r.getNextPosition(), message, this.succeededMessageCount, this.failedMessageCount, this.succeededMessageMeter, this.failedMessageMeter, this.context); ApiFutures.addCallback(apiFuture, callback); } }
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom(other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } this.mergeUnknownFields(other.unknownFields); onChanged(); return this; }
@Test @SuppressWarnings("all") public void publishExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockPublisher.addException(exception); try { ProjectTopicName topic = ProjectTopicName.of("[PROJECT]", "[TOPIC]"); ByteString data = ByteString.copyFromUtf8("-86"); PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build(); List<PubsubMessage> messages = Arrays.asList(messagesElement); client.publish(topic, messages); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
private ApiFuture<String> sendTestMessage(Publisher publisher, String data) { return publisher.publish( PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(data)).build()); }