Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. @Component
  2. public class MySampleService {
  3.  
  4. private final static Logger LOGGER = Logger
  5. .getLogger(MySampleService.class);
  6.  
  7. @Async
  8. public Future<String> callAsync(JdbcTemplate template, String query) throws InterruptedException {
  9.  
  10. try {
  11. jdbcTemplate.query(query);
  12. //process the results
  13. return new AsyncResult<String>("success");
  14. }
  15. catch (Exception ex){
  16. return new AsyncResult<String>("failed");
  17. }
  18. }
  19.  
  20. public String taskExecutor() throws InterruptedException, ExecutionException {
  21. Future<String> asyncResult1 = mySampleService.callAsync(jdbcTemplate,query1);
  22. Future<String> asyncResult2 = mySampleService.callAsync(jdbcTemplate,query2);
  23. Future<String> asyncResult3 = mySampleService.callAsync(jdbcTemplate,query3);
  24. Future<String> asyncResult4 = mySampleService.callAsync(jdbcTemplate,query4);
  25.  
  26. LOGGER.info(asyncResult1.get());
  27. LOGGER.info(asyncResult2.get());
  28. LOGGER.info(asyncResult3.get());
  29. LOGGER.info( asyncResult4.get());
  30.  
  31. //now all threads finished, close the connection
  32. jdbcTemplate.getConnection().close();
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement