use of com.google.pubsub.v1.TopicName 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.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class ITSubscriptionAdminClientSnippets method createSubscription.
private Subscription createSubscription(String topicName, String subscriptionName) throws Exception {
createTopic(topicName);
Subscription subscription = subscriptionAdminClientSnippets.createSubscription(topicName, subscriptionName);
assertNotNull(subscription);
Subscription retrievedSubscription = subscriptionAdminClientSnippets.getSubscription(subscriptionName);
assertNotNull(retrievedSubscription);
assertEquals(subscription.getName(), retrievedSubscription.getName());
return subscription;
}
use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class ITTopicAdminClientSnippets method topicAddedIsSameAsRetrieved.
@Test
public void topicAddedIsSameAsRetrieved() throws Exception {
String topicName = topics[0];
Topic topicAdded = topicAdminClientSnippets.createTopic(topicName);
assertNotNull(topicAdded);
Topic topicRetrieved = topicAdminClientSnippets.getTopic(topicName);
assertEquals(topicAdded, topicRetrieved);
}
use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class ITTopicAdminClientSnippets method deletedTopicIsNotRetrievableAndThrowsException.
@Test(expected = ApiException.class)
public void deletedTopicIsNotRetrievableAndThrowsException() throws Exception {
String topicName = topics[0];
Topic topicAdded = topicAdminClientSnippets.createTopic(topicName);
assertNotNull(topicAdded);
TopicName formattedName = topicAdminClientSnippets.deleteTopic(topicName);
assertNotNull(formattedName);
topicAdminClientSnippets.getTopic(topicName);
}
use of com.google.pubsub.v1.TopicName 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();
}
}
}
Aggregations