如何使用 Spring Boot Java 配置编写自定义 Apache Camel component/endpoint
How to write a custom Apache Camel component/endpoint with Spring Boot Java Config
我正在寻找有关如何在 Java 配置中使用 Spring 引导实现自定义 Apache Camel component
和 endpoint
的 example/documentation。我不知道如何注释 classes,请您提供一个示例。
不使用 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;
}
}
已注册 resources/META-INF/services/org/apache/camel/component/myscheme
中的组件,其 FQN 为 MyCustomComponent
class。
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
class:
@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
class 按需(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>
我正在寻找有关如何在 Java 配置中使用 Spring 引导实现自定义 Apache Camel component
和 endpoint
的 example/documentation。我不知道如何注释 classes,请您提供一个示例。
不使用 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;
}
}
已注册 resources/META-INF/services/org/apache/camel/component/myscheme
中的组件,其 FQN 为 MyCustomComponent
class。
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
class:
@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
class 按需(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>