简体   繁体   中英

Spring Cloud Streams Conditional Forwarding of Data to Kafka Topics

I am trying to send Data to different topics based on some evaluation. I am using SPring CLoud Streams and Kafka

How can I conditionally forward to kafka topics. I need to insert SCS-kafka related code in the places where I commented specifically.

Thank you.

    @EnableBinding(Sink.class)
public class SampleSink {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SomeService someService;

    @ServiceActivator(inputChannel = Sink.INPUT)
    public void processor(Message<?> message1) {


        EvaluateData evaluateData = someService.evaluateData(message1);

        String Result = String.valueOf(evaluateData.getResult());

        try {

            if(validationResult.equalsIgnoreCase("allgood")){

                //Send message1 to Topic1


            }

            else if (validationResult.equalsIgnoreCase("notgood")){
 new SomeException("topic1");

//sent data to topic2

            }

            else {
                throw new SomeException("topic3");
            }

        }
        catch (SomeException e){

          //something

//sent data to topic2 and 3 respectively }

    }
}

What you are trying to do is essentially a router which we already provide as an out of the box application. It is basically a router Sink which will route message based on conditions you specify via configuration. Please see more here

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