use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class MessageDispatcher method processOutstandingBatches.
public void processOutstandingBatches() {
while (true) {
boolean batchDone = false;
Runnable batchCallback = null;
OutstandingMessage outstandingMessage;
synchronized (outstandingMessageBatches) {
OutstandingMessagesBatch nextBatch = outstandingMessageBatches.peek();
if (nextBatch == null) {
return;
}
outstandingMessage = nextBatch.messages.peek();
if (outstandingMessage == null) {
return;
}
try {
// This is a non-blocking flow controller.
flowController.reserve(1, outstandingMessage.receivedMessage().getMessage().getSerializedSize());
} catch (FlowController.MaxOutstandingElementCountReachedException | FlowController.MaxOutstandingRequestBytesReachedException flowControlException) {
return;
} catch (FlowControlException unexpectedException) {
throw new IllegalStateException("Flow control unexpected exception", unexpectedException);
}
// We got a hold to the message already.
nextBatch.messages.poll();
batchDone = nextBatch.messages.isEmpty();
if (batchDone) {
outstandingMessageBatches.poll();
batchCallback = nextBatch.doneCallback;
}
}
final PubsubMessage message = outstandingMessage.receivedMessage().getMessage();
final AckHandler ackHandler = outstandingMessage.ackHandler();
final SettableFuture<AckReply> response = SettableFuture.create();
final AckReplyConsumer consumer = new AckReplyConsumer() {
@Override
public void ack() {
response.set(AckReply.ACK);
}
@Override
public void nack() {
response.set(AckReply.NACK);
}
};
Futures.addCallback(response, ackHandler);
executor.submit(new Runnable() {
@Override
public void run() {
try {
receiver.receiveMessage(message, consumer);
} catch (Exception e) {
response.setException(e);
}
}
});
if (batchDone) {
batchCallback.run();
}
}
}
use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class ITPubSubTest method testPublishSubscribe.
@Test
public void testPublishSubscribe() throws Exception {
TopicName topicName = TopicName.create(projectId, formatForTest("testing-publish-subscribe-topic"));
SubscriptionName subscriptionName = SubscriptionName.create(projectId, formatForTest("testing-publish-subscribe-subscription"));
topicAdminClient.createTopic(topicName);
subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.newBuilder().build(), 10);
PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("my message")).build();
final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, new MessageReceiver() {
@Override
public void receiveMessage(final PubsubMessage message, final AckReplyConsumer consumer) {
if (received.set(message)) {
consumer.ack();
} else {
consumer.nack();
}
}
}).build();
subscriber.addListener(new Subscriber.Listener() {
public void failed(Subscriber.State from, Throwable failure) {
received.setException(failure);
}
}, MoreExecutors.directExecutor());
subscriber.startAsync();
Publisher publisher = Publisher.defaultBuilder(topicName).build();
publisher.publish(message).get();
publisher.shutdown();
assertEquals(received.get().getData(), message.getData());
subscriber.stopAsync().awaitTerminated();
subscriptionAdminClient.deleteSubscription(subscriptionName);
topicAdminClient.deleteTopic(topicName);
}
use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class ITPubSubSnippets method testPublisherSubscriberHelper.
private void testPublisherSubscriberHelper(TopicName topicName, SubscriptionName subscriptionName) throws Exception {
String messageToPublish = "my-message";
Publisher publisher = null;
try {
publisher = Publisher.defaultBuilder(topicName).build();
PublisherSnippets snippets = new PublisherSnippets(publisher);
final SettableApiFuture<Void> done = SettableApiFuture.create();
ApiFutures.addCallback(snippets.publish(messageToPublish), new ApiFutureCallback<String>() {
public void onSuccess(String messageId) {
done.set(null);
}
public void onFailure(Throwable t) {
done.setException(t);
}
});
done.get();
} finally {
if (publisher != null) {
publisher.shutdown();
}
}
final BlockingQueue<PubsubMessage> queue = new ArrayBlockingQueue<>(1);
final SettableApiFuture<Void> done = SettableApiFuture.create();
final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
SubscriberSnippets snippets = new SubscriberSnippets(subscriptionName, new MessageReceiverSnippets(queue).messageReceiver(), done, MoreExecutors.directExecutor());
new Thread(new Runnable() {
@Override
public void run() {
try {
received.set(queue.poll(10, TimeUnit.MINUTES));
} catch (InterruptedException e) {
received.set(null);
}
// signal the subscriber to clean up
done.set(null);
}
}).start();
// blocks until done is set
snippets.startAndWait();
PubsubMessage message = received.get();
assertNotNull(message);
assertEquals(message.getData().toStringUtf8(), messageToPublish);
}
use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class CreateSubscriptionAndConsumeMessages method main.
public static void main(String... args) throws Exception {
TopicName topic = TopicName.create("my-project-id", "my-topic-id");
SubscriptionName subscription = SubscriptionName.create("my-project-id", "my-topic-id");
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
}
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
System.out.println("Received message: " + message.getData().toStringUtf8());
consumer.ack();
}
};
Subscriber subscriber = null;
try {
subscriber = Subscriber.defaultBuilder(subscription, receiver).build();
subscriber.addListener(new Subscriber.Listener() {
@Override
public void failed(Subscriber.State from, Throwable failure) {
// Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
System.err.println(failure);
}
}, MoreExecutors.directExecutor());
subscriber.startAsync().awaitRunning();
Thread.sleep(60000);
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.
the class CreateTopicAndPublishMessages method publishMessages.
public static void publishMessages() throws Exception {
// [START pubsub_publish]
TopicName topicName = TopicName.create("my-project-id", "my-topic-id");
Publisher publisher = null;
List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.defaultBuilder(topicName).build();
List<String> messages = Arrays.asList("first message", "second message");
// schedule publishing one message at a time : messages get automatically batched
for (String message : messages) {
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
// Once published, returns a server-assigned message id (unique within the topic)
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
messageIdFutures.add(messageIdFuture);
}
} finally {
// wait on any pending publish requests.
List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
for (String messageId : messageIds) {
System.out.println("published with message ID: " + messageId);
}
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}
// [END pubsub_publish]
}
Aggregations