栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

改善此方案性能的方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

改善此方案性能的方法

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();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号