camel系列-处理器(Processor)

概念

问题

在保持独立性和灵活性的前提下,如何对消息完成复杂的处理?

解决方案

使用管道和过滤器体系结构把较大的处理任务划分为一系列较小的独立处理步骤(过滤器),这些步骤由通道(管道)连接。

处理器示例

接口定义

1
2
3
4
5
6
7
8
9
10
11
@FunctionalInterface
public interface Processor {

/**
* Processes the message exchange
*
* @param exchange the message exchange
* @throws Exception if an internal processing error has occurred.
*/
void process(Exchange exchange) throws Exception;
}

自定义实现

1
2
3
4
5
public class MyProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
// do something...
}
}

然后在 Camel 中你可以调用这个处理器:

1
2
from("activemq:myQueue")
.process(new MyProcessor());

参考