1

CamelContextStartedEvent为相同的骆驼上下文(camel-1)调用了两次。问题可能是我注册EventNotifier. 您可以使用 Spring Boot 1.5.14、Spring Boot Camel Starter 2.21.1 和 Spring Boot Web Starter 重现 Spring Initializr 的问题。

查看日志:

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)

初始化的服务EventNotifier

@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);
    }

}

EventNotifier: _

@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
4

1 回答 1

4

它被调用了两次,因为它被注册了两次。一次由您,一次由 Apache Camel。EventNotifier自动注册,如果在 中找到Registry。由于您StartupEventNotifier的注释为Component,因此它是RegistryApache Camel 在CamelContext启动期间注册它的一部分(您可以在第 424 行看到它CamelAutoConfiguration) 。

您有四个选择:

  • 从 中删除您的自定义注册SchedulerService
  • 删除@Component注释StartupEventNotifier并将其注册到camelContext.getManagementStrategy().addEventNotifier(new StartupEventNotifier())
  • 将重复性检查添加到您的SchedulerService. 就像是:

    if (!context.getManagementStrategy().getEventNotifiers().contains(startupEventNotifier)){
        context.getManagementStrategy().addEventNotifier(startupEventNotifier);
    }
    
  • EventNotifier@PostConstruct中注册RouteBuilder。它会在自动发现开始之前注册,然后会被跳过(见第422行)CamelAutoConfiguration

于 2018-07-06T14:15:40.130 回答