Commit eac46d03 authored by Quxl's avatar Quxl

x

parent 8cb03b92
package com.egolm.pds.schedule;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egolm.common.DateUtil;
import com.egolm.pds.config.HttpUtil;
//@Component
public class PriceTask {
private static final Log log = LogFactory.getLog(PriceTask.class);
@Value("${pds.auth_key}")
private String authKey;
@Value("${pds.auth_value}")
private String authValue;
@Value("${pds.url_query}")
private String urlQuery;
@Scheduled(cron="${pds.cron.price_download}")
public void execute() {
log.debug(DateUtil.format(new Date()) + " PdsProductsUpdateTask RUN");
boolean boo = true;
int loopCount = 0;
while(boo) {
boo = executeUpdateTask() > 0 && loopCount++ < 10;
}
}
private int executeUpdateTask() {
Map<String, String> headers = new HashMap<String, String>();
headers.put(authKey, authValue);
String responseBody = HttpUtil.get(urlQuery, headers);
JSONObject jsonObject = JSON.parseObject(responseBody);
JSONArray jsonArray = jsonObject.getJSONArray("data");
if(jsonArray != null) {
for(int i = 0; i < jsonArray.size(); i++) {
JSONObject priceObject = jsonArray.getJSONObject(i);
String barcode = priceObject.getString("barcode");
Integer contract_id = priceObject.getInteger("contract_id");
String effective_date = priceObject.getString("effective_date");
String effective_end_date = priceObject.getString("effective_end_date");
String new_buying_price_with_tax = priceObject.getString("new_buying_price_with_tax");
String new_selling_price_with_tax = priceObject.getString("new_selling_price_with_tax");
String price_change_type = priceObject.getString("price_change_type");
Map<String, Object> map = new HashMap<String, Object>();
map.put("barcode", barcode);
map.put("contract_id", contract_id);
map.put("effective_date", effective_date);
map.put("effective_end_date", effective_end_date);
map.put("new_buying_price_with_tax", new_buying_price_with_tax);
map.put("new_selling_price_with_tax", new_selling_price_with_tax);
map.put("price_change_type", price_change_type);
log.debug(JSON.toJSONString(map));
}
}
return jsonArray == null ? 0 : jsonArray.size();
}
}
......@@ -16,6 +16,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egolm.common.DateUtil;
import com.egolm.pds.config.HttpUtil;
import com.egolm.pds.schedule.service.EmailService;
import com.egolm.pds.schedule.service.PdsService;
@Component
......@@ -26,6 +27,9 @@ public class ProductsTask {
@Autowired
private PdsService pdsService;
@Autowired
private EmailService emailService;
@Value("${pds.auth_key}")
private String authKey;
......@@ -100,8 +104,9 @@ public class ProductsTask {
productMap.put("dLastUpdateTime", new Date());
try {
pdsService.savePdsProduct(transactionId, productMap);;
} catch (Exception e) {
} catch (Throwable e) {
log.error("", e);
emailService.appendToQueue("PDS TASK ERROR", productJsonObject.toJSONString(), e);
}
}
}
......
package com.egolm.pds.schedule.service;
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.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.egolm.common.MailUtil;
import com.egolm.pds.utils.ThrowableUtil;
@Component
public class EmailService {
@Value("${mail.to}") private List<String> to;
@Value("${mail.host}") private String host;
@Value("${mail.port}") private Integer port;
@Value("${mail.from}") private String from;
@Value("${mail.username}") private String username;
@Value("${mail.password}") private String password;
private static final Log log = LogFactory.getLog(EmailService.class);
private static final LinkedBlockingQueue<EmailObject> queue = new LinkedBlockingQueue<EmailObject>();
public void appendToQueue(String subject, String content, Throwable e) {
StringBuffer stackString = ThrowableUtil.toStackString(e);
EmailObject emailObject = new EmailObject();
emailObject.subject = subject;
emailObject.content = content + "\r\n" + stackString.toString();
queue.add(emailObject);
}
@Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
private void exec() {
while(true) {
EmailObject obj = null;
try {
obj = queue.take();
String subject = obj.subject;
String content = obj.content;
MailUtil.sendBySmtps(host, port, username, password, from, subject, content, null, to.toArray(new String[to.size()]));
} catch (Throwable e) {
log.error("", e);
if(obj != null) {
queue.add(obj);
}
}
}
}
private static class EmailObject {
String subject;
String content;
}
}
package com.egolm.pds.utils;
public class ThrowableUtil {
public static StringBuffer toStackString(Throwable ex) {
StringBuffer stack = new StringBuffer();
if(ex != null) {
stack.append(ex.getClass().getName()).append(": ").append(ex.getMessage()).append(System.lineSeparator());
StackTraceElement[] elms = ex.getStackTrace();
for(StackTraceElement elm : elms) {
stack.append("\t").append(elm.getClassName()).append(".").append(elm.getMethodName()).append("(").append(elm.getFileName()).append(" ").append(elm.getLineNumber()).append(")\n");
}
if(ex.getCause() != null) {
stack.append("\n\n").append("Cause By: \n").append(toStackString(ex.getCause()));
}
for(Throwable e : ex.getSuppressed()) {
stack.append("\n\n").append("Suppressed By: \n").append(toStackString(e));
}
}
return stack;
}
}
......@@ -29,3 +29,13 @@ pds:
price:
url_query: https://pztafj5g87.execute-api.ap-southeast-1.amazonaws.com/test/sunshine/price_changes
url_update: https://pztafj5g87.execute-api.ap-southeast-1.amazonaws.com/test/sunshine/confirm
mail:
to:
- customerservice@powere2e.net
- hanxu@linkfern.com
- qiu.zhihua@linkfern.com
from: customerservice@powere2e.net
host: mail.powere2e.net
port: 587
username: customerservice@powere2e.net
password: CSpowere2e/.
\ No newline at end of file
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