Commit a1e2f276 authored by Quxl's avatar Quxl

X

parent bdad5e3a
...@@ -20,7 +20,7 @@ public class EmailServiceTest { ...@@ -20,7 +20,7 @@ public class EmailServiceTest {
@Test @Test
public void sendEmailTest() { public void sendEmailTest() {
try { try {
service.sendForsync("测试邮件", "测试邮件"); service.sendForsync(0, "测试邮件", "测试邮件");
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); 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; package com.egolm.pds.schedule;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
...@@ -49,67 +51,86 @@ public class ProductsTask { ...@@ -49,67 +51,86 @@ public class ProductsTask {
private void executeUpdateTask() { private void executeUpdateTask() {
Map<String, String> headers = new HashMap<String, String>(); Map<String, String> headers = new HashMap<String, String>();
headers.put(authKey, authValue); headers.put(authKey, authValue);
while(true) { List<PdsErrDetail> errs = new ArrayList<PdsErrDetail>();
String responseBody = HttpUtil.get(urlQuery, headers); try {
JSONObject jsonObject = JSON.parseObject(responseBody); while(true) {
JSONArray jsonArray = jsonObject.getJSONArray("data"); String responseBody = HttpUtil.get(urlQuery, headers);
if(jsonArray == null || jsonArray.size() == 0) { JSONObject jsonObject = JSON.parseObject(responseBody);
break; JSONArray jsonArray = jsonObject.getJSONArray("data");
} else { if(jsonArray == null || jsonArray.size() == 0) {
for(int i = 0; i < jsonArray.size(); i++) { break;
JSONObject productJsonObject = jsonArray.getJSONObject(i); } else {
String article_description = productJsonObject.getString("article_description"); for(int i = 0; i < jsonArray.size(); i++) {
String article_group_no = productJsonObject.getString("article_group_no"); JSONObject productJsonObject = jsonArray.getJSONObject(i);
String article_short_description = productJsonObject.getString("article_short_description"); String article_description = productJsonObject.getString("article_description");
String article_type = productJsonObject.getString("article_type"); String article_group_no = productJsonObject.getString("article_group_no");
String barcode = productJsonObject.getString("barcode"); String article_short_description = productJsonObject.getString("article_short_description");
String buying_price = productJsonObject.getString("buying_price"); String article_type = productJsonObject.getString("article_type");
String buying_price_with_tax = productJsonObject.getString("buying_price_with_tax"); String barcode = productJsonObject.getString("barcode");
Integer case_qty = productJsonObject.getInteger("case_qty"); String buying_price = productJsonObject.getString("buying_price");
String deduction = productJsonObject.getString("deduction"); String buying_price_with_tax = productJsonObject.getString("buying_price_with_tax");
String made_in = productJsonObject.getString("made_in"); Integer case_qty = productJsonObject.getInteger("case_qty");
String order_way = productJsonObject.getString("order_way"); String deduction = productJsonObject.getString("deduction");
String pack_unit = productJsonObject.getString("pack_unit"); String made_in = productJsonObject.getString("made_in");
String selling_price = productJsonObject.getString("selling_price"); String order_way = productJsonObject.getString("order_way");
String specification = productJsonObject.getString("specification"); String pack_unit = productJsonObject.getString("pack_unit");
String stores = productJsonObject.getString("stores"); String selling_price = productJsonObject.getString("selling_price");
String supplier_article_no = productJsonObject.getString("supplier_article_no"); String specification = productJsonObject.getString("specification");
String supplier_no = productJsonObject.getString("supplier_no"); String stores = productJsonObject.getString("stores");
String tax_rate = productJsonObject.getString("tax_rate"); String supplier_article_no = productJsonObject.getString("supplier_article_no");
String trade_mode = productJsonObject.getString("trade_mode"); String supplier_no = productJsonObject.getString("supplier_no");
String weight_article = productJsonObject.getString("weight_article"); String tax_rate = productJsonObject.getString("tax_rate");
Object transactionId = productJsonObject.get("transaction_id"); String trade_mode = productJsonObject.getString("trade_mode");
String weight_article = productJsonObject.getString("weight_article");
Map<String, Object> productMap = new HashMap<String, Object>(); Object transactionId = productJsonObject.get("transaction_id");
productMap.put("article_description", article_description);
productMap.put("article_group_no", article_group_no); Map<String, Object> productMap = new HashMap<String, Object>();
productMap.put("article_short_description", article_short_description); productMap.put("article_description", article_description);
productMap.put("article_type", article_type); productMap.put("article_group_no", article_group_no);
productMap.put("barcode", barcode); productMap.put("article_short_description", article_short_description);
productMap.put("buying_price", buying_price); productMap.put("article_type", article_type);
productMap.put("buying_price_with_tax", buying_price_with_tax); productMap.put("barcode", barcode);
productMap.put("case_qty", case_qty); productMap.put("buying_price", buying_price);
productMap.put("deduction", deduction); productMap.put("buying_price_with_tax", buying_price_with_tax);
productMap.put("made_in", made_in); productMap.put("case_qty", case_qty);
productMap.put("order_way", order_way); productMap.put("deduction", deduction);
productMap.put("pack_unit", pack_unit); productMap.put("made_in", made_in);
productMap.put("selling_price", selling_price); productMap.put("order_way", order_way);
productMap.put("specification", specification); productMap.put("pack_unit", pack_unit);
productMap.put("stores", stores); productMap.put("selling_price", selling_price);
productMap.put("supplier_article_no", supplier_article_no); productMap.put("specification", specification);
productMap.put("supplier_no", supplier_no); productMap.put("stores", stores);
productMap.put("tax_rate", tax_rate); productMap.put("supplier_article_no", supplier_article_no);
productMap.put("trade_mode", trade_mode); productMap.put("supplier_no", supplier_no);
productMap.put("weight_article", weight_article); productMap.put("tax_rate", tax_rate);
productMap.put("dLastUpdateTime", new Date()); productMap.put("trade_mode", trade_mode);
try { productMap.put("weight_article", weight_article);
pdsService.savePdsProduct(transactionId, productMap);; productMap.put("dLastUpdateTime", new Date());
} catch (Throwable e) { try {
log.error("", e); pdsService.savePdsProduct(transactionId, productMap);;
emailService.sendForasync("PDS TASK ERROR", productJsonObject.toJSONString(), e); } catch (Throwable e) {
log.error("", 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,61 +2,54 @@ package com.egolm.pds.service; ...@@ -2,61 +2,54 @@ package com.egolm.pds.service;
import java.util.Date; import java.util.Date;
import java.util.List; 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.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.egolm.common.DateUtil; import com.egolm.common.DateUtil;
import com.egolm.common.MailUtil; import com.egolm.common.MailUtil;
import com.egolm.pds.bean.MailObject;
import com.egolm.pds.bean.MailConfig; import com.egolm.pds.bean.MailConfig;
import com.egolm.pds.utils.ThrowableUtil; import com.egolm.pds.schedule.ProductsTask.PdsErrDetail;
@Component @Component
public class EmailService { public class EmailService {
@Autowired @Autowired
private MailConfig config; 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) { public void sendPdsErrorDetails(String subject, List<PdsErrDetail> errs) {
StringBuffer stackString = ThrowableUtil.toStackString(e);
String timeString = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"); String timeString = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
MailObject emailObject = new MailObject(); StringBuffer bodyString = new StringBuffer();
emailObject.setSubject(subject); bodyString.append(timeString).append(System.lineSeparator());
emailObject.setContent("<pre>" + timeString + "\r\n" + subject + "\r\n" + content + "\r\n" + stackString.toString() + "</pre>"); for(PdsErrDetail err: errs) {
queue.add(emailObject); 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>");
} }
@Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE) public void sendForsync(int tryCount, String subject, String content) {
private void exec() { try {
while(true) { String host = config.getHost();
MailObject obj = null; Integer port = config.getPort();
try { String username = config.getUsername();
obj = queue.take(); String password = config.getPassword();
this.sendForsync(obj.getSubject(), obj.getContent()); String from = config.getFrom();
} catch (Throwable e) { List<String> to = config.getTo();
log.error("", e); MailUtil.sendBySmtp(host, port, true, username, password, from, subject, content, null, to.toArray(new String[to.size()]));
if(obj != null) { } catch (Exception e) {
queue.add(obj); e.printStackTrace();
if(tryCount > 0) {
try {
Thread.sleep(1000L*60*5);
} catch (InterruptedException e1) {
e1.printStackTrace();
} }
this.sendForsync(--tryCount, subject, content);
} }
} }
} }
public void sendForsync(String subject, String content) {
String host = config.getHost();
Integer port = config.getPort();
String username = config.getUsername();
String password = config.getPassword();
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()]));
}
} }
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