对于测试人员来说,有时候对线上问题的跟踪解决,缺乏一定意识,有时因禅道bug过多,导致没有重点根据线上问题。若通过钉钉机器人,设置定时任务发送,提醒相关人员按时处理禅道bug,就会让人关注重点。
实现步骤:通过java代码实现
1、找到对应消息接收群,设置钉钉机器人,拷贝关键信息Webhook地址
2、在java代码中编写代码实现发送钉钉机器人信息,以表格形式发送
3、在java代码中,连接收集禅道bug的数据库连接方式,并编写对应sql获取关键统计信息
4、在java代码中,编写定时器代码,触发钉钉机器人发送信息
5、将java代码打成jar包,放在服务器上,根据java代码中设置的定时器时间,进行触发任务
具体实现代码:
发送钉钉机器人代码
package Timer;
import org.json.JSONObject;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SendMessageDing {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://XXXX:ip/zentao";
private static final String DB_USER = "root";
private static final String DB_PASS = "123456";
private static final String DINGTALK_WEBHOOK_URL = "你的机器人webhook地址";
// public static void main(String[] args) {
sendMessage();
// }
public static void sendMessage(){
// SpringApplication.run(ZentaoSentMessageApplication.class, args);
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
stmt = conn.createStatement();
// String sql = "SELECT b.id as ID,p.`name` as 产品名称, b.title as BUG标题, b.deadline as 截止时间, u1.realname as 指派给 "
// + "FROM zt_bug b JOIN zt_project p ON b.project = p.id "
// + "JOIN zt_user u1 ON b.assignedTo = u1.account "
// + "WHERE b.title LIKE '%线上问题%' AND b.status = 'active' AND b.deadline != '0000-00-00' AND b.deadline%s", id, rs.getString("BUG标题"));
String deadline = rs.getString("截止时间");
String assignedTo = rs.getString("指派给");
message.append("| " + productName + " | " + bugTitle + " | " + deadline + " | " + assignedTo + " |\n");
hasContent = true;
}
if (hasContent) {
JSONObject json = new JSONObject();
json.put("msgtype", "markdown");
JSONObject markdown = new JSONObject();
markdown.put("title", "线上超时未解决问题列表");
markdown.put("text", message.toString());
json.put("markdown", markdown);
post(DINGTALK_WEBHOOK_URL, json.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
}
}
}
private static void post(String url, String body) {
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8);
writer.write(body);
writer.flush();
writer.close();
int responseCode = con.getResponseCode();
System.out.println("DingTalkBot: HTTP POST request to " + url + " returned " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
};
定时器代码,每天早上9点执行
package Timer;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class TimeScheduled {
private static Timer timer;
public static void main(String[] args) {
setScheduledTask(9, 0, 0); // 设置每天早上9点执行任务
}
/**
* 设置定时任务,每天在指定时间执行一次
* @param hour 小时数(24小时制)
* @param minute 分钟数
* @param second 秒数
*/
public static void setScheduledTask(int hour, int minute, int second) {
Calendar currentTime = Calendar.getInstance();
Calendar scheduledTime = (Calendar)currentTime.clone();
scheduledTime.set(Calendar.HOUR_OF_DAY, hour);
scheduledTime.set(Calendar.MINUTE, minute);
scheduledTime.set(Calendar.SECOND, second);
// 如果当前时间已经超过了设定的时间,则将任务执行时间加1天,避免立即执行
if (scheduledTime.before(currentTime)) {
scheduledTime.add(Calendar.DAY_OF_MONTH, 1);
}
long delayMillis = scheduledTime.getTimeInMillis() - currentTime.getTimeInMillis();
TimerTask task = new TimerTask() {
@Override
public void run() {
// 在这里编写定时任务需要执行的操作,例如打印日志或发送邮件等。
SendMessageDing sendMessageDing = new SendMessageDing();
sendMessageDing.sendMessage();
System.out.println("定时任务执行了!");
}
};
timer = new Timer();
timer.scheduleAtFixedRate(task, delayMillis, 24 * 60 * 60 * 1000);
System.out.println("定时任务已启动,每天" + hour + ":" + minute + ":" + second + "执行一次。");
}
}
pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.14
com.example
ZentaoSentMessage
0.0.1-SNAPSHOT
ZentaoSentMessage
ZentaoSentMessage
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-validation
com.alibaba
fastjson
1.2.78
com.squareup.okhttp3
okhttp
4.9.3
mysql
mysql-connector-java
8.0.25
org.springframework.boot
spring-boot-maven-plugin
将代码进行打包
打包完成后在target中查找jar包文件
打开需要放入jar的服务器地址,将jar包扔进去
修改配置文件docker-compose.yml【可手动增加该文件】
version: '3'
services:
xxl-job:
container_name: xxl-job
restart: always
image: java8:latest
volumes:
#jar文件目录
- /root/TimerData/data:/data
expose:
- "9999"
ports:
- "8099:8099"
command: [
'/data/ZentaoSentMessage-0.0.1-SNAPSHOT.jar',
'--server.port=8099'
]
docker-compose up -d 执行代码jar中的main方法
docker ps -a 查看进程信息
docker logs --tail 500 bec66e81fb44 查看日志信息
然后观察到点是否触发任务
若要关闭该定时任务,需要在服务器上执行docker命令
docker-compose down
docker stop bec66e81fb44
以上两种都是终止定时器任务
到此,通过钉钉机器人定时发送禅道bug消息,并且以表格的形式发送,就完成了~~~~