I'm back on my Kafka project. I'm slowly advancing...
# cfml-general
s
I'm back on my Kafka project. I'm slowly advancing. The latest issue encountered deals with CF-2021 and a returned instance of java.util.Iterator. The sample code I am using loops through the iterator but it terminates immediately, as if no records:
Copy code
for ( record in records.iterator() ) {
      try{
        event = record.value();
        writeOutput("<br>EVENT: #event#");
      }catch( any e ){
        writeDump(var=e,label="exception");
      }
}
If I dump records.count(), it shows 1 record, but the for loop above does not reflect the record. Has anyone experience issues using a java.util.Iterator instance? In the screenshot below, after the records.iterator() dump, but before the Counter = 6... would have been additional text displayed - EVENT: something - with EVENT being hard-coded proving the iterator loop is terminating at zero records (hopefully that makes sense).
And more strange, changing the loop to the following works:
Copy code
// works
    rows = records.iterator();
    while( rows.hasNext() ){
      row = rows.next();
      event = row.value();
      writeOutput("<br>EVENT via next: #event#");
    }

    // does not work
    rows = records.iterator();
    for ( row in rows ) {
      event = row.value();
      writeOutput("<br>EVENT via loop: #event#");
    }
To me, this looks like a bug in CF-2021. Anyone else have any thoughts?
w
curious why you chose to use the iterator() rather than simply looping the resultset:
for( record in records ) { writeoutput(record.colname); }
is there some real or perceived advantage to using the java iterator here?
oh sorry, it's not a native cf resultset?
s
The jar component I am using returns an iterator. The code used to work on cf-2016 and prior, it appears broken at the moment in cf-2021.
w
understood. i have nothing to add other than i recall always using a while loop and hasNext rather than your original example (whether it worked previously or not). hardly a java expert here, but have used my share from cf
👍 1
b
I'm fairly sure cfml for loops don't work on java iterator Instances. I've always used hasNext() and next()
s
I found it works sometimes, possibly with certain versions or patch levels. The problem is, I use "for( var item in items )..." so much that I forget that with a java iterator, it can bite you. Now that I figured this particular case out, now I remember solving this before in another case.