Rid Hrant
Rid Hrant

Reputation: 152

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?

Upvotes: 4

Views: 21969

Answers (6)

Oskarro
Oskarro

Reputation: 403

I had the same problem. Manually initializing the beans didn't help, because each time another dependency was missing (PubSubTemplate, PubSubPublisherTemplate and PublisherFactory etc.).

My troubles started when I wanted to upgrade Spring from 2.7.10 to 3.0.9.

Make sure you have both dependencies defined inside dependencyManagement in pom.xml - if not, add them.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2022.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-dependencies</artifactId>
            <version>4.1.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Upvotes: 2

HackerMan0
HackerMan0

Reputation: 81

THE ANSWER IN ANOTHER THREAD THIS IS BASED ON

See the above for more detail, I won't bother to re-author it all as the poster there did a good job dictating the issue.

To raise out more specifically the solve, in my case this was an issue with using the org.springframework.cloud provided artifacts instead of the com.google.cloud ones. When I swapped to the latter the GcpPubSubAutoConfiguration kicked into gear as expected

I think whether this is a solution for you will matter what version of Spring/Spring Boot you use. See these blog posts which talk about the matter:

Upvotes: 0

AJW
AJW

Reputation: 21

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.

Upvotes: 1

Abhinav Atul
Abhinav Atul

Reputation: 629

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{

}

Upvotes: 0

Rid Hrant
Rid Hrant

Reputation: 152

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'
}

Upvotes: 0

Barath
Barath

Reputation: 5283

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>

Upvotes: 3

Related Questions