骆驼循环在第三次迭代中使用不同的交换

camel loop uses a different exchange in the third iteration

我是 Apache 骆驼的新手。我正在使用 Red Hat JBoss developer studio 11.0.0.GA。我正在尝试从 table 中读取大量记录并将它们插入到另一个记录中。因为我不能一次插入所有记录(我认为骆驼对插入 7000 条记录有限制)。我用了一个骆驼环。首先我得到了所有的记录。对它们进行了一些处理并将结果列表设置为 属性。此外,我设置了一个 Integer-value-属性 来表示我们必须为当前迭代跳过多少条记录。现在我的问题是,对于第三轮(迭代),交换对象发生了变化。调试了一下,发现exchangeId不一样。所以我设置的属性都没有了

    <camelContext id="_context1" xmlns="http://camel.apache.org/schema/blueprint">
    <route id="_route1">
        <from id="_from1" uri="file:work/input"/>
        <to id="_to2" uri="sqlAbniyeh: select id , CRATERSLIST from ABNIYEH_BRIDGE "/>
        <setProperty propertyName="isThereAnyRecord">
            <simple resultType="java.lang.Boolean">true</simple>
        </setProperty>
        <loop doWhile="true" id="_loop1" >
            <simple>${exchangeProperty.isThereAnyRecord} != false</simple>                
            <process id="_process1" ref="craters"/>
            <to id="_to1" uri="sqlAbniyeh:  insert into Z_ABNIYEH_BRIDGE_CRATERS( ID , NUMBER1 , LENGTH , BRIDGE_ID) values( HIBERNATE_SEQUENCE.nextval , :#value1 , :#value2 , :#id)?batch=true"/>
        </loop>
    </route>
</camelContext>

这是我的处理方法:

    public void process(Exchange exchange) throws Exception {
    try
    {
        System.out.println("Entered Process method!");
        List<Map<String, Object>> currentList = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> newList = new ArrayList<Map<String,Object>>();
        int numberOfRecordsToSkip = 0;
        int numberOfRecordsToSkipForTheNextTime ;
        List<Map<String, Object>> currentListOnProperties = (List<Map<String,Object>>) exchange.getProperty("listOfRecords");
        numberOfRecordsToSkip = exchange.getProperty("numberOfRecordsToSkip") != null ?
                                                                    (Integer)exchange.getProperty("numberOfRecordsToSkip"): 0;

        if(currentListOnProperties != null)
        {
            newList = currentListOnProperties;              
        }
        else
        {
            // It occurs just the first time                
            currentList = (List<Map<String,Object>>)exchange.getIn().getBody();
            newList = OrganizeListForInsert(currentList);
        }

        int temp = (numberOfRecordsToSkip + NUMBER_OF_RECORDS_FOR_EACH_ROUND);
        if(temp < newList.size()) 
        {
            numberOfRecordsToSkipForTheNextTime =   temp; 
        }
        else
        {   
            numberOfRecordsToSkipForTheNextTime = numberOfRecordsToSkip + ( currentList.size() - numberOfRecordsToSkip);
            exchange.removeProperty("isThereAnyRecord");
            exchange.setProperty("isThereAnyRecord", false);
        }
        exchange.removeProperty("numberOfRecordsToSkip");
        exchange.setProperty("numberOfRecordsToSkip", new Integer(numberOfRecordsToSkipForTheNextTime));
        exchange.setProperty("listOfRecords", newList);
        List<Map<String, Object>> sublistOfNewList = 
                        new ArrayList<Map<String,Object>>(newList.subList(numberOfRecordsToSkip, numberOfRecordsToSkipForTheNextTime));
        exchange.getIn().setBody(sublistOfNewList);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    System.out.println("End of everything!!");
}

正如 Bedla 所说,我应该使用 EIP 而不是在 java 中编写所有内容。我使用了 Splitter EIP,它很适合我的情况。这是新代码:

    <camelContext id="_context1" xmlns="http://camel.apache.org/schema/blueprint">
    <route id="_route1">
        <from id="_from1" uri="file:work/input"/>
        <to id="_to2" uri="sqlAbniyeh: select id , CRATERSLIST from ABNIYEH_BRIDGE "/>
        <process id="_process1" ref="craters"/>
        <split>
            <simple>${body}</simple>
            <to id="_to1" uri="sqlAbniyeh:  insert into ABNIYEH_BRIDGE_CRATERS( ID , NUMBER1 , LENGTH , BRIDGE_ID) values( HIBERNATE_SEQUENCE.nextval , :#value1 , :#value2 , :#id)"/>
        </split>
    </route>
</camelContext>

及处理方法:

    @Override
public void process(Exchange exchange) throws Exception {
    try
    {
        List<Map<String, Object>> currentList = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> newList = new ArrayList<Map<String,Object>>();
        currentList = (List<Map<String,Object>>)exchange.getIn().getBody();
        newList = OrganizeListForInsert(currentList);
        exchange.getIn().setBody(newList);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}