Syd*_*ney 1 spring apache-camel spring-boot spring-camel
CamelContextStartedEvent对于相同的骆驼上下文 (camel-1) 被调用两次。问题可能是我注册的方式EventNotifier。您可以使用 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)
Run Code Online (Sandbox Code Playgroud)
初始化的服务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);
}
}
Run Code Online (Sandbox Code Playgroud)
这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;
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序.yml:
camel:
springboot:
main-run-controller: true
server:
port: 11223
Run Code Online (Sandbox Code Playgroud)
它被调用两次,因为它被注册了两次。一次是你,一次是 Apache Camel。EventNotifier如果在 中找到,则自动注册Registry。由于您的StartupEventNotifier注释为,它是Apache Camel 的Component一部分,并且在启动期间注册了它(您可以在第 424 行中看到它)。RegistryCamelContextCamelAutoConfiguration
您有四个选择:
SchedulerService。@Component注释StartupEventNotifier并将其注册为camelContext.getManagementStrategy().addEventNotifier(new StartupEventNotifier())添加口是心非检查到您的SchedulerService. 就像是:
if (!context.getManagementStrategy().getEventNotifiers().contains(startupEventNotifier)){
context.getManagementStrategy().addEventNotifier(startupEventNotifier);
}
Run Code Online (Sandbox Code Playgroud)注册EventNotifier于.@PostConstruct RouteBuilder它将在自动发现开始之前注册,然后将被跳过(参见第422行)CamelAutoConfiguration
| 归档时间: |
|
| 查看次数: |
1246 次 |
| 最近记录: |