You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by contactreji <co...@gmail.com> on 2016/01/06 12:40:46 UTC
Producer Template // Count mismatch
Hi
I have a code like this
package com.processors
import org.apache.camel.Endpoint
import org.apache.camel.Exchange
import org.apache.camel.ExchangePattern
import org.apache.camel.Processor
import org.apache.camel.impl.DefaultCamelContext
/**
* Created by Z001NLH on 12/30/2015.
*/
class BatchOutRecordsImpl implements Processor{
//Static variables section
static def BATCH_GROUP_KEY = "FRTB_I"
static def DESTINATION_CAMEL_ENDPOINT =
"seda:AllocationAmountPerShipment" //This should be a valid camel endpoint
//static def DESTINATION_CAMEL_ENDPOINT = "vm:batchedRecords" //This
should be a valid camel endpoint
@Override
void process(Exchange exchange) throws Exception {
def records=(List<Map<String, Object>>)
exchange.getIn().getBody()
def camelContext = exchange.getContext()
def producerTemplate = camelContext.createProducerTemplate()
def batch= new ArrayList<HashMap<String,Object>>()
def previousBatchKey=null
def isLastIteration=false
def counter=1
//Looping through resultset
for(HashMap<String,Objects> record in records)
{
isLastIteration=false
if(counter==records.size()) {
isLastIteration = true
}
def batchKey=record.get(BATCH_GROUP_KEY)
try{
previousBatchKey=batch.get(batch.size()-1).get(BATCH_GROUP_KEY)
}catch(java.lang.ArrayIndexOutOfBoundsException e)
{
println("Batch was Empty / Cleared stage")
previousBatchKey=batchKey
}
if(batchKey==previousBatchKey) {
batch.add(record)
}
if(isLastIteration||(batchKey!=previousBatchKey)) {
//Sending for processing
*producerTemplate.sendBody(DESTINATION_CAMEL_ENDPOINT,
ExchangePattern.InOnly, batch)*
//wiretapping records to folder dumped records
def freightID=batch.get(batch.size()-1).get("FRTB_I")
* producerTemplate.sendBodyAndHeader("vm:batchedRecords",
ExchangePattern.InOnly,
batch.toString(),"CamelFileName",freightID.toString())*
batch.clear()
if(isLastIteration==false) {
batch.add(record)
}
}
counter++
}
}
}
I have highlighted 2 producer templates.. Unfortunately I am trying to send
the same body from both templates. But on the other end, the numbers
mismatch. One producer template received 2098 records and other one received
2089.
Any clue of why this happens? The body contains List<HashMaps<k,v>>
Cheers
Reji
-----
Reji Mathews
Sr. Developer - Middleware Integration / SOA ( Open Source - Apache Camel & Jboss Fuse ESB | Mule ESB )
LinkedIn - http://in.linkedin.com/pub/reji-mathews/31/9a2/40a
Twitter - reji_mathews
--
View this message in context: http://camel.465427.n5.nabble.com/Producer-Template-Count-mismatch-tp5775891.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Producer Template // Count mismatch
Posted by Reji Mathews <co...@gmail.com>.
Hey Saurabh
Thanks for ur solution . It was indeed perfect. I overlooked on the fact
that java works on call by reference model and it's always pointers to the
memory location.
We fixed it by using Cloned objects
Cheers
Reji
On 7 Jan 2016 02:49, "CamelNayar" <Na...@gmail.com> wrote:
> Reji,
>
> I am not sure why you are facing the problem but something looks off with
> the example. You are passing an object "batch" to vm component and clearing
> the object in next line. vm component is asynchronous - so the object you
> are trying to pass will clear once you do batch.clear().
>
> Regards,
> Saurabh Nayar
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Producer-Template-Count-mismatch-tp5775891p5775932.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: Producer Template // Count mismatch
Posted by CamelNayar <Na...@gmail.com>.
Reji,
I am not sure why you are facing the problem but something looks off with
the example. You are passing an object "batch" to vm component and clearing
the object in next line. vm component is asynchronous - so the object you
are trying to pass will clear once you do batch.clear().
Regards,
Saurabh Nayar
--
View this message in context: http://camel.465427.n5.nabble.com/Producer-Template-Count-mismatch-tp5775891p5775932.html
Sent from the Camel - Users mailing list archive at Nabble.com.