简体   繁体   中英

spring cloud stream supplier and consumer not triggered

i'm using the functional programming model of spring cloud stream to declare the supplier and the consumer.
i need my publisher to be triggered every second then the message to be processed by the processor and finally to be consumed by the consumer. but these functions are never triggered. are there any missing configuration?

Here is my code:-

spring boot App Main class

import java.util.Date;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Example1 {

    public static void main(String[] args) {

        Properties properties = new Properties();

        properties.put("spring.cloud.stream.bindings.myPublisher-out-0.destination", "myProcessor-in");
        properties.put("spring.cloud.stream.bindings.myProcessor-in-0.destination", "myProcessor-in");
        properties.put("spring.cloud.stream.bindings.myProcessor-out-0.destination", "myProcessor-out");
        properties.put("spring.cloud.stream.bindings.mySubscriber-in-0.destination", "myProcessor-out");

        SpringApplication app = new SpringApplication(Example1.class);
        app.setDefaultProperties(properties);
        app.run(args);

    }

    @Bean
    public Supplier<String> myPublisher() {
        return () -> new Date().toString();
    }

    @Bean
    public Function<String, String> myProcessor() {
        return s -> "ML PROCESSED: " + s;
    }

    @Bean
    public Consumer<String> mySubscriber() {
        return s -> System.out.println("ML RECEIVED: " + s);
    }
}

pom.xml file

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.javaworld</groupId>
    <artifactId>code-snippet</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <spring-cloud.version>2021.0.2</spring-cloud.version>
    </properties>
    
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <!-- 
            <scope>test</scope>
             -->
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
         
        <!-- C3P0 connection pool dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.1</version>
        </dependency>

        <!-- Hikari connection pool dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-hikaricp</artifactId>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>

        <!-- actuators support -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>io.dropwizard.metrics</groupId>
            <artifactId>metrics-core</artifactId>
        </dependency>

        <!-- ################################ JMS using activemq support ######################################## -->
        <!--  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        
        <!-- needed when running JMS without any framework..specifically the 
        examples in this package spring_boot._4_jms.no_framework  -->
        
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-kahadb-store</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <!--  the above spring-boot-starter-activemq replaces all these following dependencies -->
        
        <!-- 
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
        </dependency>
        -->
        
        <!-- ################################ AMQP support ######################################## -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!-- ################################ Wiremock support #################################### -->
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock-jre8</artifactId>
            <version>2.32.0</version>
            <scope>test</scope>
        </dependency>
        
        <!-- ################################ Spring cloud stream ###################################### -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
            
    </dependencies>
    
    <!-- for spring cloud stream support -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

You should define your Function , Supplier and Consumer for spring-cloud-stream as below.

properties.put("spring.cloud.stream.function.definition", "myPublisher;myProcessor;mySubscriber");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM