项目迁移
This commit is contained in:
36
notify-spring-boot-starter/pom.xml
Normal file
36
notify-spring-boot-starter/pom.xml
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>notify-spring-boot-starter</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<!--邮件发送-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,78 @@
|
||||
package com.hxuanyu.notify.common;
|
||||
|
||||
|
||||
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* 邮件队列
|
||||
*
|
||||
* @author 22521
|
||||
*/
|
||||
public class MailQueue {
|
||||
/**
|
||||
* 队列大小
|
||||
*/
|
||||
static final int QUEUE_MAX_SIZE = 1000;
|
||||
|
||||
static BlockingQueue<Mail> blockingQueue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE);
|
||||
|
||||
/**
|
||||
* 私有的默认构造子,保证外界无法直接实例化
|
||||
*/
|
||||
private MailQueue() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例
|
||||
* 没有绑定关系,而且只有被调用到才会装载,从而实现了延迟加载
|
||||
*/
|
||||
private static class SingletonHolder {
|
||||
/**
|
||||
* 静态初始化器,由JVM来保证线程安全
|
||||
*/
|
||||
private static final MailQueue MAIL_QUEUE = new MailQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 单例队列
|
||||
*
|
||||
* @return 队列实例
|
||||
*/
|
||||
public static MailQueue getMailQueue() {
|
||||
return SingletonHolder.MAIL_QUEUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生产者入队
|
||||
*
|
||||
* @param mail 邮件
|
||||
* @throws InterruptedException 异常
|
||||
*/
|
||||
public void produce(Mail mail) throws InterruptedException {
|
||||
blockingQueue.put(mail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消费出队
|
||||
*
|
||||
* @return 邮件
|
||||
* @throws InterruptedException 异常
|
||||
*/
|
||||
public Mail consume() throws InterruptedException {
|
||||
return blockingQueue.take();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取队列大小
|
||||
*
|
||||
* @return 队列大小
|
||||
*/
|
||||
public int size() {
|
||||
return blockingQueue.size();
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.hxuanyu.notify.config;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* @author hxuanyu
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan("com.hxuanyu.notify.service")
|
||||
public class NotifyConfiguration {
|
||||
@Bean(name = "mailExecutorService")
|
||||
public ExecutorService mailExecutorService() {
|
||||
// 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory
|
||||
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
|
||||
.setNameFormat("Mail-pool-%d").build();
|
||||
|
||||
// 创建线程池,其中任务队列需要结合实际情况设置合理的容量
|
||||
return new ThreadPoolExecutor(1,
|
||||
1,
|
||||
0L,
|
||||
TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<>(1024),
|
||||
namedThreadFactory,
|
||||
new ThreadPoolExecutor.AbortPolicy());
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.hxuanyu.notify.enums;
|
||||
|
||||
/**
|
||||
* 通知类型,目前只有邮件
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
public enum NotifyType {
|
||||
|
||||
|
||||
/**
|
||||
* 邮件类型
|
||||
*/
|
||||
MAIL_TYPE("邮件"),
|
||||
SMS_TYPE("短信");
|
||||
|
||||
private final String typeName;
|
||||
|
||||
NotifyType(String typeName) {
|
||||
this.typeName = typeName;
|
||||
}
|
||||
|
||||
public String getTypeName() {
|
||||
return typeName;
|
||||
}
|
||||
}
|
@@ -0,0 +1,108 @@
|
||||
package com.hxuanyu.notify.model;
|
||||
|
||||
/**
|
||||
* 邮件
|
||||
*
|
||||
* @author hxuanyu
|
||||
*/
|
||||
public class Mail {
|
||||
private String from;
|
||||
private String to;
|
||||
private String cc;
|
||||
private String subject;
|
||||
private String content;
|
||||
|
||||
public Mail() {
|
||||
}
|
||||
|
||||
public Mail(String from, String to, String cc, String subject, String content) {
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
this.cc = cc;
|
||||
this.subject = subject;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不传入发送者时使用默认配置好的发件人
|
||||
*
|
||||
* @param to 收件人
|
||||
* @param cc 抄送人
|
||||
* @param subject 主题
|
||||
* @param content 邮件内容
|
||||
*/
|
||||
public Mail(String to, String cc, String subject, String content) {
|
||||
this.from = null;
|
||||
this.to = to;
|
||||
this.cc = cc;
|
||||
this.subject = subject;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 不传入发送者时使用默认配置好的发件人
|
||||
*
|
||||
* @param to 收件人
|
||||
* @param subject 主题
|
||||
* @param content 邮件内容
|
||||
*/
|
||||
public Mail(String to, String subject, String content) {
|
||||
this.from = null;
|
||||
this.to = to;
|
||||
this.cc = to;
|
||||
this.subject = subject;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getCc() {
|
||||
return cc;
|
||||
}
|
||||
|
||||
public void setCc(String cc) {
|
||||
this.cc = cc;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MailBean{" +
|
||||
"from='" + from + '\'' +
|
||||
", to='" + to + '\'' +
|
||||
", cc='" + cc + '\'' +
|
||||
", subject='" + subject + '\'' +
|
||||
", content='" + content + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.hxuanyu.notify.service;
|
||||
|
||||
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
|
||||
/**
|
||||
* 邮件服务
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface MailService {
|
||||
|
||||
/**
|
||||
* 发送简单邮件
|
||||
*
|
||||
* @param mail 发件人
|
||||
* @throws InterruptedException 异常
|
||||
*/
|
||||
void sendMail(Mail mail) throws InterruptedException;
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.hxuanyu.notify.service;
|
||||
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 通知服务
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface NotifyService {
|
||||
/**
|
||||
* 向用户发送通知
|
||||
*
|
||||
* @param content 通知内容
|
||||
* @param notifyType 通知类型
|
||||
*/
|
||||
void notify(Object content, NotifyType notifyType);
|
||||
}
|
@@ -0,0 +1,135 @@
|
||||
package com.hxuanyu.notify.service.impl;
|
||||
|
||||
|
||||
import com.hxuanyu.notify.common.MailQueue;
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
import com.hxuanyu.notify.service.MailService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.context.Context;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
/**
|
||||
* @author 22521
|
||||
*/
|
||||
@Service
|
||||
public class MailServiceImpl implements MailService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
|
||||
private static boolean isRunning = true;
|
||||
ExecutorService executor;
|
||||
|
||||
private static JavaMailSender javaMailSender;
|
||||
private static TemplateEngine templateEngine;
|
||||
|
||||
|
||||
private static String defaultFrom;
|
||||
|
||||
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void startThread() {
|
||||
executor.submit(new PollMail());
|
||||
}
|
||||
|
||||
static class PollMail implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
while (isRunning) {
|
||||
try {
|
||||
logger.info("剩余邮件总数:{}", MailQueue.getMailQueue().size());
|
||||
Mail mail = MailQueue.getMailQueue().consume();
|
||||
if (mail != null) {
|
||||
//可以设置延时 以及重复校验等等操作
|
||||
sendMailSync(mail);
|
||||
Thread.sleep(10000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sendMail(Mail mail) throws InterruptedException {
|
||||
MailQueue.getMailQueue().produce(mail);
|
||||
}
|
||||
|
||||
|
||||
private static void sendMailSync(Mail mail) {
|
||||
String from = mail.getFrom();
|
||||
if (from == null) {
|
||||
from = defaultFrom;
|
||||
logger.info("未传入发件人,从配置中读取:{}", from);
|
||||
}
|
||||
MimeMessage mimeMessage;
|
||||
|
||||
try {
|
||||
mimeMessage = javaMailSender.createMimeMessage();
|
||||
// true 表示多部分,可添加内联资源
|
||||
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
||||
// 设置邮件信息
|
||||
mimeMessageHelper.setFrom(from);
|
||||
mimeMessageHelper.setTo(mail.getTo());
|
||||
mimeMessageHelper.setSubject(mail.getSubject());
|
||||
// 利用 Thymeleaf 引擎渲染 HTML
|
||||
Context context = new Context();
|
||||
// 设置注入的变量
|
||||
context.setVariable("templates/mail", mail);
|
||||
// 模板设置为 "mail"
|
||||
String content = templateEngine.process("templates/mail/mail", context);
|
||||
// 设置邮件内容
|
||||
// true 表示开启 html
|
||||
mimeMessageHelper.setText(content, true);
|
||||
javaMailSender.send(mimeMessage);
|
||||
} catch (MessagingException e) {
|
||||
logger.error("发送邮件出错:" + e.getMessage() + e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
public static void setIsRunning(boolean isRunning) {
|
||||
MailServiceImpl.isRunning = isRunning;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stopThread() {
|
||||
logger.info("destroy");
|
||||
}
|
||||
|
||||
|
||||
@Resource
|
||||
public void setJavaMailSender(JavaMailSender javaMailSender) {
|
||||
MailServiceImpl.javaMailSender = javaMailSender;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setExecutor(@Qualifier("mailExecutorService") ExecutorService executor) {
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
|
||||
@Autowired
|
||||
public void setTemplateEngine(TemplateEngine templateEngine) {
|
||||
MailServiceImpl.templateEngine = templateEngine;
|
||||
}
|
||||
|
||||
@Value("${spring.mail.username}")
|
||||
public void setDefaultFrom(String defaultFrom) {
|
||||
MailServiceImpl.defaultFrom = defaultFrom;
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
package com.hxuanyu.notify.service.impl;
|
||||
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
import com.hxuanyu.notify.service.MailService;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 通知服务
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class NotifyServiceImpl implements NotifyService {
|
||||
private final Logger logger = LoggerFactory.getLogger(NotifyServiceImpl.class);
|
||||
|
||||
@Resource
|
||||
MailService mailService;
|
||||
|
||||
@Override
|
||||
public void notify(Object content, NotifyType notifyType) {
|
||||
if (NotifyType.MAIL_TYPE.equals(notifyType)) {
|
||||
sendMail(content);
|
||||
} else if (NotifyType.SMS_TYPE.equals(notifyType)) {
|
||||
sendSms(content);
|
||||
} else {
|
||||
logger.info("未匹配到通知类型:[{}]", content);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendSms(Object content) {
|
||||
logger.info("即将发送短信通知,通知内容:{}", content);
|
||||
}
|
||||
|
||||
private void sendMail(Object content) {
|
||||
logger.info("即将发送邮件通知,通知内容:{}", content);
|
||||
if (content instanceof Mail) {
|
||||
try {
|
||||
mailService.sendMail((Mail) content);
|
||||
} catch (InterruptedException e) {
|
||||
logger.warn("邮件发送失败:{}", e.getLocalizedMessage());
|
||||
}
|
||||
} else {
|
||||
logger.warn("通知内容格式不符");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hxuanyu.notify.config.NotifyConfiguration
|
@@ -0,0 +1,14 @@
|
||||
spring:
|
||||
mail:
|
||||
host: smtp.domain
|
||||
protocol: smtp
|
||||
default-encoding: UTF-8
|
||||
password: your_password
|
||||
username: your_username
|
||||
port: 587
|
||||
properties:
|
||||
mail:
|
||||
debug: false
|
||||
stmp:
|
||||
socketFactory:
|
||||
class: javax.net.ssl.SSLSocketFactory
|
@@ -0,0 +1,129 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
|
||||
<!--${government}-->
|
||||
<!--${title}-->
|
||||
<!--${suggestion}-->
|
||||
<!--${deadline}-->
|
||||
<!--${secret}-->
|
||||
<!--${url}-->
|
||||
<!--${officeName}-->
|
||||
<!--${createTime}-->
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
|
||||
<title>${title}</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
|
||||
<style>
|
||||
.contact-clean {
|
||||
background: #f1f7fc;
|
||||
padding: 80px 0;
|
||||
}
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
@media (max-width: 767px) {
|
||||
.contact-clean {
|
||||
padding: 20px 0;
|
||||
}
|
||||
}
|
||||
|
||||
.contact-clean form {
|
||||
max-width: 480px;
|
||||
width: 90%;
|
||||
margin: 0 auto;
|
||||
background-color: #ffffff;
|
||||
padding: 40px;
|
||||
border-radius: 4px;
|
||||
color: #505e6c;
|
||||
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
@media (max-width: 767px) {
|
||||
.contact-clean form {
|
||||
padding: 30px;
|
||||
}
|
||||
}
|
||||
|
||||
.contact-clean h2 {
|
||||
margin-top: 5px;
|
||||
font-weight: bold;
|
||||
font-size: 28px;
|
||||
margin-bottom: 36px;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.contact-clean .form-group:last-child {
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.contact-clean form .form-control {
|
||||
background: #fff;
|
||||
border-radius: 2px;
|
||||
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.05);
|
||||
outline: none;
|
||||
color: inherit;
|
||||
padding-left: 12px;
|
||||
height: 42px;
|
||||
}
|
||||
|
||||
.contact-clean form .form-control:focus {
|
||||
border: 1px solid #b2b2b2;
|
||||
}
|
||||
|
||||
.contact-clean form textarea.form-control {
|
||||
min-height: 100px;
|
||||
max-height: 260px;
|
||||
padding-top: 10px;
|
||||
resize: vertical;
|
||||
}
|
||||
|
||||
.contact-clean form .btn {
|
||||
padding: 16px 32px;
|
||||
border: none;
|
||||
background: none;
|
||||
box-shadow: none;
|
||||
text-shadow: none;
|
||||
opacity: 0.9;
|
||||
text-transform: uppercase;
|
||||
font-weight: bold;
|
||||
font-size: 13px;
|
||||
letter-spacing: 0.4px;
|
||||
line-height: 1;
|
||||
outline: none !important;
|
||||
}
|
||||
|
||||
.contact-clean form .btn:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.contact-clean form .btn:active {
|
||||
transform: translateY(1px);
|
||||
}
|
||||
|
||||
.contact-clean form .btn-primary {
|
||||
background-color: #055ada !important;
|
||||
margin-top: 15px;
|
||||
color: #fff;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="contact-clean">
|
||||
<form method="post">
|
||||
<h2 class="text-center" th:text="${mail.getSubject()}"></h2>
|
||||
<p th:utext="${mail.getContent()}"></p>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<span>
|
||||
<strong>注意</strong>本邮件由系统自动发送,请勿回复本邮件,如果邮件内容您并不知情,请忽略本邮件
|
||||
</span>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
Reference in New Issue
Block a user