Commit 06acd152 authored by Quxl's avatar Quxl

x

parent eac46d03
...@@ -45,7 +45,6 @@ ...@@ -45,7 +45,6 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
......
...@@ -106,7 +106,7 @@ public class ProductsTask { ...@@ -106,7 +106,7 @@ public class ProductsTask {
pdsService.savePdsProduct(transactionId, productMap);; pdsService.savePdsProduct(transactionId, productMap);;
} catch (Throwable e) { } catch (Throwable e) {
log.error("", 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; package com.egolm.pds.schedule.service;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.egolm.common.DateUtil;
import com.egolm.common.MailUtil; import com.egolm.common.MailUtil;
import com.egolm.pds.utils.ThrowableUtil; import com.egolm.pds.utils.ThrowableUtil;
@Component @Component
public class EmailService { public class EmailService {
@Value("${mail.to}") private List<String> to; @Autowired
@Value("${mail.host}") private String host; private MailConfig config;
@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 Log log = LogFactory.getLog(EmailService.class);
private static final LinkedBlockingQueue<EmailObject> queue = new LinkedBlockingQueue<EmailObject>(); 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); StringBuffer stackString = ThrowableUtil.toStackString(e);
String timeString = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
EmailObject emailObject = new EmailObject(); EmailObject emailObject = new EmailObject();
emailObject.subject = subject; 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); queue.add(emailObject);
} }
...@@ -39,9 +37,7 @@ public class EmailService { ...@@ -39,9 +37,7 @@ public class EmailService {
EmailObject obj = null; EmailObject obj = null;
try { try {
obj = queue.take(); obj = queue.take();
String subject = obj.subject; this.sendForsync(obj.subject, obj.content);
String content = obj.content;
MailUtil.sendBySmtps(host, port, username, password, from, subject, content, null, to.toArray(new String[to.size()]));
} catch (Throwable e) { } catch (Throwable e) {
log.error("", e); log.error("", e);
if(obj != null) { if(obj != null) {
...@@ -51,6 +47,16 @@ public class EmailService { ...@@ -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 { private static class EmailObject {
String subject; 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: ...@@ -31,9 +31,7 @@ pds:
url_update: https://pztafj5g87.execute-api.ap-southeast-1.amazonaws.com/test/sunshine/confirm url_update: https://pztafj5g87.execute-api.ap-southeast-1.amazonaws.com/test/sunshine/confirm
mail: mail:
to: to:
- customerservice@powere2e.net - qu.xinliang@linkfern.com
- hanxu@linkfern.com
- qiu.zhihua@linkfern.com
from: customerservice@powere2e.net from: customerservice@powere2e.net
host: mail.powere2e.net host: mail.powere2e.net
port: 587 port: 587
......
...@@ -29,4 +29,13 @@ pds: ...@@ -29,4 +29,13 @@ pds:
price: price:
url_query: https://api.goodsmaster.com/sunshine/price_changes url_query: https://api.goodsmaster.com/sunshine/price_changes
url_update: https://api.goodsmaster.com/sunshine/price_changes/confirm url_update: https://api.goodsmaster.com/sunshine/price_changes/confirm
mail:
\ No newline at end of file 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