2016-07-26 114 views
2

我正在尝试使用骆驼的bindy注释来解析通过CSV。我在网上看了一些教程,但似乎没有让他们工作。我是骆驼这边的新手,所以我不太明白我得到的错误。目前我的CSV非常简单,因为我只是想了解这个功能的工作原理。 该CSV目前看起来是这样的:使用骆驼来解析CSV

HDR | Suborg | CountryCode | BrokerFile | Batch | Time | Date |

的错误即时得到它这样的:

org.apache.camel.RuntimeCamelException:java.lang.InstantiationException:com.ups.ttg.bsis.fromdos.AlamoHdr

这里是我的代码:

public class AlamoPipeRouteBuilder extends RouteBuilder { 

    final DataFormat bindy = new BindyCsvDataFormat(AlamoHdr.class); 

    /* 
    * Endpoints 
    */ 
    @EnforceInitialization 
    private Logging logging; 

    @EnforceInitialization 
    private RatingProfileAlamoSplitHandler ratingProfileAlamoSplitHandler; 

    @EnforceInitialization 
    private String start = ""; 

    @EnforceInitialization 
    private String end = ""; 

    @Override 
    public void configure() throws Exception { 
     System.out.println("Started Configure Method"); 

     /* 
     * Basic Route 
     */ 
     from(start) 
     .setExchangePattern(ExchangePattern.InOnly) 
     .routeId("processRatingProfile.alamo") 
     //.beanRef("RatingProfileExchangeUtilies", "checkForNoRecords(*)") 
     .beanRef("logging", "debug(*, 'Starting aggregation strategy loop...')") 
     .split(body().tokenize("\n"), ratingProfileAlamoSplitHandler).streaming() 
      .unmarshal(bindy) 
      .setHeader("INDEX", simple("${header.CamelSplitIndex}")) 
      .setHeader("COMPLETE", simple("${header.CamelSplitComplete}")) 
     .end() 
     .beanRef("logging", "debug(*, 'Aggregation strategy loop complete...')") 
     .removeHeader("lastRatingProfile") 
     .to(end); 
    } 

    public void setStart(String start) { 
     this.start = start; 
    } 

    public void setEnd(String end) { 
     this.end = end; 
    } 

    public void setRatingProfileAlamoSplitHandler(RatingProfileAlamoSplitHandler ratingProfileAlamoSplitHandler) { 
     this.ratingProfileAlamoSplitHandler = ratingProfileAlamoSplitHandler; 
    } 

    public void setLogging(Logging logging) { 
     this.logging = logging; 
    } 
} 



public class RatingProfileAlamoSplitHandler implements AggregationStrategy {  

    @EnforceInitialization 
    private static Logging logging; 

    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { 
     Integer currIndex = -1; 
     boolean lastLine = false; 

     if(newExchange != null) { 
      currIndex = (Integer) newExchange.getIn().getHeader("INDEX"); 
      System.out.println("THIS IS THE INDEX: " + currIndex); 

      /*lastLine = (Boolean) newExchange.getIn().getHeader("COMPLETE"); 
      System.out.println("THIS IS THE COMPLETE: " + lastLine);*/ 

      System.out.println("This IS THE BODY: " + newExchange.getIn().getBody()); 

      if(currIndex == 0) { 
       AlamoHdr alamoHdr = (AlamoHdr) newExchange.getIn().getBody(); 
      } 
     } 

     return newExchange; 
    } 

    public static void setLogging(Logging logging) { 
     RatingProfileAlamoSplitHandler.logging = logging; 
    } 

} 

public class AlamoHdr implements InitializingBean, DisposableBean { 

    @DataField(pos = 2, trim = true) 
    private String suborg; 

    @DataField(pos = 3, trim = true) 
    private String countryCode; 

    @DataField(pos = 4, trim = true) 
    private String brokerFile; 

    @DataField(pos = 5, trim = true) 
    private String batch; 

    @DataField(pos = 6, trim = true) 
    private String time; 

    @DataField(pos = 7, trim = true) 
    private String date; 

    public AlamoHdr(String suborg, String countryCode, String brokerFile, String batch, String time, String date) { 
     super(); 
     this.suborg = suborg; 
     this.countryCode = countryCode; 
     this.brokerFile = brokerFile; 
     this.batch = batch; 
     this.time = time; 
     this.date = date; 
    } 

    public String getSuborg() { 
     return suborg; 
    } 

    public void setSuborg(String suborg) { 
     this.suborg = suborg; 
    } 

    public String getCountryCode() { 
     return countryCode; 
    } 

    public void setCountryCode(String countryCode) { 
     this.countryCode = countryCode; 
    } 

    public String getBrokerFile() { 
     return brokerFile; 
    } 

    public void setBrokerFile(String brokerFile) { 
     this.brokerFile = brokerFile; 
    } 

    public String getBatch() { 
     return batch; 
    } 

    public void setBatch(String batch) { 
     this.batch = batch; 
    } 

    public String getTime() { 
     return time; 
    } 

    public void setTime(String time) { 
     this.time = time; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 

    @Override 
    public String toString() { 
     return "AlamoHdr [suborg=" + suborg + ", countryCode=" + countryCode + ", brokerFile=" + brokerFile + ", batch=" 
       + batch + ", time=" + time + ", date=" + date + "]"; 
    } 

    public void destroy() throws Exception { 
     // TODO Auto-generated method stub 

    } 

    public void afterPropertiesSet() throws Exception { 
     // TODO Auto-generated method stub 

    } 

} 
+0

由于某种原因,错误日志没有打印在我的问题...这里他们是 –

+0

org.apache.camel.RuntimeCamelException:java.lang.InstantiationException:com.ups.ttg.bsis.fromdos.AlamoHdr –

+0

请编辑您的问题与您的错误日志,不要张贴日志中的评论。 – rmlan

回答

0

对于任何人也可能会遇到这个问题,我发现什么问题。使用|时作为你的分隔符,你需要通过它像这样

@CsvRecord(separator = "\\|", skipFirstLine = false) 

因为|是表示OR操作的元字符,我们需要这个操作的正则表达式。

此外,您不能像AlamoHdr文件中的构造函数,因为变量正在通过绑定填充,而不是通过调用构造函数。