Commit 06acd152 authored by Quxl's avatar Quxl

x

parent eac46d03
......@@ -45,7 +45,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
......
......@@ -106,7 +106,7 @@ public class ProductsTask {
pdsService.savePdsProduct(transactionId, productMap);;
} catch (Throwable e) {
log.error("", e);
emailService.appendToQueue("PDS TASK ERROR", productJsonObject.toJSONString(), e);
emailService.sendForasync("PDS TASK ERROR", productJsonObject.toJSONString(), e);
}
}
}
......
package com.egolm.pds.schedule.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.Value;
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.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;
@Autowired
private MailConfig config;
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) {
public void sendForasync(String subject, String content, Throwable e) {
StringBuffer stackString = ThrowableUtil.toStackString(e);
String timeString = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
EmailObject emailObject = new EmailObject();
emailObject.subject = subject;
emailObject.content = content + "\r\n" + stackString.toString();
emailObject.content = timeString + "\r\n" + subject + "\r\n" + content + "\r\n" + stackString.toString();
queue.add(emailObject);
}
......@@ -39,9 +37,7 @@ public class EmailService {
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()]));
this.sendForsync(obj.subject, obj.content);
} catch (Throwable e) {
log.error("", e);
if(obj != null) {
......@@ -51,6 +47,16 @@ public class EmailService {
}
}
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()]));
}
private static class EmailObject {
String subject;
......
package com.egolm.pds.schedule.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import com.egolm.pds.PDSApplication;
@ActiveProfiles("dev")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PDSApplication.class)
public class EmailServiceTest {
@Autowired
private EmailService service;
@Test
public void sendEmailTest() {
try {
service.sendForsync("测试邮件", "测试邮件");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
package com.egolm.pds.schedule.service;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "mail")
public class MailConfig {
private List<String> to;
private String host;
private Integer port;
private String from;
private String username;
private String password;
public List<String> getTo() {
return to;
}
public String getHost() {
return host;
}
public Integer getPort() {
return port;
}
public String getFrom() {
return from;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setTo(List<String> to) {
this.to = to;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(Integer port) {
this.port = port;
}
public void setFrom(String from) {
this.from = from;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
......@@ -31,9 +31,7 @@ pds:
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
- qu.xinliang@linkfern.com
from: customerservice@powere2e.net
host: mail.powere2e.net
port: 587
......
......@@ -29,4 +29,13 @@ pds:
price:
url_query: https://api.goodsmaster.com/sunshine/price_changes
url_update: https://api.goodsmaster.com/sunshine/price_changes/confirm
\ No newline at end of file
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