Commit a1e2f276 authored by Quxl's avatar Quxl

X

parent bdad5e3a
......@@ -20,7 +20,7 @@ public class EmailServiceTest {
@Test
public void sendEmailTest() {
try {
service.sendForsync("测试邮件", "测试邮件");
service.sendForsync(0, "测试邮件", "测试邮件");
} catch (Throwable e) {
e.printStackTrace();
}
......
package com.egolm.pds.bean;
public class MailObject {
private String subject;
private String content;
public String getSubject() {
return subject;
}
public String getContent() {
return content;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setContent(String content) {
this.content = content;
}
}
package com.egolm.pds.schedule;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
......@@ -49,6 +51,8 @@ public class ProductsTask {
private void executeUpdateTask() {
Map<String, String> headers = new HashMap<String, String>();
headers.put(authKey, authValue);
List<PdsErrDetail> errs = new ArrayList<PdsErrDetail>();
try {
while(true) {
String responseBody = HttpUtil.get(urlQuery, headers);
JSONObject jsonObject = JSON.parseObject(responseBody);
......@@ -106,10 +110,27 @@ public class ProductsTask {
pdsService.savePdsProduct(transactionId, productMap);;
} catch (Throwable e) {
log.error("", e);
emailService.sendForasync("PDS TASK ERROR", productJsonObject.toJSONString(), e);
PdsErrDetail err = new PdsErrDetail(JSON.toJSONString(productJsonObject), e);
errs.add(err);
}
}
}
}
} catch (Exception e) {
log.error("", e);
PdsErrDetail err = new PdsErrDetail(null, e);
errs.add(err);
} finally {
emailService.sendPdsErrorDetails("PDS TASK ERROR", errs);
}
}
public static class PdsErrDetail {
public final String data;
public final Throwable e;
public PdsErrDetail(String data, Throwable e) {
this.data = data;
this.e = e;
}
}
......
......@@ -2,54 +2,35 @@ package com.egolm.pds.service;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.egolm.common.DateUtil;
import com.egolm.common.MailUtil;
import com.egolm.pds.bean.MailObject;
import com.egolm.pds.bean.MailConfig;
import com.egolm.pds.utils.ThrowableUtil;
import com.egolm.pds.schedule.ProductsTask.PdsErrDetail;
@Component
public class EmailService {
@Autowired
private MailConfig config;
private static final Log log = LogFactory.getLog(EmailService.class);
private static final LinkedBlockingQueue<MailObject> queue = new LinkedBlockingQueue<MailObject>();
public void sendForasync(String subject, String content, Throwable e) {
StringBuffer stackString = ThrowableUtil.toStackString(e);
public void sendPdsErrorDetails(String subject, List<PdsErrDetail> errs) {
String timeString = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
MailObject emailObject = new MailObject();
emailObject.setSubject(subject);
emailObject.setContent("<pre>" + timeString + "\r\n" + subject + "\r\n" + content + "\r\n" + stackString.toString() + "</pre>");
queue.add(emailObject);
}
@Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
private void exec() {
while(true) {
MailObject obj = null;
try {
obj = queue.take();
this.sendForsync(obj.getSubject(), obj.getContent());
} catch (Throwable e) {
log.error("", e);
if(obj != null) {
queue.add(obj);
}
}
StringBuffer bodyString = new StringBuffer();
bodyString.append(timeString).append(System.lineSeparator());
for(PdsErrDetail err: errs) {
bodyString.append(err.data).append(System.lineSeparator());
bodyString.append(err.e.getMessage()).append(System.lineSeparator());
}
bodyString.append(System.lineSeparator()).append(System.lineSeparator()).append(System.lineSeparator());
this.sendForsync(5, subject, "<pre>" + bodyString.toString() + "</pre>");
}
public void sendForsync(String subject, String content) {
public void sendForsync(int tryCount, String subject, String content) {
try {
String host = config.getHost();
Integer port = config.getPort();
String username = config.getUsername();
......@@ -57,6 +38,18 @@ public class EmailService {
String from = config.getFrom();
List<String> to = config.getTo();
MailUtil.sendBySmtp(host, port, true, username, password, from, subject, content, null, to.toArray(new String[to.size()]));
} catch (Exception e) {
e.printStackTrace();
if(tryCount > 0) {
try {
Thread.sleep(1000L*60*5);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
this.sendForsync(--tryCount, subject, content);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment