
for循环由于两个原因而花费时间。
1)个人电子邮件通过减少传输连接来
改善它2)个人提交通过以下方法来改善它
所以理想的情况是同时处理这两种情况,我建议批量处理1000次,然后再打数字
例
int BATCH_SIZE = 1000conn = DriverManager.getConnection("username","password");conn.setAutoCommit(false);Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);int count = 0;Map<String, String> emails_map = new HashMap(BATCH_SIZE)<String, String>;for (Map.Entry<String, List<ClassOBj>> entry : testMap .entrySet()) { String email = get_useremail(); String const_val = do_magic(); // this is how you are computing some constant String body = construct_body(); count++; String SQL = "YOUR UPDATe STATEMENT"; stmt.executeUpdate(SQL); emails_map.put(email, body); // can create if (count % BATCH_SIZE == 0) { //commits all transcations conn.commit(); //bulk send emails sending //http://stackoverflow.com/questions/13287515/how-to-send-bulk-mails-using-javax-mail-api-efficiently-can-we-use-reuse-auth bulk_emails_send(emails_map) }}public void bulk_emails_send(Map<String, String> emails_map) { // Get the default Session object through your setting Session session = Session.getDefaultInstance(properties); Transport t = session.getTransport(); t.connect(); try { for (String email_id in emails_map) { Message m = new MimeMessage(session); //add to, from , subject, body m.saveChanges(); t.sendMessage(m, m.getAllRecipients()); } } finally { t.close(); }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)