简体   繁体   中英

How to create spring bean which is “Kafka SINK” bean to refresh the context from config server

I hope to add a listener that allow me to pull messages from Kafka topic. Upon receiving message and filtering based on application name, context refresh request is sent to Spring Config Server, which refreshes properties from Spring Config Server.

However, my application is not spring boot application, and I found that almost every document I read need spring boot application to achieve that goal. Is there any possibility I can add such stream listener in Spring Bean without Spring boot Application?

I have created a bean KafkaListener. And also added annotation like @EnableBinding(Sink.class) . But the annotation I added throw java.lang.NoSuchMethodError , then block the running process, cannot reach the constructor part.

Here is my code

@EnableBinding(Sink.class)
public class ApiApplication {
......


@StreamListener(Sink.INPUT)
    public void configRefreshSink(RefreshRemoteApplicationEvent event) {
        log.info("ApiApplication.configRefreshSink() called");
        String applicationToRefreshContext = event.getDestinationService();
        if (applicationToRefreshContext.startsWith(this.applicationName)) {
            Set<String> keys = contextRefresher.refresh();
            log.info("Received remote refresh request. Keys refreshed: " + keys);
        }
        log.info("Received remote refresh request. Destination: " + event.getDestinationService());
    }
}

These are the errors I got

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org/springframework/util/Assert.notNull(Ljava/lang/Object;Ljava/util/function/Supplier;)V (loaded from file:xxxxx/repository/org/springframework/spring-core/4.3.22.RELEASE/spring-core-4.3.22.RELEASE.jar by
  1. The error

NoSuchMethodError: org/springframework/util/Assert.notNull(Ljava/lang/Object;Ljava/util/function/Supplier;)V

means that the version of Spring Framework is too old - that method was added in Spring Framework 5.0

  1. Spring Cloud Stream ( @StreamListener ) requires Spring Boot; it won't work without Boot.

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