10 Commits

24 changed files with 602 additions and 192 deletions

View File

@@ -2,9 +2,63 @@
本项目基于springboot进行开发实现了一系列的spring-boot-starter可以作为开发中的工具包进行使用。
### 模块划分
- common-spring-boot-starter常用的基础类比如用作消息流转的`Msg`以及一些工具类
- monitor-spring-boot-starter监控工具包可以实现定时监控某些数据并在触发条件后实时通知
- network-spring-boot-starter网络工具实现了一系列http请求发送方法引入后即可快速发送http请求
- notify-spring-boot-starter通知工具集成了邮件通知、短信通知等功能
## 快速开始
### 1. 引入maven依赖
本项目已经上传到中央仓库使用时在pom文件中添加如下依赖即可
- network-spring-boot-starter
```xml
<!--network-spring-boot-starter-->
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>network-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
```
- notify-spring-boot-starter
```xml
<!--notify-spring-boot-starter-->
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>notify-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
```
- monitor-spring-boot-starter
```xml
<!--monitor-spring-boot-starter-->
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>monitor-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
```
- monitor-spring-boot-starter
```xml
<!--monitor-spring-boot-starter-->
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>monitor-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
```

View File

@@ -3,7 +3,7 @@
<parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId>
<version>1.0.2</version>
<version>1.0.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -3,7 +3,7 @@
<parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId>
<version>1.0.2</version>
<version>1.0.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -91,7 +91,7 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
CheckResult checkResult = item.check();
if (checkResult.isTriggered()) {
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
notifyService.notify(checkResult.getNotifyContent(), NotifyType.MAIL_TYPE);
notifyService.notify(checkResult.getNotifyContent(), NotifyType.TYPE_MAIL);
}
}, new CronTrigger(cron)));
}

View File

@@ -3,7 +3,7 @@
<parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId>
<version>1.0.2</version>
<version>1.0.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -277,18 +277,16 @@ public interface HttpService {
*
* @param url 请求地址
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<String> doDelete(String url) throws Exception;
Msg<String> doDelete(String url);
/**
* 发送delete请求不带请求参数
*
* @param url 请求地址
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<HttpEntity> doDeleteWithEntity(String url) throws Exception;
Msg<HttpEntity> doDeleteWithEntity(String url);
/**
* 发送delete请求带请求参数
@@ -296,9 +294,8 @@ public interface HttpService {
* @param url 请求地址
* @param params 请求参数
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<String> doDelete(String url, Map<String, String> params) throws Exception;
Msg<String> doDelete(String url, Map<String, String> params);
/**
@@ -307,9 +304,8 @@ public interface HttpService {
* @param url 请求地址
* @param params 请求参数
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params) throws Exception;
Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params);
interface NetWorkListener<T> {
/**

View File

@@ -3,7 +3,7 @@
<parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId>
<version>1.0.2</version>
<version>1.0.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -0,0 +1,108 @@
package com.hxuanyu.notify.config;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author hxuanyu
*/
@ConfigurationProperties(prefix = "notify.mail")
@ConditionalOnProperty(prefix = "notify.mail", havingValue = "true")
public class MailProperties {
private static final Charset DEFAULT_CHARSET;
private String host;
private Integer port;
private String username;
private String password;
private String protocol = "smtp";
private Charset defaultEncoding;
private Map<String, String> properties;
private String jndiName;
private Integer interval = 10000;
public MailProperties() {
this.defaultEncoding = DEFAULT_CHARSET;
this.properties = new HashMap();
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public Integer getInterval() {
return interval;
}
public void setInterval(Integer interval) {
this.interval = interval;
}
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return this.port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProtocol() {
return this.protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public Charset getDefaultEncoding() {
return this.defaultEncoding;
}
public void setDefaultEncoding(Charset defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}
public Map<String, String> getProperties() {
return this.properties;
}
public void setJndiName(String jndiName) {
this.jndiName = jndiName;
}
public String getJndiName() {
return this.jndiName;
}
static {
DEFAULT_CHARSET = StandardCharsets.UTF_8;
}
}

View File

@@ -1,18 +1,57 @@
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.*;
/**
* @author hxuanyu
*/
@Configuration
@ComponentScan("com.hxuanyu.notify.service")
@ComponentScan("com.hxuanyu.notify")
@EnableConfigurationProperties(MailProperties.class)
public class NotifyConfiguration {
@Autowired
MailProperties mailProperties;
@Bean
JavaMailSenderImpl mailSender(MailProperties properties) {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
this.applyProperties(properties, sender);
return sender;
}
private void applyProperties(MailProperties properties, JavaMailSenderImpl sender) {
sender.setHost(properties.getHost());
if (properties.getPort() != null) {
sender.setPort(properties.getPort());
}
sender.setUsername(properties.getUsername());
sender.setPassword(properties.getPassword());
sender.setProtocol(properties.getProtocol());
if (properties.getDefaultEncoding() != null) {
sender.setDefaultEncoding(properties.getDefaultEncoding().name());
}
if (!properties.getProperties().isEmpty()) {
sender.setJavaMailProperties(this.asProperties(properties.getProperties()));
}
}
private Properties asProperties(Map<String, String> source) {
Properties properties = new Properties();
properties.putAll(source);
return properties;
}
@Bean(name = "mailExecutorService")
public ExecutorService mailExecutorService() {
// 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory

View File

@@ -12,8 +12,9 @@ public enum NotifyType {
/**
* 邮件类型
*/
MAIL_TYPE("邮件"),
SMS_TYPE("短信");
TYPE_LOG("日志输出"),
TYPE_MAIL("邮件"),
TYPE_MSG("短信");
private final String typeName;

View File

@@ -2,18 +2,19 @@ package com.hxuanyu.notify.service.impl;
import com.hxuanyu.notify.common.MailQueue;
import com.hxuanyu.notify.config.MailProperties;
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.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
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;
@@ -27,15 +28,14 @@ import java.util.concurrent.ExecutorService;
*/
@Service
public class MailServiceImpl implements MailService {
@Resource
MailProperties mailProperties;
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
private static boolean isRunning = true;
ExecutorService executor;
@Resource
private JavaMailSender javaMailSender;
private static JavaMailSender javaMailSender;
private static TemplateEngine templateEngine;
private static String defaultFrom;
@@ -45,7 +45,7 @@ public class MailServiceImpl implements MailService {
executor.submit(new PollMail());
}
static class PollMail implements Runnable {
class PollMail implements Runnable {
@Override
public void run() {
while (isRunning) {
@@ -55,7 +55,7 @@ public class MailServiceImpl implements MailService {
if (mail != null) {
//可以设置延时 以及重复校验等等操作
sendMailSync(mail);
Thread.sleep(10000);
Thread.sleep(mailProperties.getInterval());
}
} catch (Exception e) {
e.printStackTrace();
@@ -71,10 +71,14 @@ public class MailServiceImpl implements MailService {
}
private static void sendMailSync(Mail mail) {
private void sendMailSync(Mail mail) {
if (mailProperties == null){
logger.error("未配置邮件信息,请在配置文件中添加配置信息后再发送邮件");
return;
}
String from = mail.getFrom();
if (from == null) {
from = defaultFrom;
from = mailProperties.getUsername();
logger.info("未传入发件人,从配置中读取:{}", from);
}
MimeMessage mimeMessage;
@@ -87,12 +91,7 @@ public class MailServiceImpl implements MailService {
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);
String content = mail.getContent();
// 设置邮件内容
// true 表示开启 html
mimeMessageHelper.setText(content, true);
@@ -112,24 +111,9 @@ public class MailServiceImpl implements MailService {
}
@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;
}
}

View File

@@ -25,10 +25,12 @@ public class NotifyServiceImpl implements NotifyService {
@Override
public void notify(Object content, NotifyType notifyType) {
if (NotifyType.MAIL_TYPE.equals(notifyType)) {
if (NotifyType.TYPE_MAIL.equals(notifyType)) {
sendMail(content);
} else if (NotifyType.SMS_TYPE.equals(notifyType)) {
} else if (NotifyType.TYPE_MSG.equals(notifyType)) {
sendSms(content);
} else if (NotifyType.TYPE_LOG.equals(notifyType)) {
logger.info("新通知:{}", content);
} else {
logger.info("未匹配到通知类型:[{}]", content);
}

View File

@@ -0,0 +1,48 @@
{
"properties": [
{
"name": "notify.mail.host",
"type": "java.lang.String",
"description": "Description for notify.mail.host."
},
{
"name": "notify.mail.protocol",
"type": "java.lang.String",
"description": "Description for notify.mail.protocol."
},
{
"name": "notify.mail.default-encoding",
"type": "java.lang.String",
"description": "Description for notify.mail.default-encoding."
},
{
"name": "notify.mail.password",
"type": "java.lang.String",
"description": "Description for notify.mail.password."
},
{
"name": "notify.mail.username",
"type": "java.lang.String",
"description": "Description for notify.mail.username."
},
{
"name": "notify.mail.port",
"type": "java.lang.String",
"description": "Description for notify.mail.port."
},
{
"name": "notify.mail.properties.mail.debug",
"type": "java.lang.String",
"description": "Description for notify.mail.properties.mail.debug."
},
{
"name": "notify.mail.properties.mail.interval",
"type": "java.lang.String",
"description": "Description for notify.mail.properties.mail.debug."
},
{
"name": "notify.mail.properties.stmp.socketFactory.class",
"type": "java.lang.String",
"description": "Description for notify.mail.properties.stmp.socketFactory.class."
}
] }

View File

@@ -1,129 +0,0 @@
<!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>

View File

@@ -4,7 +4,7 @@
<groupId>com.hxuanyu</groupId>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<version>1.0.2</version>
<version>1.0.4-SNAPSHOT</version>
<name>hxuanyu-spring-boot-starter-parent</name>
<description>MonitorPushingParent</description>
<packaging>pom</packaging>
@@ -19,7 +19,7 @@
<connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection>
<developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection>
<url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url>
<tag>v1.0.2</tag>
<tag>v1.0.0</tag>
</scm>
<developers>
<developer>
@@ -34,6 +34,7 @@
<module>network-spring-boot-starter</module>
<module>notify-spring-boot-starter</module>
<module>monitor-spring-boot-starter</module>
<module>test</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
@@ -165,7 +166,7 @@
<tagNameFormat>v@{project.version}</tagNameFormat>
<autoVersionSubmodules>true</autoVersionSubmodules>
<password>${git.password}</password>
<username>hxuanyu</username>
<username>${git.username}</username>
</configuration>
</plugin>
</plugins>

66
test/pom.xml Normal file
View File

@@ -0,0 +1,66 @@
<?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.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>notify-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>common-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>monitor-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>network-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,17 @@
package com.hxuanyu.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* TODO
*
* @author hanxuanyu
* @version 1.0
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}

View File

@@ -0,0 +1,72 @@
package com.hxuanyu.test.controller;
import com.hxuanyu.common.message.Msg;
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.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 测试控制器
*
* @author hanxuanyu
* @version 1.0
*/
@RestController
public class TestController {
private final Logger logger = LoggerFactory.getLogger(TestController.class);
@Resource
MailService mailService;
@RequestMapping("/testMail")
public Msg<Mail> testMail() {
Mail mail = new Mail();
mail.setContent("邮件内容");
mail.setSubject("邮件主题");
mail.setTo("2252193204@qq.com");
try {
mailService.sendMail(mail);
} catch (InterruptedException e) {
e.printStackTrace();
}
return Msg.success("发送邮件成功", mail);
}
@ResponseBody
@GetMapping("/")
public Msg<String> testGet() {
logger.info("收到GET请求");
return Msg.success("GET 测试");
}
@ResponseBody
@PostMapping("/")
public Msg<String> testPost() {
logger.info("收到POST请求");
return Msg.success("POST 测试");
}
@ResponseBody
@PutMapping("/")
public Msg<String> testPut() {
logger.info("收到PUT请求");
return Msg.success("PUT 测试");
}
@ResponseBody
@DeleteMapping("/")
public Msg<String> testDelete() {
logger.info("收到DELETE请求");
return Msg.success("DELETE 测试");
}
}

View File

@@ -0,0 +1,20 @@
package com.hxuanyu.test.monitor;
import com.hxuanyu.monitor.annotation.MonitorItem;
import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.common.CheckResult;
import com.hxuanyu.notify.enums.NotifyType;
/**
* 定时任务监控测试
*
* @author hxuanyu
*/
@MonitorItem(cron = "0/5 * * * * *")
public class TestMonitorItem extends BaseMonitorItem {
@Override
public CheckResult check() {
return CheckResult.triggered("Hello", NotifyType.TYPE_LOG);
}
}

View File

@@ -1,10 +1,10 @@
spring:
notify:
mail:
host: smtp.domain
host: smtp.qq.com
protocol: smtp
default-encoding: UTF-8
password: your_password
username: your_username
password: your pwd
username: your account
port: 587
properties:
mail:
@@ -12,3 +12,4 @@ spring:
stmp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
interval: 5000

View File

@@ -0,0 +1,22 @@
package com.hxuanyu.starter.test.common;
import com.hxuanyu.common.message.Msg;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MsgTest {
private final Logger logger = LoggerFactory.getLogger(MsgTest.class);
@Test
public void test() {
Msg<String> msg = Msg.success("测试内容", "测试体");
logger.info(msg.toString());
assert msg.isSuccess();
msg = Msg.failed("失败消息");
logger.info(msg.toString());
assert msg.isFailed();
}
}

View File

@@ -0,0 +1,25 @@
package com.hxuanyu.starter.test.monitor;
import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = MainApplication.class)
public class MonitorTest {
private final Logger logger = LoggerFactory.getLogger(MonitorTest.class);
@Autowired
MonitorItemBeanManager monitorItemBeanManager;
@Test
public void testMonitorItemScan() {
BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem");
assert testMonitorItem.getMonitorItemName() != null;
logger.info("获取到的Bean{}", testMonitorItem.toString());
}
}

View File

@@ -0,0 +1,56 @@
package com.hxuanyu.starter.test.network;
import com.hxuanyu.common.message.Msg;
import com.hxuanyu.network.service.HttpService;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.web.WebAppConfiguration;
import static java.lang.Thread.sleep;
@SpringBootTest(classes = MainApplication.class)
public class NetworkTest {
@Autowired
HttpService httpService;
private final Logger logger = LoggerFactory.getLogger(NetworkTest.class);
@Test
@Timeout(3000)
public void testGet() throws InterruptedException {
Msg<String> msg = httpService.doGet("https://baidu.com");
logger.info("GET测试{}", msg);
assert msg.isSuccess();
}
@Test
@Timeout(3000)
public void testPost() {
Msg<String> msg = httpService.doPost("https://baidu.com");
logger.info("POST测试{}", msg);
assert msg.isSuccess();
}
@Test
@Timeout(3000)
public void testDelete() {
Msg<String> msg = httpService.doDelete("https://baidu.com");
logger.info("DELETE测试{}", msg);
assert msg.isSuccess();
}
@Test
@Timeout(3000)
public void testPut() {
Msg<String> msg = httpService.doPut("https://baidu.com");
logger.info("PUT测试{}", msg);
assert msg.isSuccess();
}
}

View File

@@ -0,0 +1,27 @@
package com.hxuanyu.starter.test.notify;
import com.hxuanyu.notify.enums.NotifyType;
import com.hxuanyu.notify.model.Mail;
import com.hxuanyu.notify.service.NotifyService;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static java.lang.Thread.sleep;
@SpringBootTest(classes = MainApplication.class)
public class NotifyTest {
@Autowired
NotifyService notifyService;
@Test
public void testNotify() throws InterruptedException {
notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
sleep(2000);
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
}
}