use of com.google.cloud.pubsub.v1.Subscriber 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.cloud.pubsub.v1.Subscriber 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.cloud.pubsub.v1.Subscriber in project google-cloud-java by GoogleCloudPlatform.
the class SubscriberSnippets method startAndWait.
// [TARGET startAsync()]
public void startAndWait() throws Exception {
Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build();
subscriber.addListener(new Subscriber.Listener() {
public void failed(Subscriber.State from, Throwable failure) {
// Handle error.
}
}, executor);
subscriber.startAsync();
// Wait for a stop signal.
done.get();
subscriber.stopAsync().awaitTerminated();
}
use of com.google.cloud.pubsub.v1.Subscriber in project google-cloud-java by GoogleCloudPlatform.
the class SubscriberSnippets method createSubscriberWithCustomFlowSettings.
private Subscriber createSubscriberWithCustomFlowSettings() throws Exception {
// [START pubsub_subscriber_flow_settings]
int maxMessageCount = 10;
// Configure max number of messages to be pulled
FlowControlSettings flowControlSettings = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(maxMessageCount).build();
Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).setFlowControlSettings(flowControlSettings).build();
// [END pubsub_subscriber_flow_settings]
return subscriber;
}
use of com.google.cloud.pubsub.v1.Subscriber in project spring-cloud-gcp by spring-cloud.
the class DefaultSubscriberFactoryTests method testNewSubscriber.
@Test
public void testNewSubscriber() {
DefaultSubscriberFactory factory = new DefaultSubscriberFactory(() -> "angeldust");
factory.setCredentialsProvider(this.credentialsProvider);
Subscriber subscriber = factory.createSubscriber("midnight cowboy", (message, consumer) -> {
});
assertThat(subscriber.getSubscriptionNameString()).isEqualTo("projects/angeldust/subscriptions/midnight cowboy");
}
Aggregations