use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class SubscriberSnippets method createSubscriber.
private void createSubscriber() throws Exception {
// [START pubsub_pull]
String projectId = "my-project-id";
String subscriptionId = "my-subscription-id";
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
// Instantiate an asynchronous message receiver
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
// handle incoming message, then ack/nack the received message
System.out.println("Id : " + message.getMessageId());
System.out.println("Data : " + message.getData().toStringUtf8());
consumer.ack();
}
};
Subscriber subscriber = null;
try {
// Create a subscriber for "my-subscription-id" bound to the message receiver
subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build();
subscriber.startAsync();
// ...
} finally {
// stop receiving messages
if (subscriber != null) {
subscriber.stopAsync();
}
}
// [END pubsub_pull]
}
use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientTest method publishTest.
@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);
TopicName topic = TopicName.create("[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, actualRequest.getTopicAsTopicName());
Assert.assertEquals(messages, actualRequest.getMessagesList());
}
use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientTest method publishExceptionTest.
@Test
@SuppressWarnings("all")
public void publishExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockPublisher.addException(exception);
try {
TopicName topic = TopicName.create("[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 (ApiException e) {
Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
}
}
use of com.google.pubsub.v1.PubsubMessage in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusher method sendBatch.
@Override
protected ImmutableList<PubsubMessage> sendBatch(final List<PubsubMessage> batch) throws InterruptedException {
// For Pub/Sub we assume the following:
// - Batching behaviour is set to flush everything ASAP.
// - Retry behaviour will retry indefinitely, so long as it seems likely to succeed.
// First start sending the messages.
// (This will serialize them, determine the partition and then assign them to a per-partition buffer.)
final int batchSize = batch.size();
final List<ApiFuture<String>> sendResults = batch.stream().map(publisher::publish).collect(Collectors.toCollection(() -> new ArrayList<>(batchSize)));
// At this point the messages are in flight, and we assume being flushed.
// When they eventually complete, each message can be in one of several states:
// - Completed.
// - An error occurred, but a retry may succeed.
// - A fatal error occurred.
final ImmutableList.Builder<PubsubMessage> remaining = ImmutableList.builder();
for (int i = 0; i < batchSize; ++i) {
final ApiFuture<String> pendingResult = sendResults.get(i);
try {
final String messageId = pendingResult.get();
if (logger.isDebugEnabled()) {
final PubsubMessage message = batch.get(i);
logger.debug("Finished sending event (partyId={}) to Pub/Sub: messageId = {}", message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID), messageId);
}
} catch (final ExecutionException e) {
final PubsubMessage message = batch.get(i);
// The Pub/Sub publisher internally has a retry policy, but outside that we also
// retry indefinitely unless it's a cause that we don't understand.
final Throwable cause = e.getCause();
if (cause instanceof ApiException) {
final ApiException apiException = (ApiException) cause;
if (apiException.isRetryable()) {
if (logger.isDebugEnabled()) {
logger.debug("Transient error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; retrying.", cause);
}
remaining.add(message);
} else {
logger.warn("Permanent error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
}
} else {
logger.error("Unknown error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
}
}
}
return remaining.build();
}
use of com.google.pubsub.v1.PubsubMessage in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusherTest method testMessageBodyIsNakedAvroRecord.
@Test
public void testMessageBodyIsNakedAvroRecord() throws IOException {
processSingleMessage();
final PubsubMessage deliveredMessage = getFirstPublishedMessage();
final ByteString body = deliveredMessage.getData();
final DatumReader<GenericRecord> reader = new GenericDatumReader<>(MINIMAL_SCHEMA);
final Decoder decoder = DecoderFactory.get().binaryDecoder(body.newInput(), null);
final GenericRecord record = reader.read(null, decoder);
assertEquals(partyId.orElseThrow(IllegalStateException::new).toString(), record.get("partyId").toString());
assertEquals(sessionId.orElseThrow(IllegalStateException::new).toString(), record.get("sessionId").toString());
assertEquals(0L, record.get("counter"));
}
Aggregations