Search in sources :

Example 6 with PubSubTemplate

use of org.springframework.cloud.gcp.pubsub.core.PubSubTemplate 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");
    });
}
Also used : ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) Test(org.junit.Test)

Example 7 with PubSubTemplate

use of org.springframework.cloud.gcp.pubsub.core.PubSubTemplate in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateDocumentationTests method pubSubTest.

private void pubSubTest(PubSubTest pubSubTest, Class... configClass) {
    ApplicationContextRunner contextRunner = configClass.length == 0 ? this.contextRunner : this.contextRunner.withUserConfiguration(configClass[0]);
    contextRunner.run((context) -> {
        PubSubAdmin pubSubAdmin = context.getBean(PubSubAdmin.class);
        PubSubTemplate pubSubTemplate = context.getBean(PubSubTemplate.class);
        String subscriptionName = "test_subscription_" + UUID.randomUUID();
        String topicName = "test_topic_" + UUID.randomUUID();
        try {
            assertThat(pubSubAdmin.getTopic(topicName)).isNull();
            assertThat(pubSubAdmin.getSubscription(subscriptionName)).isNull();
            // tag::create_topic[]
            pubSubAdmin.createTopic(topicName);
            // end::create_topic[]
            // tag::create_subscription[]
            pubSubAdmin.createSubscription(subscriptionName, topicName);
            // end::create_subscription[]
            pubSubTest.run(pubSubTemplate, subscriptionName, topicName);
        } finally {
            // tag::list_subscriptions[]
            List<String> subscriptions = pubSubAdmin.listSubscriptions().stream().map(Subscription::getName).collect(Collectors.toList());
            // end::list_subscriptions[]
            // tag::list_topics[]
            List<String> topics = pubSubAdmin.listTopics().stream().map(Topic::getName).collect(Collectors.toList());
            // end::list_topics[]
            pubSubAdmin.deleteSubscription(subscriptionName);
            pubSubAdmin.deleteTopic(topicName);
            assertThat(subscriptions.stream().map(this::getLastPart)).contains(subscriptionName);
            assertThat(topics.stream().map(this::getLastPart)).contains(topicName);
        }
    });
}
Also used : ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) PubSubAdmin(org.springframework.cloud.gcp.pubsub.PubSubAdmin)

Aggregations

PubSubTemplate (org.springframework.cloud.gcp.pubsub.core.PubSubTemplate)7 ByteString (com.google.protobuf.ByteString)6 Test (org.junit.Test)6 PubSubAdmin (org.springframework.cloud.gcp.pubsub.PubSubAdmin)4 ConvertedAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage)4 AcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage)3 PubsubMessage (com.google.pubsub.v1.PubsubMessage)2 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CompositeHealthContributor (org.springframework.boot.actuate.health.CompositeHealthContributor)1 ApplicationContextRunner (org.springframework.boot.test.context.runner.ApplicationContextRunner)1