Search code examples
spring-cloud-streamspring-cloud-dataflow

PollableMessageSource issue with spring-cloud-stream 4.0.0 and Spring boot 3


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


Solution

  • 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>