Skip to content

Commit 50f01d1

Browse files
author
litongjava
committed
add notification
1 parent 45884d1 commit 50f01d1

File tree

4 files changed

+307
-0
lines changed

4 files changed

+307
-0
lines changed

docs/zh/06_内置组件/13.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 邮箱组件
2+
3+
tio-boot 内置了邮箱工具类 com.litongjava.tio.utils.email.EMailUtils 用于支持发送邮件
4+
5+
## 发送邮件
6+
7+
app.properties
8+
9+
```
10+
mail.host=smtp.126.com
11+
mail.protocol=smtp
12+
mail.smpt.port=465
13+
14+
mail.password=g8NmXf6hcy9NexE
15+
mail.from=litongjava_alarm
16+
```
17+
18+
测试发送
19+
20+
```
21+
String filename = "app.properties";
22+
EnvironmentUtils.load(filename);
23+
EMailUtils.send("[email protected]", "验证码", "欢迎你登录XXX系统,你的验证码是 1234");
24+
```
25+
26+
默认整合的邮箱是 网易 126 邮箱服务
27+
28+
## 使用自定义邮箱
29+
30+
- 1. 实现类 com.litongjava.tio.utils.email.EMail 重写 send 方法
31+
- 2.实现类 com.litongjava.tio.utils.email.IEMailFactory 重写 getMail 方法 返回 Mail
32+
- 3. 调用 com.litongjava.tio.utils.email.EMailUtils.setEMailFactory(IEMailFactory) 设置实现类
33+
34+
```
35+
package com.litongjava.tio.utils.email;
36+
37+
import java.util.Properties;
38+
39+
import javax.mail.Message;
40+
import javax.mail.Session;
41+
import javax.mail.Transport;
42+
import javax.mail.internet.InternetAddress;
43+
import javax.mail.internet.MimeMessage;
44+
45+
import com.litongjava.tio.utils.IoUtils;
46+
import com.litongjava.tio.utils.environment.EnvironmentUtils;
47+
48+
public class Wangyi126Mail implements EMail {
49+
50+
/**
51+
* 发送邮件
52+
* @param to 收件人
53+
* @param subject 主题
54+
* @param content 内容
55+
* @paaram isDebug 是否开启debug模式
56+
*/
57+
public void send(String to, String subject, String content, boolean isDebug) {
58+
String mailHost = EnvironmentUtils.get("mail.host");
59+
String mailTransportProtocol = EnvironmentUtils.get("mail.protocol");
60+
61+
Integer smtpPort = EnvironmentUtils.getInt("mail.smpt.port");
62+
String user = EnvironmentUtils.get("mail.user");
63+
String password = EnvironmentUtils.get("mail.password");
64+
String from = EnvironmentUtils.get("mail.from");
65+
....
66+
67+
}
68+
}
69+
70+
```
71+
72+
```
73+
package com.litongjava.tio.utils.email;
74+
75+
public class Wangyi126MailFactory implements IEMailFactory {
76+
77+
@Override
78+
public EMail getMail() {
79+
return new Wangyi126Mail();
80+
}
81+
}
82+
83+
```

docs/zh/22_tio-utils/01.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# tio-utils
2+
3+
tio-utils 是 tio-boot 的内置工具类库
4+
5+
```
6+
<dependency>
7+
<groupId>com.litongjava</groupId>
8+
<artifactId>tio-utils</artifactId>
9+
<version>3.7.3.v20240203-RELEASE</version>
10+
</dependency>
11+
```

docs/zh/22_tio-utils/02.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# http
2+
3+
## HttpUtils 类文档
4+
5+
com.litongjava.tio.utils.http.HttpUtils 是对 okhttp 的二次封装,使用前请添加 okhttp 依赖
6+
`HttpUtils` 是一个使用 OkHttpClient 处理 HTTP 请求和响应的 Java 工具类。该类提供了一组静态方法,方便进行 GET 和 POST 请求,并处理请求头和请求体。
7+
8+
```
9+
<dependency>
10+
<groupId>com.squareup.okhttp3</groupId>
11+
<artifactId>okhttp</artifactId>
12+
<version>3.14.9</version>
13+
</dependency>
14+
```
15+
16+
### 公共方法
17+
18+
#### GET 请求方法
19+
20+
1. **get(String url, Map<String, String> headerMap)**
21+
向指定 URL 发送带有可选头的 HTTP GET 请求。
22+
23+
- **参数:**
24+
- `url` - 发送 GET 请求的 URL。
25+
- `headerMap` - 包含要附加到请求的头名称和值的映射。如果不需要头,则可以为 `null`
26+
- **返回值:** `Response` - 服务器的响应。
27+
- **抛出:** 如果请求过程中发生错误,则抛出 `Exception`
28+
29+
2. **get(String url)**
30+
向指定 URL 发送 HTTP GET 请求。
31+
- **参数:**
32+
- `url` - 发送 GET 请求的 URL。
33+
- **返回值:** `Response` - 服务器的响应。
34+
- **抛出:** 如果请求过程中发生错误,则抛出 `Exception`
35+
36+
#### POST 请求方法
37+
38+
1. **post(String url, Map<String, String> headerMap, List<String> paramNames, List<String> paramValues)**
39+
向指定 URL 发送带有可选头和表单参数的 HTTP POST 请求。
40+
41+
- **参数:**
42+
- `url` - 发送 POST 请求的 URL。
43+
- `headerMap` - 包含要附加到请求的头名称和值的映射。如果不需要头,则可以为 `null`
44+
- `paramNames` - 表单数据的参数名称列表。
45+
- `paramValues` - 对应于 `paramNames` 的参数值列表。
46+
- **返回值:** `Response` - 服务器的响应。
47+
- **抛出:** 如果请求过程中发生错误,则抛出 `Exception`
48+
49+
2. **post(String url, Map<String, String> headerMap, Map<String, String> paramMap)**
50+
使用参数映射发送 HTTP POST 请求。
51+
52+
- **参数:**
53+
- `url` - 发送 POST 请求的 URL。
54+
- `headerMap` - 包含头名称和值的映射。
55+
- `paramMap` - 包含表单数据名称和值的映射。
56+
- **返回值:** `Response` - 服务器的响应。
57+
- **抛出:** 如果请求过程中发生错误,则抛出 `Exception`
58+
59+
3. **post(String url, Map<String, String> headerMap, String bodyString)**
60+
发送带有字符串体的 HTTP POST 请求。
61+
62+
- **参数:**
63+
- `url` - 发送 POST 请求的 URL。
64+
- `headerMap` - 包含头名称和值的映射。
65+
- `bodyString` - 代表请求体的字符串。
66+
- **返回值:** `Response` - 服务器的响应。
67+
- **抛出:** 如果请求过程中发生错误,则抛出 `Exception`
68+
69+
4. **post(String url, Map<String, String> headerMap)**
70+
发送带有可选头的 HTTP POST 请求。
71+
72+
- **参数:**
73+
- `url` - 发送 POST 请求的 URL。
74+
- `headerMap` - 包含头名称和值的映射。如果不需要头,则可以为 `null`
75+
- **返回值:** `Response` - 服务器的响应。
76+
- **抛出:** 如果请求过程中发生错误,则抛出 `Exception`
77+
78+
5. **post(String url)**
79+
向指定 URL 发送 HTTP POST 请求。
80+
- **参数:**
81+
- `url` - 发送 POST 请求的 URL。
82+
- **返回值:** `Response` - 服务器的响应。
83+
- **抛出:** 如果请求过程中发生错误,则抛出 `Exception`
84+
85+
### 使用方法
86+
87+
要使用 `HttpUtils` 类,只需用适当的参数调用所需方法。例如,要发送 GET 请求,可以使用:
88+
89+
```java
90+
Response response = HttpUtils.get("http://example.com");
91+
```
92+
93+
对于带有表单数据的 POST 请求,你可能会使用:
94+
95+
```java
96+
Map<String, String> paramMap = new HashMap<>();
97+
paramMap.put("key1", "value1");
98+
paramMap.put("key2", "value2");
99+
Response response = HttpUtils.post("http://example.com", null, paramMap);
100+
```
101+
102+
确保在使用后正确处理 `Response` 对象并关闭它,以避免资源泄露。
103+
104+
### 注意事项
105+
106+
- 所有方法都抛出通用的 `Exception`,因此建议根据使用场景处理特定异常。
107+
- 该类使用 `okhttp3` 库发送和接收 HTTP 请求和响应。
108+
- 管理 `Response` 对象非常重要。确保在使用后将其关闭,以释放网络资源。
109+
- 该类不处理重试或更复杂的 HTTP 功能,如缓存或连接池。对于更高级的用例,请直接使用 `OkHttpClient`
110+
- 请确保在实现中包含错误处理,以管理可能出现的网络问题或意外的响应状态。

docs/zh/22_tio-utils/03.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Notification
2+
3+
### 1. 概述
4+
5+
tio-boot 内置了消息通知组件,用户发送消息到 企业微信群
6+
本文档提供了一个通知组件的使用指南,介绍通过不同的方式发送通知,比如发送文本消息或者发送预警信息。它支持自定义通知方式,用户可以通过实现特定的接口来集成不同的通知服务。
7+
8+
### 2. 核心组件介绍
9+
10+
1. **INotification**: 这是一个接口,定义了发送通知的基本方法。任何通知实现类都应该实现这个接口。
11+
2. **INotificationFactory**: 这是一个工厂接口,用于生成`INotification`的实例。
12+
3. **NotificationUtils**: 这是一个工具类,提供了静态方法来发送不同类型的通知。它使用`INotificationFactory`来获取`INotification`的实例。
13+
14+
### 3. 使用说明
15+
16+
#### 3.1. 配置 WebHook URL
17+
18+
`app.properties`文件中配置 WebHook URL:
19+
20+
```properties
21+
notification.webhook.url=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=get key from wecom
22+
```
23+
24+
#### 3.2. 发送文本消息
25+
26+
要发送文本消息,可以使用`NotificationUtils.sendTextMsg`方法:
27+
28+
```java
29+
NotificationUtils.sendTextMsg("您的消息内容");
30+
```
31+
32+
#### 3.3. 发送预警信息
33+
34+
要发送预警信息,可以使用`NotificationUtils.sendWarm`方法:
35+
36+
```java
37+
String warningName = "警告名称";
38+
String level = "警告级别";
39+
String deviceName = "设备名称";
40+
String content = "警告内容";
41+
NotificationUtils.sendWarm(warningName, level, deviceName, content);
42+
```
43+
44+
### 4. 自定义通知工厂
45+
46+
1. **实现 INotification 接口**: 创建一个新类实现`INotification`接口,并实现其方法,以支持发送通知的具体实现。
47+
48+
```java
49+
public class CustomNotification implements INotification {
50+
// 实现接口中的方法
51+
}
52+
```
53+
54+
2. **实现 INotificationFactory 接口**: 创建一个工厂类来生成自定义通知的实例。
55+
56+
```java
57+
public class CustomNotificationFactory implements INotificationFactory {
58+
@Override
59+
public INotification getNotifaction() {
60+
return new CustomNotification();
61+
}
62+
}
63+
```
64+
65+
3. **配置工厂**: 在程序开始时,配置`NotificationUtils`使用自定义的工厂。
66+
67+
```java
68+
NotificationUtils.setFactory(new CustomNotificationFactory());
69+
```
70+
71+
4. **发送通知**: 使用`NotificationUtils`发送通知,它会使用您配置的自定义工厂来获取通知实例,并发送通知。
72+
73+
```java
74+
// 使用自定义通知发送消息
75+
NotificationUtils.sendTextMsg("您的消息内容");
76+
// 或者发送预警信息
77+
NotificationUtils.sendWarm(warningName, level, deviceName, content);
78+
```
79+
80+
### 5. 单元测试
81+
82+
`WeComPushUtilTest`类中提供了一个示例单元测试,演示了如何加载配置文件和使用`NotificationUtils`来发送通知。
83+
84+
```java
85+
public class WeComPushUtilTest {
86+
87+
@Test
88+
public void test() {
89+
// 加载配置文件
90+
EnvironmentUtils.load("app.properties");
91+
92+
// 发送文本消息
93+
NotificationUtils.sendTextMsg("Hello");
94+
95+
// 发送预警信息
96+
String warningName = "测试警告";
97+
String level = "一级";
98+
String deviceName = "设备1";
99+
String content = "异常信息如下";
100+
NotificationUtils.sendWarm(warningName, level, deviceName, content);
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)