use of com.google.pubsub.v1.PubsubMessage in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusherTest method getFirstPublishedMessage.
private PubsubMessage getFirstPublishedMessage() {
final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
final ArgumentCaptor<PubsubMessage> argumentCaptor = ArgumentCaptor.forClass(PubsubMessage.class);
verify(publisher).publish(argumentCaptor.capture());
return argumentCaptor.getValue();
}
use of com.google.pubsub.v1.PubsubMessage in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusherTest method testMessagesHavePartyIdAttribute.
@Test
public void testMessagesHavePartyIdAttribute() {
processSingleMessage();
final PubsubMessage deliveredMessage = getFirstPublishedMessage();
assertEquals(partyId.orElseThrow(IllegalStateException::new).toString(), deliveredMessage.getAttributesOrThrow("partyIdentifier"));
}
use of com.google.pubsub.v1.PubsubMessage in project java-docs-samples by GoogleCloudPlatform.
the class SubscriberExample method main.
/**
* Receive messages over a subscription.
*/
public static void main(String... args) throws Exception {
// set subscriber id, eg. my-sub
String subscriptionId = args[0];
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.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();
}
}
}
use of com.google.pubsub.v1.PubsubMessage in project java-docs-samples by GoogleCloudPlatform.
the class PubSubPublishTest method servletPublishesPayloadMessage.
@Test
public void servletPublishesPayloadMessage() throws Exception {
assertNotNull(System.getenv("PUBSUB_TOPIC"));
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getParameter("payload")).thenReturn("test-message");
HttpServletResponse response = mock(HttpServletResponse.class);
Publisher publisher = mock(Publisher.class);
PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("test-message")).build();
when(publisher.publish(eq(message))).thenReturn(SettableApiFuture.create());
PubSubPublish pubSubPublish = new PubSubPublish(publisher);
// verify content of published test message
pubSubPublish.doPost(request, response);
verify(publisher, times(1)).publish(eq(message));
}
use of com.google.pubsub.v1.PubsubMessage in project java-docs-samples by GoogleCloudPlatform.
the class PubSubPublish method doPost.
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Publisher publisher = this.publisher;
// construct a pubsub message from the payload
final String payload = req.getParameter("payload");
Message message = new Message(null);
message.setData(payload);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).putAttributes("sourceLang", req.getParameter("sourceLang")).putAttributes("targetLang", req.getParameter("targetLang")).build();
String topicId = System.getenv("PUBSUB_TOPIC");
// create a publisher on the topic
if (publisher == null) {
this.publisher = publisher = Publisher.newBuilder(ProjectTopicName.newBuilder().setProject(ServiceOptions.getDefaultProjectId()).setTopic(topicId).build()).build();
}
publisher.publish(pubsubMessage);
// redirect to home page
resp.sendRedirect("/");
}
Aggregations