当前位置: 首页 > 知识库问答 >
问题:

如何使用Spring Boot Java配置编写自定义Apache Camel组件/endpoint

薛经纶
2023-03-14
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类。

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 {
    }
}

共有1个答案

宦宏爽
2023-03-14

经过大量搜索,我找到了一个基于Spring的BeanFactory的解决方案。主要障碍是循环依赖关系(组件->endpoint->组件endpoint->生产者->endpoint)。首先,我介绍了一个Spring配置类:

@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注释的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>
 类似资料:
  • 问题内容: 我目前正在开发的Android应用程序的主要活动已经变得非常大。这主要是因为它包含带有3个标签的。每个选项卡都有很多组件。活动必须一次控制所有这些组件。因此,我想您可以想象这个Activity有20个字段(几乎每个组件都有一个字段)。它还包含许多逻辑(单击侦听器,填充列表的逻辑等)。 我通常在基于组件的框架中所做的就是将所有内容拆分为自定义组件。每个自定义组件将承担明确的责任。它包含它

  • 本文向大家介绍MyBatis 如何编写一个自定义插件?相关面试题,主要包含被问及MyBatis 如何编写一个自定义插件?时的应答技巧和注意事项,需要的朋友参考一下 自定义插件实现原理: MyBatis 自定义插件针对 MyBatis 四大对象(Executor、StatementHandler、ParameterHandler、ResultSetHandler)进行拦截: Executor:拦截内

  • 问题内容: 我有一个django-allauth应用程序的django项目。我需要在注册时从用户那里收集其他数据。我在这里遇到了类似的问题, 但不幸的是,没有人回答个人资料定制部分。 根据提供的文档django-allauth: 指向自定义表单类(例如)的字符串,在注册过程中使用该字符串来询问用户其他输入(例如,简报注册,生日)。此类应实现一种方法,接受新注册的用户作为其唯一参数。 我是djang

  • 我试图用ffmpeg来编写mkv文件,用FFV1和FLAC编码,用NTSC格式,但是VLC和媒体信息中显示的帧率不正确。 下面是我如何创建和配置输出格式上下文:

  • 问题内容: 我正在将NHibernate与旧的rdbms规则引擎一起使用。我正在使用GenericDialect,但生成的某些sql无法正常工作。如果我需要为此规则引擎编写自定义方言,该如何开始? 问题答案: 这是一个方言示例: 它所在的程序集引用了NHibernate.dll hibernate.cfg.dll(请注意,我这里没有设置’connection.connection_string’属

  • 问题内容: 我创建了自定义的 Elasticsearch 插件。现在,我想为此插件编写一个测试。我的期望是-我可以运行嵌入式 Elasticsearch 实例,对其进行正确设置,然后进行一些测试(对一些文档进行索引,然后对其进行查询) 问题是我无法正确设置插件 自定义插件代码正在解析JSON查询并设置一些对象供以后使用: 测试代码: 我已经为 pathToPlugin 尝试了多个值-但似乎没有什么