'Problem with Google Cloud Pub/Sub API and Spring boot application

I write spring boot application for subscribing Google cloud Pub/Sub topic for this I use Google's tutorial, but when I run application I have get this error

2019-02-02 18:03:10.248  INFO 15080 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-02-02 18:03:10.271  INFO 15080 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-02 18:03:10.610 ERROR 15080 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of method messageChannelAdapter in tech.garoon.cloud.CloudApplication required a bean of type 'org.springframework.cloud.gcp.pubsub.core.PubSubTemplate' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.cloud.gcp.pubsub.core.PubSubTemplate' in your configuration.


Process finished with exit code 1

How Can I solve this problem?



Solution 1:[1]

GcpPubSubAutoConfiguration provides autoconfiguration feature of creating necessary beans including PubSubTemplate. In your case, somethng is missed, Kindly ensure that dependencies are in place or recreate following bean to make it work.

    @Bean
    public PubSubTemplate pubSubTemplate(PubSubPublisherTemplate pubSubPublisherTemplate,
            PubSubSubscriberTemplate pubSubSubscriberTemplate) {
        return new PubSubTemplate(pubSubPublisherTemplate, pubSubSubscriberTemplate);
    }

Additionally, make sure GcpContextAutoConfiguration is created based on below properties in application.properties.

spring.cloud.gcp.credentials.location=${gcp_credentials}

starter dependency:

      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
        </dependency>

Solution 2:[2]

Solution

I added this dependency

implementation 'org.springframework.cloud:spring-cloud-gcp-autoconfigure:1.1.0.RELEASE'

My dependencies

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-gcp-pubsub:1.1.0.RELEASE'
    implementation 'org.springframework.cloud:spring-cloud-gcp-autoconfigure:1.1.0.RELEASE'
    implementation "org.springframework.boot:spring-boot-starter-web:2.1.2.RELEASE"
    implementation 'org.springframework.integration:spring-integration-core:5.1.2.RELEASE'
}

Solution 3:[3]

if using external configuration class that is registering your channels, message handlers etc, make sure to annotate the configuration class with @Import({GcpPubSubAutoConfiguration.class})

@Configuration
@Import({GcpPubSubAutoConfiguration.class})
public class PubSubConfig{

}

Solution 4:[4]

I ran into this issue with these versions of spring-boot and spring-cloud-gcp-starter-pub:

            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>

and in my application.properties I had spring.cloud.gcp.pubsub.enabled=false for local development. I removed spring.cloud.gcp.pubsub.enabled=false and it worked. Although now it creates a connection to the pubsub gcp topic, so for local development, you will need to comment out publishing, to avoid sending messages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Rid Hrant
Solution 3 Abhinav Atul
Solution 4 AJW