如果您只想更改默认 CamelContext
bean 的名称,您可以使用 Camel CDI 提供的 @ContextName
qualifier,例如:
@ContextName("camel-context")
class MyRouteBean extends RouteBuilder {
@Override
public void configure() {
from("jms:invoices").to("file:/invoices");
}
}
@ContextName("camel-context")
class MyRouteBean extends RouteBuilder {
@Override
public void configure() {
from("jms:invoices").to("file:/invoices");
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
否则,如果需要更多自定义,都可以使用任何 CamelContext
类来声明自定义 Camel 上下文 Bean。然后,可以执行 @PostConstruct
和 @PreDestroy
生命周期回调来进行自定义,例如:
@ApplicationScoped
class CustomCamelContext extends DefaultCamelContext {
@PostConstruct
void customize() {
// Set the Camel context name
setName("custom");
// Disable JMX
disableJMX();
}
@PreDestroy
void cleanUp() {
// ...
}
}
@ApplicationScoped
class CustomCamelContext extends DefaultCamelContext {
@PostConstruct
void customize() {
// Set the Camel context name
setName("custom");
// Disable JMX
disableJMX();
}
@PreDestroy
void cleanUp() {
// ...
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
生产者 和发布者方法也可用于自定义 Camel 上下文 Bean,例如: http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#disposer_method
class CamelContextFactory {
@Produces
@ApplicationScoped
CamelContext customize() {
DefaultCamelContext context = new DefaultCamelContext();
context.setName("custom");
return context;
}
void cleanUp(@Disposes CamelContext context) {
// ...
}
}
class CamelContextFactory {
@Produces
@ApplicationScoped
CamelContext customize() {
DefaultCamelContext context = new DefaultCamelContext();
context.setName("custom");
return context;
}
void cleanUp(@Disposes CamelContext context) {
// ...
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
同样,可以使用 producer 字段,例如:
@Produces
@ApplicationScoped
CamelContext context = new CustomCamelContext();
class CustomCamelContext extends DefaultCamelContext {
CustomCamelContext() {
setName("custom");
}
}
@Produces
@ApplicationScoped
CamelContext context = new CustomCamelContext();
class CustomCamelContext extends DefaultCamelContext {
CustomCamelContext() {
setName("custom");
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
例如,可以通过调用 setAutoStartup
方法来避免在容器初始化时自动启动 Camel 上下文路由,例如:
@ApplicationScoped
class ManualStartupCamelContext extends DefaultCamelContext {
@PostConstruct
void manual() {
setAutoStartup(false);
}
}
@ApplicationScoped
class ManualStartupCamelContext extends DefaultCamelContext {
@PostConstruct
void manual() {
setAutoStartup(false);
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow