for (Object current : items) {
/**
* Throw exception for testing purposes?
*/
if (!exceptions.isEmpty()) {
Employee emp = (Employee) current;
Class<Exception> ex = exceptions.get(emp.getId());
if (ex != null) {
LOG.warn("We reached item with employee ID " + emp.getId()
+ ", throwing emulated exception as requested: "
+ ex.getCanonicalName());
LOG.warn("Affected item: " + current);
emp.setRemark("This item caused an exception!");
failedItems.add(emp);
throw ex.getConstructor(String.class).newInstance(
"Emulated exception thrown by request (configured in job XML!.)");
}
}
}
/*
Remember! This is JPA. Since we are still in the transaction as opened
in the reader, our entities are still attached to the persistence context.
There is no need to call for persistence explicitly ;)
However, before the transaction is finished, we ensure writing by calling
EntityManager.flush() via our service.
*/
employeeService.flush();
// Debugging? OK, log our each written employee...
if (LOG.isDebugEnabled()) {
for (Object current : items) {
Employee e = (Employee) current;
LOG.debug("WRITE: " + e);
}
}
}