我尝试使用resequencer流处理Camel路由中的Exception
我使用Camel 2.15.2
有时交换不会与我的deadLetterChannel一起路由,
并且在日志中是NullPointerException
似乎交换的UnitOfWork有时会丢失
我可以用这个类重现它:
音序器(处理器):
public class Sequencer implements Processor {
private int sequence = -1;
@Override
public void process(Exchange exchange) throws Exception {
exchange.setProperty(PropertyFields.SEQUENCE_NUMBER, getNextSequenceNumber());
}
private synchronized int getNextSequenceNumber() {
sequence++;
if (sequence >= Integer.MAX_VALUE) {
sequence = 0;
}
return sequence;
}
}
处理器抛出异常:
public class ProzessorErrorThrower implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
try {
UnitOfWork unit = exchange.getUnitOfWork();
System.out.println("UNIT: " + unit + " | " + exchange.getIn().getBody());
} catch(Exception e) {
e.printStackTrace();
}
exchange.setException(new Exception("Exception: " + exchange.getIn().getBody()));
}
}
处理异常的处理器:
public class ProzessorErrorHandler implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
Object o = exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
if (o != null) {
if (o instanceof Exception) {
Exception e = (Exception)o;
System.out.println("Error Handler " + exchange.getIn().getBody() + " " + e.getMessage());
} else {
System.out.println("ERROR");
}
} else {
System.out.println("ERROR");
}
}
}
我的方法路线和发送消息到路线:
CamelContext context = new DefaultCamelContext();
RouteBuilder builder = new RouteBuilder() {
@Override
public void configure() throws Exception {
errorHandler(deadLetterChannel("direct:deadLetter"));
from("direct:deadLetter").process(new ProzessorErrorHandler());
StreamResequencerConfig configStream = new StreamResequencerConfig(100, 3000L);
from("direct:resequencer").
resequence(exchangeProperty(PropertyFields.SEQUENCE_NUMBER)).
stream(configStream).
process(new ProzessorErrorThrower());
from("direct:start").process(new Sequencer()).to("direct:resequencer");
}
};
context.addRoutes(builder);
ProducerTemplate template = context.createProducerTemplate();
context.start();
try {
template.sendBody("direct:start", "Test 01");
template.sendBody("direct:start", "Test 02");
template.sendBody("direct:start", "Test 03");
Thread.sleep(5000);
System.out.println();
System.out.println();
template.sendBody("direct:start", "Test 04");
template.sendBody("direct:start", "Test 05");
template.sendBody("direct:start", "Test 06");
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
} finally {
context.stop();
}
我得到以下输出:
UNIT: DefaultUnitOfWork | Test 01
Error Handler Test 01 Exception: Test 01
UNIT: DefaultUnitOfWork | Test 02
Error Handler Test 02 Exception: Test 02
UNIT: DefaultUnitOfWork | Test 03
Error Handler Test 03 Exception: Test 03
UNIT: null | Test 04
UNIT: DefaultUnitOfWork | Test 05
Error Handler Test 05 Exception: Test 05
UNIT: DefaultUnitOfWork | Test 06
Error Handler Test 06 Exception: Test 06
在日志中:
WARN - 11.08.2015 09:52:17.247 [Caused by: [java.lang.NullPointerException - null]] -> camel.processor.StreamResequencer
java.lang.NullPointerException
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:372)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87)
at org.apache.camel.processor.StreamResequencer.sendElement(StreamResequencer.java:192)
at org.apache.camel.processor.StreamResequencer.sendElement(StreamResequencer.java:68)
at org.apache.camel.processor.resequencer.ResequencerEngine.deliverNext(ResequencerEngine.java:280)
at org.apache.camel.processor.resequencer.ResequencerEngine.deliver(ResequencerEngine.java:244)
at org.apache.camel.processor.StreamResequencer$Delivery.run(StreamResequencer.java:262)
如果我修改了我的代码并设置了一个新的工作单元,如果它是null,则在另一条消息上出现相同的错误,但没有输出它是null。
日志中的第一个条目指向访问工作单元的骆驼代码行(RedeliveryErrorHandler.java:372)
我还没有找到与此相关的东西。
有人可以帮助我吗?