我是Apache Camel的新手。在hp不间断中,有一个Receiver接收事件管理器生成的事件,就像流一样。我的目标是设置一个消费者端点,它接收传入的消息并通过Camel处理它。
另一个终点我只需要在日志中写一下。从我的研究中我了解到,对于消费者终点我需要创建自己的组件,配置就像
from("myComp:receive").to("log:net.javaforge.blog.camel?level=INFO")
这是我的代码片段,它接收来自事件系统的消息。
Receive receive = com.tandem.ext.guardian.Receive.getInstance();
byte[] maxMsg = new byte[500]; // holds largest possible request
short errorReturn = 0;
do { // read messages from $receive until last close
try {
countRead = receive.read(maxMsg, maxMsg.length);
String receivedMessage=new String(maxMsg, "UTF-8");
//Here I need to handover receivedMessage to camel
} catch (ReceiveNoOpeners ex) {
moreOpeners = false;
} catch(Exception e) {
moreOpeners = false;
}
} while (moreOpeners);
有人可以提供一些提示,指导如何将其作为消费者。
答案 0 :(得分:4)
10'000英尺的视图是这样的:
您需要从实施组件开始。最简单的入门方法是扩展org.apache.camel.impl.DefaultComponent
。您唯一要做的就是覆盖DefaultComponent::createEndpoint(..)
。很明显它的作用是创建你的终端。
所以接下来你需要的是实现你的端点。为此扩展org.apache.camel.impl.DefaultEndpoint
。覆盖至少DefaultEndpoint::createConsumer(Processor)
以创建您自己的消费者。
最后但并非最不重要的是,您需要实施消费者。同样,最好是扩展org.apache.camel.impl.DefaultConsumer
。消费者是您的代码必须生成消息的地方。通过构造函数,您将收到对端点的引用。使用端点引用创建新Exchange,填充它并沿路径发送它。
Exchange ex = endpoint.createExchange(ExchangePattern.InOnly);
setMyMessageHeaders(ex.getIn(), myMessagemetaData);
setMyMessageBody(ex.getIn(), myMessage);
getAsyncProcessor().process(ex, new AsyncCallback() {
@Override
public void done(boolean doneSync) {
LOG.debug("Mssage was processed " + (doneSync ? "synchronously" : "asynchronously"));
}
});
我建议您选择一个简单的组件(DirectComponent
?)作为示例。
答案 1 :(得分:0)
因此,添加我自己的消费者组件可能会帮助某人。
public class MessageConsumer extends DefaultConsumer {
private final MessageEndpoint endpoint;
private boolean moreOpeners = true;
public MessageConsumer(MessageEndpoint endpoint, Processor processor) {
super(endpoint, processor);
this.endpoint = endpoint;
}
@Override
protected void doStart() throws Exception {
int countRead=0; // number of bytes read
do {
countRead++;
String msg = String.valueOf(countRead)+" "+System.currentTimeMillis();
Exchange ex = endpoint.createExchange(ExchangePattern.InOnly);
ex.getIn().setBody(msg);
getAsyncProcessor().process(ex, new AsyncCallback() {
@Override
public void done(boolean doneSync) {
log.info("Mssage was processed " + (doneSync ? "synchronously" : "asynchronously"));
}
});
// This is an echo server so echo request back to requester
} while (moreOpeners);
}
@Override
protected void doStop() throws Exception {
moreOpeners = false;
log.debug("Message processor is shutdown");
}
}