Spring cloud stream kafka pause/resume binders

前端 未结 1 1280
感情败类
感情败类 2021-01-22 14:09

We are using spring cloude stream 2.0 & Kafka as a message broker.
We\'ve implemented a circuit breaker which stops the Application context, for cases w

1条回答
  •  北海茫月
    2021-01-22 14:50

    Sorry, I misread your question.

    You can auto wire the BindingsEndpoint but, unfortunately, its State enum is private so you can't call changeState() programmatically.

    I have opened an issue for this.

    EDIT

    You can do it with reflection, but it's a bit ugly...

    @SpringBootApplication
    @EnableBinding(Sink.class)
    public class So53476384Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So53476384Application.class, args);
        }
    
        @Autowired
        BindingsEndpoint binding;
    
        @Bean
        public ApplicationRunner runner() {
            return args -> {
                Class clazz = ClassUtils.forName("org.springframework.cloud.stream.endpoint.BindingsEndpoint$State",
                        So53476384Application.class.getClassLoader());
                ReflectionUtils.doWithMethods(BindingsEndpoint.class, method -> {
                    try {
                        method.invoke(this.binding, "input", clazz.getEnumConstants()[2]); // PAUSE
                    }
                    catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }, method -> method.getName().equals("changeState"));
            };
        }
    
        @StreamListener(Sink.INPUT)
        public void listen(String in) {
    
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题