Как написать пользовательский Apache компонент / конечную точку Camel с помощью Spring Boot Java Config - PullRequest
0 голосов
/ 03 февраля 2020

Я ищу пример / документацию о том, как реализовать пользовательские Apache Camel component и endpoint с Spring Boot в Java Config. Я не знаю, как я должен аннотировать классы, которые могли бы вы привести пример. Без использования 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 класса.

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 Ответ

0 голосов
/ 04 февраля 2020

После долгих поисков я нашел решение на основе 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);
    }
}

. Сделайте пользовательский компонент Camel компонентом Spring с аннотацией @Component, поэтому я могу добавить 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>
...