在spring-boot应用程序中使用的自定义骆驼组件

时间:2019-05-19 17:44:06

标签: spring-boot apache-camel

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

我尝试使用 maven原型生成组件。

所以命令是这样的:

  

MVN原型:generate -DarchetypeGroupId = org.apache.camel.archetypes   -DarchetypeArtifactId =骆驼原型组件-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与骆驼自动发现组件一起使用。

谢谢。

1 个答案:

答案 0 :(得分:0)

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

我决定仅将其用作参考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原型的骆驼自定义组件,然后在Spring-boot应用程序中将其作为jar导入。