【问题标题】:Custom camel component using in spring-boot application在 spring-boot 应用程序中使用的自定义骆驼组件
【发布时间】:2019-05-19 17:44:05
【问题描述】:

我是 Camel 集成的初学者,我需要创建我的自定义骆驼组件并在 Spring Boot 应用程序中使用它。

我尝试使用 maven archetype 生成我的组件。

所以命令是这样的:

mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-component -DarchetypeVersion=2.12.1 -DgroupId=my.tcp.camel.component -DartifactId=my-tcp -Dname=MyTCP -Dscheme=my-tcp

生成的代码是这样的

public class MyTCPComponent extends DefaultComponent {

    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        Endpoint endpoint = new MyTCPEndpoint(uri, this);
        setProperties(endpoint, parameters);
        return endpoint;
    }
}

public class MyTCPEndpoint extends DefaultEndpoint {

    public MyTCPEndpoint() {}

    public MyTCPEndpoint(String uri, PtTCPComponent component) {
        super(uri, component);
    }

    public MyTCPEndpoint(String endpointUri) {
        super(endpointUri);
    }

    public Producer createProducer() throws Exception {
        return new MyTCPProducer(this);
    }

    public Consumer createConsumer(Processor processor) throws Exception {
        return new MyTCPConsumer(this, processor);
    }

    public boolean isSingleton() {
        return true;
    }
}

public class MyTCPConsumer extends ScheduledPollConsumer {
    private final MyTCPEndpoint endpoint;

    public MyTCPConsumer(MyTCPEndpoint endpoint, Processor processor) {
        super(endpoint, processor);
        this.endpoint = endpoint;
    }

    @Override
    protected int poll() throws Exception {
        Exchange exchange = endpoint.createExchange();

        // create a message body
        Date now = new Date();
        exchange.getIn().setBody("Hello World! The time is " + now);

        try {
            // send message to next processor in the route
            getProcessor().process(exchange);
            return 1; // number of messages polled
        } finally {
            // log exception if an exception occurred and was not handled
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
            }
        }
    }
}

public class MyTCPProducer extends DefaultProducer {
    private static final Logger LOG = LoggerFactory.getLogger(MyTCPProducer.class);
    private MyTCPEndpoint endpoint;

    public MyTCPProducer(MyTCPEndpoint endpoint) {
        super(endpoint);
        this.endpoint = endpoint;
    }

    public void process(Exchange exchange) throws Exception {
        System.out.println(exchange.getIn().getBody());    
    }

}

以及在资源中创建的清单文件。

我发现你可以用 FatJar 初始化 springBoot

@SpringBootApplication
public class MySpringBootRouter extends FatJarRouter {

    @Override
    public void configure() {
        from("timer://trigger").
                transform().simple("ref:myBean").
                to("log:out", "mock:test");
    }

    @Bean
    String myBean() {
        return "I'm Spring bean!";
    }

}

有人在 SpringBoot 应用程序中集成了他们的自定义组件。

我更愿意让 springboot 与骆驼自动发现组件一起工作。

谢谢。

【问题讨论】:

  • 你的组件是做什么的?你真的需要你自己的组件来做这些事情吗? IE。对于 TCP 连接,您可以使用 Camel 的 mina-component,您可以按照 here 的说明在您自己的 Spring 组件中定义它@
  • 你需要使用这么旧的 Camel 版本 - 2.12 吗?你使用什么版本的 Spring Boot?我建议使用较新版本的骆驼。并且不要使用 FarJarRouter,而只是使用标准的 Spring Boot 和 Camel。查看所有使用 Camel 和 Spring Boot 的示例:github.com/apache/camel/tree/master/examples#examples
  • @RomanVottner 我想隔离一些单独的逻辑,它不在 mina-component 中,也不在 netty4-component 中。
  • @ClausIbsen,谢谢。我认为我的解决方案将类似于此示例。 camel-example-spring-boot/。我将在哪里命名 bean,因为我打算命名骆驼组件(即 @Component("my-tcp") public class MyBean { }
  • @ClausIbsen 使用 camel-spring-boot 访问路由的方式如下:` from("my-tcp://foo") .to ("my-tcp://bar")` ?

标签: spring-boot apache-camel


【解决方案1】:

问题是我试图在骆驼弹簧靴中添加一个自定义骆驼组件

我决定仅将其用作参考 bean 而不是组件。

  @Component("my-tcp")
    @Slf4j
    public class MyTCPComponent {

           public String messageProcess(Exchange msg){
             // do your logic here
           }
  }

@Configuration
public class MyTCPCamelRouter extends RouteBuilder {


    @Override
    public void configure() throws Exception {

        from("direct:my-tcp")
                .to("bean:my-tcp")
                .to("log:foo");
    }
}

在 pom.xml 中

 <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>3.0.0-SNAPSHOT</version>
 </dependency>

我正在考虑的另一个解决方案是使用 maven 原型生成骆驼自定义组件,然后将其作为 jar 导入到 spring-boot 应用程序中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多