异步任务
同步,在网站上发送邮件时,后台会去发送邮件,这个过程中前台会造成响应不动,直到邮件发送完毕,响应才会成功。
@EnableAsync开启异步注解功能
package com.tasks;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync //开启异步注解功能
public class TasksApplication {
public static void main(String[] args) {
SpringApplication.run(TasksApplication.class, args);
}
}
@Async告诉Spring这是一个异步方法
package com.tasks.asyn;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
@Async //告诉Spring这是一个异步方法
public void sleep(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发送邮件完成...");
}
}
使用异步后,网页瞬间响应,后台代码依旧执行
定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式,提供了TaskExecutor 、TaskScheduler 两个接口。
- TaskExecutor 任务调度
- TaskScheduler任务执行者
两个注解:
- @EnableScheduling 开启定时任务的注解
- @Scheduled 什么时候执行
Cron表达式,在Linux定时任务常用
- 格式:秒 分钟 小时 日 月 周 年
- 在线Cron表达式生成器
package com.tasks;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableAsync //开启异步注解功能
@EnableScheduling //开启定时任务注解功能
public class TasksApplication {
public static void main(String[] args) {
SpringApplication.run(TasksApplication.class, args);
}
}
package com.tasks.scheduled;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
//每秒执行一次
@Scheduled(cron = "* * * * * ?")
public void hello(){
mysqlUtil.backup();
System.out.println("备份数据库");
}
}
邮件发送
邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持
- 邮件发送需要引入spring-boot-start-mail
- SpringBoot 自动配置MailSenderAutoConfiguration
- 定义MailProperties内容,配置在application.yml中
- 自动装配JavaMailSender
- 测试邮件发送
spring:
mail:
username: strtz3@163.com
password: 密钥
host: smtp.163.com
以下的配置可以解决一些异常
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.test-connection=true
JavaMailUtils工具类
package com.tasks.utils;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
public class JavaMailUtils {
//默认编码
public static final String DEFAULT_ENCODING = "UTF-8";
/**
* 简单邮件发送
*
* @param javaMailSender
* @param subject
* @param content
* @param target
* @param form
*/
public static void sendSimpleMessage(JavaMailSenderImpl javaMailSender, String subject, String content, String target, String form) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//邮件内容
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
//发送和接收方
simpleMailMessage.setTo(target);
simpleMailMessage.setFrom(form);
javaMailSender.send(simpleMailMessage);
}
/**
* 复杂邮件发送
*
* @param javaMailSender
* @param subject 主体
* @param body 内容
* @param isHtml 是否html
* @param target 发送方
* @param form 发件方
* @param attachment 附件
* @throws MessagingException
*/
public static void sendMimeMessage(JavaMailSenderImpl javaMailSender, String subject, String body, Boolean isHtml, String target, String form, File attachment
) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//创建一个helper来处理复杂邮件封装,true开启文件上传功能
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
//邮件内容
messageHelper.setSubject(subject);
messageHelper.setText(body, isHtml);
messageHelper.addAttachment(attachment.getName(), attachment);
//发送和接收方
messageHelper.setFrom(form);
messageHelper.setTo(target);
javaMailSender.send(mimeMessage);
}
}
测试
package com.tasks;
import com.tasks.utils.JavaMailUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.MessagingException;
import java.io.File;
@SpringBootTest
class TasksApplicationTests {
@Autowired
private JavaMailSenderImpl javaMailSender;
@Test
void contextLoads() throws MessagingException {
JavaMailUtils.sendMimeMessage(javaMailSender,"工具类发送复杂","<a href=''>go</a>",true,"strtz3@163.com","strtz3@163.com",new File("C:/Users/str/Desktop/mp.jpg"));
}
}
https://blog.csdn.net/qq_41741884/article/details/99079716 其他内容可以参考该文章
备份与恢复数据库
- mysqldump -h127.0.0.1 -uroot -p123456 test > d:/test.sql 备份数据
- mysql -h127.0.0.1 -uroot -p123456 test< d:/test.sql 恢复数据
在cmd调用命令行,其实是调用 mysql安装路径下面的bin目录下面的msqldump.exe和mysql.exe来完成相应的工作
//linux
String[] command = { "/bin/sh", "-c", command };
//window
String[] command = { "cmd", "/c", command};
//java调用外部软件exe执行命令的api;
Process ps = Runtime.getRuntime().exec(command );
package com.tasks.utils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 数据库备份和还原
*/
public class mysqlUtil {
/**
* 备份数据库db
* mysqldump -hlocalhost -uroot -proot test > d:/db.sql
*
* @param root
* @param pwd
* @param dbName
* @param backPath
* @param backName
*/
public static void dbBackUp(String root, String pwd, String dbName, String backPath, String backName) throws Exception {
String pathSql = backPath + backName;
File fileSql = new File(pathSql);
//创建备份sql文件
if (!fileSql.exists()) {
fileSql.createNewFile();
}
//mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" -hlocalhost");
sb.append(" -u" + root);
sb.append(" -p" + pwd);
sb.append(" " + dbName + " >");
sb.append(pathSql);
System.out.println("cmd命令为:" + sb.toString());
Runtime runtime = Runtime.getRuntime();
System.out.println("开始备份:" + dbName);
Process process = runtime.exec("cmd /c" + sb.toString());
System.out.println("备份成功!");
}
/**
* 恢复数据库
*
* @param root
* @param pwd
* @param dbName
* @param filePath mysql -hlocalhost -uroot -proot db < /home/back.sql
*/
public static void dbRestore(String root, String pwd, String dbName, String filePath) {
StringBuilder sb = new StringBuilder();
sb.append("mysql");
sb.append(" -hlocalhost");
sb.append(" -u" + root);
sb.append(" -p" + pwd);
sb.append(" " + dbName + " <");
sb.append(filePath);
System.out.println("cmd命令为:" + sb.toString());
Runtime runtime = Runtime.getRuntime();
System.out.println("开始还原数据");
try {
Process process = runtime.exec("cmd /c" + sb.toString());
InputStream is = process.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(is, "utf8"));
String line = null;
while ((line = bf.readLine()) != null) {
System.out.println(line);
}
is.close();
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("还原成功!");
}
public static void main(String[] args) throws Exception {
String backName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".sql";
mysqlUtil.dbBackUp("root", "root", "myblog", "E:/", backName);
//dbRestore("root", "root", "myblog", "name.sql");
}
}