[英]Pause message consumption from main kafka stream and start from other kafka topic
I am using @StreamListener (spring cloud stream) to consume messages from a topic ( input-channel ), do some processing and save into some cache or database.我正在使用 @StreamListener (spring cloud stream) 来消费来自一个主题 ( input-channel ) 的消息,做一些处理并保存到一些缓存或数据库中。
My requirement is, if DB goes down while processing the consumed message, I want to pause the main consumer(input-channel), and start consuming from another TOPIC (INPUT56-CHANNEL), and as soon as It consume all the message (doesn't have many) from INPUT56-CHANNEL, I want to resume the main consumer (input-channel) again.我的要求是,如果 DB 在处理消耗的消息时出现故障,我想暂停主消费者(输入通道),并从另一个 TOPIC(INPUT56-CHANNEL)开始消费,并且一旦它消耗了所有消息(不没有很多)来自 INPUT56-CHANNEL,我想再次恢复主要消费者(输入通道)。
Can that be achieved??可以实现吗??
@StreamListener
is deprecated; @StreamListener
已弃用; you should convert to the functional programming model instead.您应该改为转换为函数式编程模型。
Here is an example using that model (but the same techniques apply to the deprecated listeners).这是使用该模型的示例(但相同的技术适用于已弃用的侦听器)。
spring.cloud.function.definition=input1;input2
spring.cloud.stream.bindings.input1-in-0.group=grp1
spring.cloud.stream.bindings.input2-in-0.consumer.auto-startup=false
spring.cloud.stream.bindings.input2-in-0.group=grp2
spring.cloud.stream.kafka.bindings.input2-in-0.consumer.idle-event-interval=5000
@SpringBootApplication
public class So69726610Application {
public static void main(String[] args) {
SpringApplication.run(So69726610Application.class, args);
}
boolean dbIsDown = true;
@Autowired
BindingsLifecycleController controller;
TaskExecutor exec = new SimpleAsyncTaskExecutor();
@Bean
public Consumer<String> input1() {
return str -> {
System.out.println(str);
if (this.dbIsDown) {
this.controller.changeState("input1-in-0", State.PAUSED);
this.controller.changeState("input2-in-0", State.STARTED);
throw new RuntimeException("Paused");
}
};
}
@Bean
public Consumer<String> input2() {
return System.out::println;
}
@EventListener
public void idle(ListenerContainerIdleEvent event) {
System.out.println(event);
// assumes concurrency = 1 (default)
if (event.getListenerId().contains("input2-in-0")) {
this.controller.changeState("input1-in-0", State.RESUMED);
this.exec.execute(() -> this.controller.changeState("input2-in-0", State.STOPPED));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.