Skip to content

Instantly share code, notes, and snippets.

@Edijun
Last active April 30, 2019 07:15
Show Gist options
  • Select an option

  • Save Edijun/9755b957ff8880c3fbea6315c228e8a0 to your computer and use it in GitHub Desktop.

Select an option

Save Edijun/9755b957ff8880c3fbea6315c228e8a0 to your computer and use it in GitHub Desktop.
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import id.edijun.example.springbatch.model.Product;
@Component
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class);
private final JdbcTemplate jdbcTemplate;
@Autowired
public JobCompletionNotificationListener(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void afterJob(JobExecution jobExecution) {
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
log.info("============ JOB FINISHED ============ Verifying the results....\n");
List<Product> results = jdbcTemplate.query("SELECT id, name, price, description FROM product",
new RowMapper<Product>() {
@Override
public Product mapRow(ResultSet rs, int row) throws SQLException {
return new Product(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getString(4));
}
});
for (Product product : results) {
log.info("Discovered <" + product + "> in the database.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment