Skip to content

Commit

Permalink
Merge pull request #631 from support-project/fix/issue628_sendmail
Browse files Browse the repository at this point in the history
#628 Changed to hold session with mail server
  • Loading branch information
koda-masaru authored Feb 6, 2017
2 parents 050b626 + 71ff7a6 commit 95e316c
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.support.project.knowledge.entity.KnowledgeFilesEntity;
import org.support.project.knowledge.entity.NotifyQueuesEntity;
import org.support.project.knowledge.logic.LogManageLogic;
import org.support.project.knowledge.logic.MailLogic;
import org.support.project.knowledge.logic.UploadedFileLogic;
import org.support.project.web.dao.MailsDao;
import org.support.project.web.entity.MailsEntity;
Expand Down Expand Up @@ -63,7 +64,7 @@ private void start() {

List<MailsEntity> mailsEntities = MailsDao.get().physicalSelectAll();
for (MailsEntity mailsEntity : mailsEntities) {
if (NumberUtils.is(mailsEntity.getStatus(), MailSendBat.MAIL_STATUS_SENDED)) {
if (NumberUtils.is(mailsEntity.getStatus(), MailLogic.MAIL_STATUS_SENDED)) {
// 送信済みになっているものは、削除
MailsDao.get().physicalDelete(mailsEntity);
}
Expand Down
53 changes: 1 addition & 52 deletions src/main/java/org/support/project/knowledge/bat/MailSendBat.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
Expand All @@ -13,12 +12,7 @@
import org.apache.commons.lang.ClassUtils;
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.knowledge.config.AppConfig;
import org.support.project.knowledge.logic.MailLogic;
import org.support.project.web.dao.MailConfigsDao;
import org.support.project.web.dao.MailsDao;
import org.support.project.web.entity.MailConfigsEntity;
import org.support.project.web.entity.MailsEntity;

/**
* メールの送信処理は、時間がかかるため、バッチ処理の中で処理する
Expand All @@ -30,18 +24,6 @@ public class MailSendBat extends AbstractBat {
/** ログ */
private static final Log LOG = LogFactory.getLog(MailSendBat.class);

/** メールの状態:未送信(送信待ち) */
public static final int MAIL_STATUS_UNSENT = 0;
/** メールの状態:送信済 */
public static final int MAIL_STATUS_SENDED = 10;
/** メールの状態:なんらかのエラーが発生した */
public static final int MAIL_STATUS_ERROR = -1;
/** メールの状態:アドレスのフォーマットエラー */
public static final int MAIL_STATUS_FORMAT_ERROR = -2;

public static final String MAIL_FORMAT = "^[a-zA-Z0-9!#$%&'_`/=~\\*\\+\\-\\?\\^\\{\\|\\}]+(\\.[a-zA-Z0-9!#$%&'_`/=~\\*\\+\\-\\?\\^\\{\\|\\}]+)*"
+ "@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]*(\\.[a-zA-Z0-9\\-]+)*$";

public static void main(String[] args) throws Exception {
try {
initLogName("MailSendBat.log");
Expand Down Expand Up @@ -71,40 +53,7 @@ public static void main(String[] args) throws Exception {
*/
public void start() throws UnsupportedEncodingException, MessagingException, InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
return;
}

MailsDao dao = MailsDao.get();
List<MailsEntity> entities = dao.selectOnStatus(MAIL_STATUS_UNSENT);
int count = 0;
for (MailsEntity mailsEntity : entities) {
if (mailsEntity.getToAddress().matches(MAIL_FORMAT)) {
try {
MailLogic.get().mailSend(mailConfigsEntity, mailsEntity);
// ステータス更新
// mailsEntity.setStatus(MAIL_STATUS_SENDED);
// MailsDao.get().save(mailsEntity);
// 送信処理が終われば、物理削除
dao.physicalDelete(mailsEntity);
} catch (Exception e) {
LOG.error("mail send error", e);
//TODO メール送信失敗、二度と送らないようにする(リトライする?)
// 未送信にしておけば、再送できるが永遠に再送してしまう
// カウント制御するべきか?
mailsEntity.setStatus(MAIL_STATUS_ERROR);
MailsDao.get().save(mailsEntity);
}
} else {
mailsEntity.setStatus(MAIL_STATUS_FORMAT_ERROR);
dao.save(mailsEntity);
}
count++;
}
LOG.info("MAIL sended. count: " + count);
MailLogic.get().startSendMails();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private void sendLikeMail(LikesEntity like, KnowledgesEntity knowledge, UsersEnt
MailsEntity mailsEntity = new MailsEntity();
String mailId = idGenu("Notify");
mailsEntity.setMailId(mailId);
mailsEntity.setStatus(MailSendBat.MAIL_STATUS_UNSENT);
mailsEntity.setStatus(MailLogic.MAIL_STATUS_UNSENT);
mailsEntity.setToAddress(user.getMailAddress());
mailsEntity.setToName(user.getUserName());

Expand Down Expand Up @@ -325,7 +325,7 @@ private void sendCommentMail(CommentsEntity comment, KnowledgesEntity knowledge,
MailsEntity mailsEntity = new MailsEntity();
String mailId = idGenu("Notify");
mailsEntity.setMailId(mailId);
mailsEntity.setStatus(MailSendBat.MAIL_STATUS_UNSENT);
mailsEntity.setStatus(MailLogic.MAIL_STATUS_UNSENT);
mailsEntity.setToAddress(user.getMailAddress());
mailsEntity.setToName(user.getUserName());

Expand Down Expand Up @@ -545,7 +545,7 @@ private void insertNotifyKnowledgeUpdateMailQue(KnowledgesEntity knowledge, User
MailsEntity mailsEntity = new MailsEntity();
String mailId = idGenu("Notify");
mailsEntity.setMailId(mailId);
mailsEntity.setStatus(MailSendBat.MAIL_STATUS_UNSENT);
mailsEntity.setStatus(MailLogic.MAIL_STATUS_UNSENT);
mailsEntity.setToAddress(usersEntity.getMailAddress());
mailsEntity.setToName(usersEntity.getUserName());
String title = config.getTitle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.support.project.knowledge.deploy.v1_7_0.Migrate_1_7_0_1;
import org.support.project.knowledge.deploy.v1_8_0.Migrate_1_8_0;
import org.support.project.knowledge.deploy.v1_8_0.Migrate_1_8_1;
import org.support.project.knowledge.deploy.v1_8_0.Migrate_1_8_2;
import org.support.project.web.dao.SystemsDao;
import org.support.project.web.entity.SystemsEntity;

Expand All @@ -37,7 +38,7 @@ public class InitDB {
private static final Map<String, Migrate> MAP = new LinkedHashMap<>();

private static final Migrate INIT = InitializeSystem.get();
public static final String CURRENT = "1.8.1";
public static final String CURRENT = "1.8.2";

public InitDB() {
super();
Expand All @@ -60,6 +61,7 @@ public InitDB() {
MAP.put("1.7.0.1", Migrate_1_7_0_1.get());
MAP.put("1.8.0", Migrate_1_8_0.get());
MAP.put("1.8.1", Migrate_1_8_1.get());
MAP.put("1.8.2", Migrate_1_8_2.get());
}

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.support.project.knowledge.deploy.v1_8_0;

import org.support.project.knowledge.deploy.Migrate;
import org.support.project.ormapping.tool.dao.InitializeDao;

public class Migrate_1_8_2 implements Migrate {

public static Migrate_1_8_2 get() {
return org.support.project.di.Container.getComp(Migrate_1_8_2.class);
}

@Override
public boolean doMigrate() throws Exception {
InitializeDao initializeDao = InitializeDao.get();
String[] sqlpaths = {
"/org/support/project/knowledge/deploy/v1_8_0/migrate3.sql",
};
initializeDao.initializeDatabase(sqlpaths);
return true;
}
}
Loading

0 comments on commit 95e316c

Please sign in to comment.