当我向带有限制的路由发送多线程请求时,我会得到NPE异常。如果路线没有节流,一切都很好。在多线程环境中使用限制时,是否需要同步某些位置?
路线以成功结束:
from("direct:start").log("executed ${body}").to("mock:result");
有节流的路线有时会以NPE结束:
from("direct:start").throttle(method(ThrottlingTester.class, "getMessagesPerSecond")).timePeriodMillis(1000).rejectExecution(true).log("executed ${body}").to("mock:result");
代码发送请求:
MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
resultEndpoint.expectedMessageCount(6);
resultEndpoint.setResultWaitTime(3000);
for (int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
template.sendBody("direct:start", "<test1> </test1>");
}
}).start();
}
for (int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
template.sendBody("direct:start", "<test2> </test2>");
}
}).start();
}
resultEndpoint.assertIsSatisfied();
代码计算限制值:
public synchronized static long getMessagesPerSecond(Exchange exchange){
if(exchange.getIn().getBody(String.class).indexOf("test2")>0)
return 1000;
return 1;
}
错误:
Caused by: java.lang.NullPointerException
at org.apache.camel.processor.Throttler$TimeSlot.access$002(Throttler.java:158)
at org.apache.camel.processor.Throttler.calculateDelay(Throttler.java:123)
at org.apache.camel.processor.DelayProcessorSupport.process(DelayProcessorSupport.java:156)
使用Camel版本2.15.3