use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.
the class PubSubTemplateDocumentationTests method testCreatePublishPullNextAndDelete.
@Test
public void testCreatePublishPullNextAndDelete() {
pubSubTest((PubSubTemplate pubSubTemplate, String subscriptionName, String topicName) -> {
// tag::publish[]
Map<String, String> headers = Collections.singletonMap("key1", "val1");
pubSubTemplate.publish(topicName, "message", headers).get();
// end::publish[]
PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName);
assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("message"));
assertThat(pubsubMessage.getAttributesCount()).isEqualTo(1);
assertThat(pubsubMessage.getAttributesOrThrow("key1")).isEqualTo("val1");
});
}
use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.
the class SimplePubSubMessageConverter method toPubSubMessage.
@Override
public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) {
ByteString convertedPayload;
if (payload instanceof ByteString) {
convertedPayload = (ByteString) payload;
} else if (payload instanceof String) {
convertedPayload = ByteString.copyFrom(((String) payload).getBytes(this.charset));
} else if (payload instanceof ByteBuffer) {
convertedPayload = ByteString.copyFrom((ByteBuffer) payload);
} else if (payload instanceof byte[]) {
convertedPayload = ByteString.copyFrom((byte[]) payload);
} else {
throw new PubSubMessageConversionException("Unable to convert payload of type " + payload.getClass().getName() + " to byte[] for sending to Pub/Sub.");
}
PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder().setData(convertedPayload);
if (headers != null) {
pubsubMessageBuilder.putAllAttributes(headers);
}
return pubsubMessageBuilder.build();
}
use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.
the class PubSubReactiveFactoryTests method setUpMessages.
/**
* Replays provided messages.
* If a synthetic message "stop" is encountered, immediately returns previously collected messages.
* If a synthetic message "timeout" is encountered, throws an {@link DeadlineExceededException}.
* If a synthetic message "throw" is encountered, throws an {@link RuntimeException}.
* Fails the calling test if there are not enough messages to fulfill demand from cumulative calls to {@code pull()}.
* @param messages messages to replay
*/
private void setUpMessages(String... messages) {
List<String> msgList = new ArrayList<>(Arrays.asList(messages));
when(subscriberOperations.pullAsync(eq("sub1"), any(Integer.class), any(Boolean.class))).then(invocationOnMock -> {
List<AcknowledgeablePubsubMessage> result = new ArrayList<>();
for (int i = 0; i < (Integer) invocationOnMock.getArgument(1); i++) {
if (msgList.isEmpty()) {
fail("Ran out of provided messages.");
}
String nextPayload = msgList.remove(0);
switch(nextPayload) {
case "stop":
return AsyncResult.forValue(result);
case "timeout":
if (!result.isEmpty()) {
fail("Bad setup -- 'throw' should be the first event in batch");
}
return AsyncResult.forExecutionException(new DeadlineExceededException("this is a noop", null, GrpcStatusCode.of(Status.Code.DEADLINE_EXCEEDED), true));
case "throw":
return AsyncResult.forExecutionException(new RuntimeException("expected exception during pull of messages"));
}
AcknowledgeablePubsubMessage msg = mock(AcknowledgeablePubsubMessage.class);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom((nextPayload).getBytes())).build();
when(msg.getPubsubMessage()).thenReturn(pubsubMessage);
result.add(msg);
}
return AsyncResult.forValue(result);
});
}
use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.
the class JacksonPubSubMessageConverterTests method testPojo.
@Test
public void testPojo() throws JSONException {
Contact contact = new Contact("Thomas", "Edison", 8817);
PubsubMessage pubsubMessage = this.converter.toPubSubMessage(contact, null);
JSONAssert.assertEquals("{\"firstName\":\"Thomas\",\"lastName\":\"Edison\",\"zip\":8817}", pubsubMessage.getData().toStringUtf8(), true);
Object o = this.converter.fromPubSubMessage(pubsubMessage, Contact.class);
assertThat(o).as("verify that deserialized object is equal to the original one").isEqualTo(contact);
}
use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method pullNextAsync.
@Override
public ListenableFuture<PubsubMessage> pullNextAsync(String subscription) {
final SettableListenableFuture<PubsubMessage> settableFuture = new SettableListenableFuture<>();
this.pullAndAckAsync(subscription, 1, true).addCallback(messages -> {
PubsubMessage message = messages.isEmpty() ? null : messages.get(0);
settableFuture.set(message);
}, settableFuture::setException);
return settableFuture;
}
Aggregations