【发布时间】: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")` ?