spring - 如何使用 Spring Boot Java Config 编写自定义 Apache Camel 组件/端点
问题描述
我正在寻找有关如何在 Java Config中实现自定义 Apache Camelcomponent
和Spring Boot 的示例/文档。endpoint
我不知道我必须如何注释这些类,请你提供一个例子。在不使用 Spring 的情况下,我的(工作)代码如下所示:
public class MyCustomComponent extends DefaultComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
Endpoint endpoint = new MyCustomEndpoint(uri, this);
setProperties(endpoint, parameters);
return endpoint;
}
}
Registered 是类resources/META-INF/services/org/apache/camel/component/myscheme
的 FQN 中的组件MyCustomComponent
。
public class MyCustomEndpoint extends DefaultEndpoint {
public MyCustomEndpoint(String uri, MyCustomComponent component) {
super(uri, component);
}
@Override
public Producer createProducer() throws Exception {
return new MyCustomProducer(this);
}
@Override
public Consumer createConsumer(Processor processor) throws Exception {
throw new UnsupportedOperationException("Not implemented yet: MyCustomEndpoint#createConsumer");
}
@Override
public boolean isSingleton() {
return false;
}
}
public class MyCustomProducer extends DefaultProducer {
public MyCustomProducer(Endpoint endpoint) {
super(endpoint);
}
@Override
public void process(Exchange exchange) throws Exception {
}
}
解决方案
经过大量搜索,我找到了基于 Spring 的BeanFactory
. 主要障碍是循环依赖(组件 -> 端点 -> 组件 | 端点 -> 生产者 -> 端点)。一开始我介绍了一个 SpringConfiguration
类:
@Configuration
public class ComponentConfiguration {
@Bean("myCustomEndpoint")
@Scope("prototype")
public MyCustomEndpoint myCustomEndpoint(String uri, MyCustomComponent component) {
MyCustomEndpoint endpoint = new MyCustomEndpoint(uri, component);
return endpoint;
}
@Bean("myCustomProducer")
@Scope("prototype")
public MyCustomProducer myCustomProducer(MyCustomEndpoint endpoint) {
return new MyCustomProducer(endpoint);
}
}
使自定义 Camel 组件成为带有@Component
注释的 Spring 组件,因此我可以注入以按需(范围)BeanFactory
创建类的实例。MyCustomEndpoint
prototype
@org.springframework.stereotype.Component
public class MyCustomComponent extends DefaultComponent {
@Autowired
private BeanFactory beanFactory;
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
MyCustomEndpoint endpoint = (MyCustomEndpoint) beanFactory.getBean("myCustomEndpoint", uri, this);
setProperties(endpoint, parameters);
return endpoint;
}
}
public class MyCustomEndpoint extends DefaultEndpoint {
@Autowired
private BeanFactory beanFactory;
public MyCustomEndpoint(String uri, MyCustomComponent component) {
super(uri, component);
}
@Override
public Producer createProducer() throws Exception {
MyCustomProducer producer = (MyCustomProducer) beanFactory.getBean("myCustomProducer", this);
return producer;
}
@Override
public Consumer createConsumer(Processor processor) throws Exception {
throw new UnsupportedOperationException("Not implemented yet: MyCustomEndpoint#createConsumer");
}
@Override
public boolean isSingleton() {
return true;
}
}
public class MyCustomProducer extends DefaultProducer {
// Now, I am able to inject some other Spring beans
@Autowired
private AnotherSpringBean bean;
public MyCustomProducer(Endpoint endpoint) {
super(endpoint);
}
@Override
public void process(Exchange exchange) throws Exception {
}
}
请注意,Camel 组件MyCustomComponent
已在以下位置注册resources/META-INF/services/org/apache/camel/component/myscheme
:
class=<fqn-of-MyCustomComponent>
推荐阅读
- javascript - 如果另一个 div 包含某些单词,则更改按钮上的文本
- python - Python:并非所有参数都在 SQL 语句中使用
- azure - 无法连接到 AAS - Azure 分析服务器的实际群集终结点解析失败
- linux - 删除查找结果中的文件
- android-recyclerview - RecyclerView 在底部片断中不可见
- flutter - Mobx 颤振 observable 没有更新值。每次我得到默认值
- flutter - Flutter如何禁用横向?
- dotnetnuke - 哪个版本的2sxc内容模块稳定,源码中引用正确,可以在本地部署启动?
- java - 对 C++ DLL 的连续 JNI 调用之间的 DB2 连接中断
- python - 根据条件从两个数据框中连接和选择行