0

I'm trying to implement a polled consumer using spring-cloud-stream 4.0.0 following this example https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#spring-cloud-streams-overview-using-polled-consumers.

Here is my code:

application.properties

spring.cloud.stream.pollable-source=myDestination
@Slf4j
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ApplicationRunner poller(PollableMessageSource destIn, MessageChannel destOut) {
        return args -> {
            log.info("polling");
        };
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2022.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.1</version>
        <relativePath />
    </parent>

   <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
    </dependencies>
</project>

When I run it I get error Parameter 0 of method poller required a bean of type 'org.springframework.cloud.stream.binder.PollableMessageSource' that could not be found.. Any idea?

I have the same code implemented with older version of spring-cloud-stream using @EnableBinding(PolledProcessor.class) and it works

user3908406
  • 1,416
  • 1
  • 18
  • 32

1 Answers1

1

I am not sure as i can not see the entirety of your application, but here is a self-contained working example:

@SpringBootApplication
public class PollableSourceApplication {

    public static void main(String[] args) {
        SpringApplication.run(PollableSourceApplication.class, "--spring.cloud.stream.pollable-source=myDestination");
    }
    @Bean
    public ApplicationRunner poller(PollableMessageSource destIn) {
        return args -> {
            while (true) {
                try {
                    if (!destIn.poll(m -> {
                        String newPayload = (new String((byte[]) m.getPayload())).toUpperCase();
                        System.out.println("PAYLOAD: " + newPayload);
                    })) {
                        Thread.sleep(1000);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }
}

EDIT: Looking at your example. .
First you are missing a binder (e.g., rabbit, kafka etc). Second, there was a bug in 4.0.0 so you have to use the lates snapshot 4.0.1-SNAPSHOT. We will be releasing it before the end of the week so you'll have 2022.0.1 shortly but for now here is the entire dependency section you'd need for your sample to work

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
            <version>4.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
Oleg Zhurakousky
  • 5,820
  • 16
  • 17
  • Thanks, I just tried your example but still got the same error. I just posted my pom.xml. Can you check if you are using the same dependencies ? – user3908406 Jan 18 '23 at 15:05
  • Looks good. I suggest create a small app that reproduces the issue and push it to github and post a link here so we can take a look. Something must be missing – Oleg Zhurakousky Jan 18 '23 at 15:15
  • Thanks. I uploaded a simple project here https://github.com/lz000/polledconsumer-test. Just load the project in inteliJ as maven project and then run the `Application.java` using JDK17 – user3908406 Jan 18 '23 at 15:35
  • I just found out maybe the issue is here in the `spring-cloud-stream` project: https://github.com/spring-cloud/spring-cloud-stream/blob/main/core/spring-cloud-stream/src/main/resources/META-INF/spring.factories#L5. Spring boot 3 dropped supported for `spring.factories` – user3908406 Jan 18 '23 at 15:45
  • Just edited my response – Oleg Zhurakousky Jan 18 '23 at 16:15