代码之家  ›  专栏  ›  技术社区  ›  Sydney

CamelContextStartedEvent调用了两次

  •  1
  • Sydney  · 技术社区  · 7 年前

    这个 CamelContextStartedEvent 为同一个camel上下文(camel-1)调用两次。问题可能是我注册 EventNotifier 是的。可以使用Spring Initializer(带有Spring Boot 1.5.14、Spring Boot Camel Starter 2.21.1和Spring Boot Web Starter)重现此问题。

    查看日志:

    2018-07-06 11:04:41.104  INFO 19092 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.21.1 (CamelContext: camel-1) is starting
    2018-07-06 11:04:41.106  INFO 19092 --- [           main] o.a.c.m.ManagedManagementStrategy        : JMX is enabled
    2018-07-06 11:04:41.191  INFO 19092 --- [           main] o.a.camel.spring.SpringCamelContext      : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
    2018-07-06 11:04:41.193  INFO 19092 --- [           main] o.a.camel.spring.boot.RoutesCollector    : Starting CamelMainRunController to ensure the main thread keeps running
    2018-07-06 11:04:41.193  INFO 19092 --- [           main] o.a.camel.spring.SpringCamelContext      : Total 0 routes, of which 0 are started
    2018-07-06 11:04:41.194  INFO 19092 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.21.1 (CamelContext: camel-1) started in 0.090 seconds
    2018-07-06 11:04:41.195  INFO 19092 --- [           main] c.e.bug.service.StartupEventNotifier     : CamelContextStartedEvent for SpringCamelContext(camel-1) with spring id application:11223
    2018-07-06 11:04:41.195  INFO 19092 --- [           main] c.e.bug.service.StartupEventNotifier     : CamelContextStartedEvent for SpringCamelContext(camel-1) with spring id application:11223
    2018-07-06 11:04:41.216  INFO 19092 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 11223 (http)
    2018-07-06 11:04:41.221  INFO 19092 --- [           main] com.example.bug.BugApplication           : Started BugApplication in 4.684 seconds (JVM running for 6.773)
    

    初始化 事件通知程序 以下内容:

    @Service
    public class SchedulerService {
    
        private final CamelContext camelContext;
    
        private final StartupEventNotifier startupEventNotifier;
    
        public SchedulerService(CamelContext camelContext, StartupEventNotifier startupEventNotifier) {
            this.camelContext = camelContext;
            this.startupEventNotifier = startupEventNotifier;
        }
    
        @PostConstruct
        public void init() {
            camelContext.getManagementStrategy().addEventNotifier(startupEventNotifier);
        }
    
    }
    

    这个 事件通知程序 以下内容:

    @Component
    public class StartupEventNotifier extends EventNotifierSupport {
    
        private static final Logger logger = LoggerFactory.getLogger(StartupEventNotifier.class);
    
        @Override
        public void notify(EventObject event) throws Exception {
            if (event instanceof CamelContextStartedEvent) {
                logger.info("CamelContextStartedEvent for {}", event.getSource());
            }
        }
    
        @Override
        public boolean isEnabled(EventObject event) {
            if (event instanceof CamelContextStartedEvent) {
                return true;
            }
            return false;
        }
    }
    

    应用程序.yml:

    camel:
      springboot:
        main-run-controller: true
    server:
      port: 11223
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Bedla    7 年前

    它叫两次,因为它注册了两次。一次是你,一次是阿帕奇骆驼。 EventNotifier 自动注册,如果在 Registry 是的。自从你 StartupEventNotifier 注释为 Component ,它是 登记处 ApacheCamel在 CamelContext 启动 (你可以从中看到 CamelAutoConfiguration 424号线) 是的。

    你有四个选择:

    • 从中删除自定义注册 SchedulerService 是的。
    • 删除 @Component 注释来自 StartupEventNotifier启动事件通知程序 并向注册 camelContext.getManagementStrategy().addEventNotifier(new StartupEventNotifier())
    • 在您的 计划服务 是的。类似于:

      if (!context.getManagementStrategy().getEventNotifiers().contains(startupEventNotifier)){
          context.getManagementStrategy().addEventNotifier(startupEventNotifier);
      }
      
    • 寄存器 事件通知程序 在里面 @PostConstruct 属于 RouteBuilder .它将在启动自动发现之前注册,然后在中跳过 骆驼自动配置 (见第422行)