Search code examples
spring-bootspring-integrationspring-cloud-streamspring-amqpspring-rabbit

Spring Cloud Stream - Consume Data on Demand manually?


Using Spring Data Stream, How can I start reading from a queue and stop reading on demand?

I want to so something like this:

@EnableBinding(Sink.class)
public class SomeConsumer {

  @StreamListener(target = Sink.INPUT)
  public void receiveMsg(Message<String> message)
  {
    logger.info(" received new message [" + message.toString() + "] ");
  }

  public static void startReceiving()
  {
     //How to implement this logic?
  }

  public static void stopReceiving()
  {
     //How to implement this logic?
  }

}

Solution

  • It can't be done in a static method; autowire the BindingsEndpoint and use the changeState() method.

    See my answer to this question.