我正在使用骆驼绑定来实现解析器。 我有很多事件的固定长度记录。
因此,我尝试使用分离器解析此记录。
我认为FixedLengthFormat类可以处理页眉和页脚。 因此,我在Body Dataformat类中定义了以下代码。
@FixedLengthRecord(length = 20, header=VoLTEHeader.class, footer=VoLTEFooter.class)
但是我发现下面的异常。 请检查此问题。
**路由器**
public static class ContextConfig extends RouteBuilder {
DataFormat bindyHeader = new BindyFixedLengthDataFormat(VoLTEHeader.class);
DataFormat bindyTailer = new BindyFixedLengthDataFormat(VoLTEFooter.class);
DataFormat bindyOneBody = new BindyFixedLengthDataFormat(VoLTEBody.class);
@Override
public void configure() throws Exception {
from(URI_DIRECT_START)
.setBody().groovy("request.body.split('(?<=\\\\G.{20})')
.split(body(), AggregationStrategies.flexible().storeInHeader("bodyList").accumulateInCollection(ArrayList.class))
.unmarshal(bindyOneBody)
.end()
.to(URI_MOCK_RESULT);
}
**测试**
@Test
@DirtiesContext
public void testUnMarshallMessage() throws Exception {
String URI_DIRECT_START = "direct:start";
String records = "1VOLTE20190515 32010123456782019051520101234567820190516201012345678201905173AAAA.DAT ";
template.sendBody(URI_DIRECT_START, aaa);
**数据格式**
@FixedLengthRecord(length = 20, paddingChar = ' ')
public class VoLTEHeader {
@DataField(pos = 1, length = 1, trim = true)
String record_type;
@DataField(pos = 2, length = 5, trim = true)
String service_type;
@DataField(pos = 7, length = 8, trim = true)
String creation_date;
@DataField(pos = 15, length = 6, trim = true, align = "R")
String custom_flag;
@Override
public String toString() {
return String.format("VoLTEHeader[record_type=%s, service_type=%s, creation_date=%s, custom_flag=%s]", record_type, service_type, creation_date, custom_flag);
}
}
@FixedLengthRecord(length = 20, header=VoLTEHeader.class, footer=VoLTEFooter.class)
public class VoLTEBody {
@DataField(pos = 1, length = 1, trim = true)
String record_type;
@DataField(pos = 2, length = 11, trim = true)
String mobile_number;
@DataField(pos = 13, length = 8, trim = true)
String call_start_date;
@Override
public String toString() {
return String.format("VoLTEBody[record_type=%s, mobile_number=%s, call_start_date=%s]", record_type, mobile_number, call_start_date);
}
}
@FixedLengthRecord(length = 20, paddingChar = ' ')
public class VoLTEFooter {
@DataField(pos = 1, length = 1, trim = true)
String record_type;
@DataField(pos = 2, length = 19, trim = true)
String file_name;
@Override
public String toString() {
return String.format("VoLTEFooter[record_type=%s, file_name=%s]", record_type, file_name);
}
}
**错误日志**
Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor Elapsed (ms)
[route1 ] [route1 ] [direct://start ] [ 564]
[route1 ] [unmarshal1 ] [unmarshal[org.apache.camel.model.DataFormatDefinition@115dcaea] ] [ 50]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
java.lang.IllegalArgumentException: No records have been defined in the file
at org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat.unmarshal(BindyFixedLengthDataFormat.java:250) ~[camel-bindy-2.23.2.jar:2.23.2]
at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69) ~[camel-core-2.23.2.jar:2.23.2]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) [camel-core-2.23.2.jar:2.23.2]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.23.2.jar:2.23.2]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) [camel-core-2.23.2.jar:2.23.2]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.23.2.jar:2.23.2]
at org.apache.camel.processor.MulticastProcessor.doProcessSequential(MulticastProcessor.java:715) [camel-core-2.23.2.jar:2.23.2]