Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class MySampleService {
- private final static Logger LOGGER = Logger
- .getLogger(MySampleService.class);
- @Async
- public Future<String> callAsync(JdbcTemplate template, String query) throws InterruptedException {
- try {
- jdbcTemplate.query(query);
- //process the results
- return new AsyncResult<String>("success");
- }
- catch (Exception ex){
- return new AsyncResult<String>("failed");
- }
- }
- public String taskExecutor() throws InterruptedException, ExecutionException {
- Future<String> asyncResult1 = mySampleService.callAsync(jdbcTemplate,query1);
- Future<String> asyncResult2 = mySampleService.callAsync(jdbcTemplate,query2);
- Future<String> asyncResult3 = mySampleService.callAsync(jdbcTemplate,query3);
- Future<String> asyncResult4 = mySampleService.callAsync(jdbcTemplate,query4);
- LOGGER.info(asyncResult1.get());
- LOGGER.info(asyncResult2.get());
- LOGGER.info(asyncResult3.get());
- LOGGER.info( asyncResult4.get());
- //now all threads finished, close the connection
- jdbcTemplate.getConnection().close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement