[英]Adding Apache Camel custom component/endpoint in a Spring application
[英]How to write a custom Apache Camel component/endpoint with Spring Boot Java Config
我正在寻找有关如何在 Java Config 中使用 Spring Boot 实现自定义 Apache Camel component
和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;
}
}
使用MyCustomComponent
类的 FQN 注册的是resources/META-INF/services/org/apache/camel/component/myscheme
中的resources/META-INF/services/org/apache/camel/component/myscheme
。
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
的解决方案。 主要障碍是循环依赖(组件 -> 端点 -> 组件 | 端点 -> 生产者 -> 端点)。 首先我引入了一个 Spring Configuration
类:
@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);
}
}
使用@Component
注释使自定义 Camel 组件成为 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.