I am performing some mass writing in a .csv file using Groovy. More specifically, I have a Quartz job that is running and creates some Map messages that get sent to a RabbitMQ queue. The queue is being consumed by 10 consumers and results in producing some lists of Strings. For each element in the List I just write it in a pipe separated .csv file. The actual service that has the method that writes to the .csv file, is a standard (singleton) transactional grails service. When I log the lines to be written, everything’s fine, but in the file, some lines are “broken”. The way I am writing is:
def writeRowsToFile(List<String> rows, File file) {
rows.each {row->
file.append("${row}\n")
}
}
Initially I was using:
file.withWriterAppend {out->
out.write(row.toString())
out.newLine()
}
and got the same thing as well…
If it was something wrong it would fail for all the lines. Could it be some kind of race condition, concurrency or I don’t know what else issue?
Any help will be appreciated.
Thanks
You should be doing it the second way, ie:
However, you say it might be “some kind of race condition”
Are multiple threads writing to the same file?
If not, it is more likely that your
rowdata has\ncharacters in it