From what I can say, is that you are likely to receive single-threaded data and multi-threaded for sending via email. Roughly speaking, you will ride a bike through your result set and make a list of entries. When this list reaches a certain size, you make a copy and send this copy for processing in the stream and clear the original list. At the end of the result set, check to see if you have raw records in your list and submit them to the pool.
Finally, wait until threadpool finishes processing all entries.
Something like that:
protected void processRecords(String countryName) { ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy()); List<String[]> emaillist = new ArrayList<String>(1000); ResultSet rs = .... try { while (rs.next()) { String user[] = new String[2]; users[0] = rs.getString("userid"); users[1] = rs.getString("emailAddress"); emaillist.add(user); if (emaillist.size() == 1000) { final List<String[]> elist = new ArrayList<String[]>(emaillist); executor.execute(new Runnable() { public void run() { sendMail(elist); } } emaillist.clear(); } } } finally { DbUtils.close(rs); } if (! emaillist.isEmpty()) { final List<String[]> elist = emaillist; executor.execute(new Runnable() { public void run() { sendMail(elist); } } emaillist.clear(); }
source share