首页 > 解决方案 > 如何使用 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 {
    }
}

标签: springapache-camelspring-camel

解决方案


经过大量搜索,我找到了基于 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创建类的实例。MyCustomEndpointprototype

@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>

推荐阅读