3

I am looking for a way to stop consume messages with stream listener.

@StreamListener(MBinding.M_INPUT)
    public void consumeMessage(Message<MerchantEvent> message) {
    //handle when receive message
 }

cloud:
        stream:
            bindings:
                MInput:
                    destination: topicName
                    group: groupName

I have googled it but right now still have no idea how to stop consuming. Is there anyone who know it?

Park Jay
  • 249
  • 6
  • 14

1 Answers1

8

You can do it using the actuator (see Binding Visualization and Control). Or you can invoke the endpoint programmatically.

@SpringBootApplication
@EnableBinding(Sink.class)
public class So58795176Application {

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

    @StreamListener(Sink.INPUT)
    public void listen(String in) {
        System.out.println();
    }

    @Autowired
    BindingsEndpoint endpoint;

    @Bean
    public ApplicationRunner runner() {
        return args -> {
            System.in.read();
            endpoint.changeState("input", State.STOPPED);
            System.in.read();
            endpoint.changeState("input", State.STARTED);
        };
    }

}
Shubham
  • 109
  • 14
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I have checked the binding endpoint, when I changed the binding state to PAUSE the consumer still consume message itself, so right now I confused of binding meaning. I am just look at kafka a few day so not clear about it. Could you please let me know what binding in this case mean? – Park Jay Nov 11 '19 at 14:23
  • The pause/stop won't take effect until the records retrieved by the previous `poll()` have been processed. You can set `max.poll.records=1` if you want to pause/stop immediately. – Gary Russell Nov 11 '19 at 14:40
  • the previous message already processed, max.poll.records set to 1, then I execute the binding endpoint, the state changed to PAUSED, but the consumer still consume message – Park Jay Nov 12 '19 at 02:46
  • I make a mistake on checking wrong class, The bind enpoint is working well, Thank you so much :D – Park Jay Nov 12 '19 at 09:29
  • @GaryRussell State.STOPPED state is private. How to use it? Secondly, do we need to switch on the actuator to make the above code work? – Chandresh Mishra Sep 07 '20 at 13:33
  • It was made public in 2.1 - see https://github.com/spring-cloud/spring-cloud-stream/issues/1540 Yes; it currently needs actuator - there is an open issue to decouple this functionality from actuator - https://github.com/spring-cloud/spring-cloud-stream/issues/1907 – Gary Russell Sep 07 '20 at 13:50
  • Thanks for the reply. Sorry, it would be a new question, Could you please give me a pointer to do this without actuator. I wanted to read messages from dlq using controller endpoint and stop it after all the messages been read. – Chandresh Mishra Sep 07 '20 at 14:03
  • @GaryRussell Could you please have a look on this.https://stackoverflow.com/questions/63779394/programmatic-way-to-read-message-using-spring-cloud-stream-kafka – Chandresh Mishra Sep 07 '20 at 14:19
  • You don’t need to enable web access to the actuator but you do need the actuator starter in the class path. – Gary Russell Sep 07 '20 at 14:21