学以致用,用框架搭建极简的定时任务给你的对象发“情话”
学以致用,用框架搭建极简的定时任务给你的对象发“qinghua”
1、突发
最近刚考完试,准备去实习嘛,偶尔看见“Java技术核心”这个公众号推出了一篇文章,觉得挺好玩的,哈哈哈哈哈。
2、干就完了
2.1 创建项目
通过idea创建一个普通项目saodishengsaohua
2.2 导入依赖
在基本的框架基础上,需要加入邮件发送mail、RPC远程调用httpclient、Schedule的依赖。
<!--邮件依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- httpclient 依赖 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
2.3 编写配置
这部分的话我们先来了解一下邮件发送的原理。
邮件发送原理图
简易文本邮件发送的实现
由上图我们可以确定几个必须步骤
1.创建session对象
2.创建Transport对象
3.使用邮箱的用户名和授权码连上邮件服务器
4.创建一个Message对象(需要传递session)
message需要指明发件人、收件人以及文件内容
5.发送邮件
6.关闭连接
授权码的获得
在编写配置前需要在浏览器登录自己的邮箱在账号安全中设置开启POP3/SMTP服务。
开始开启POP3/SMTP服务需要输入验证码
复制授权码
勾选SMTP发信息后保存到服务器,勾选这一项主要是可以看到自己发送了什么信息,不勾选此项。邮件消息发送成功后,邮箱内看不到自己已发送的信息
编写配置文件application.yml
spring:
application:
name: emailmassage
# 邮件配置
mail:
username: 530518534@qq.com # 自己的邮箱
password: xxxxxxxxxxxx # smtp | pop3 | imap协议授权码
host: smtp.qq.com # 服务器地址,参考优先该服务运行商提供的信息
properties:
mail:
smtp:
auth: true # 开启SMTP协议验证
port: 587
# 具体发给谁
she:
mail: xxxxxx@qq.com
2.4 编写Springboot启动类,开启定时依赖的注解
package top.saodisheng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class SaodishengsaohuaApplication {
public static void main(String[] args) {
SpringApplication.run(SaodishengsaohuaApplication.class, args);
}
}
2.5 编写自动生成发送内容的组件
package top.saodisheng.component;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
/**
* Description:
* 自动生成发送的内容
* @author 扫地生_saodisheng
*/
@Component
public class SendMessage {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Value("${she.mail}")
private String[] sheMail;
public void sendMessage(String subject,String message) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from);//发送者邮件邮箱
helper.setTo(sheMail);//收邮件者邮箱
helper.setSubject(subject);//发件主题
helper.setText(message);//发件内容
mailSender.send(helper.getMimeMessage());//发送邮件
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**远程获取要发送的信息*/
public static String getOneS(){
try {
//创建客户端对象
HttpClient client = HttpClients.createDefault();
/*创建地址 https://du.shadiao.app/api.php*/
HttpGet get = new HttpGet("https://chp.shadiao.app/api.php");
//发起请求,接收响应对象
HttpResponse response = client.execute(get);
//获取响应体,响应数据是一种基于HTTP协议标准字符串的对象
//响应体和响应头,都是封装HTTP协议数据。直接使用可能出现乱码或解析错误
HttpEntity entity = response.getEntity();
//通过HTTP实体工具类,转换响应体数据
String responseString = EntityUtils.toString(entity, "utf-8");
return responseString;
} catch (IOException e) {
throw new RuntimeException("网站获取句子失败");
}
}
}
2.6 编写定时任务
package top.saodisheng.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Description:
* 编写定时任务
* @author 扫地生_saodisheng
*/
@Component
public class MyScheduled {
@Autowired
private SendMessage sendMessage;
/*定时执行任务方法 每天早上5点20执行该任务*/
@Scheduled(cron ="0 20 5 * * *")
public void dsrw(){
String message = sendMessage.getOneS();
sendMessage.sendMessage("来自好孩子的消息!❤", message);
}
}
整个项目结构如下:
3、打包项目
建议将打包后的jar包放入云服务器中运行(当然在Win10环境中也可以运行)
记得在云服务器中开启指定端口:587,防火墙放行587端口。还有http和https端口。
打jar包
在服务器上运行
在Linux中运行jar包格式:
nohup java -jar jar包 >test.log &
看看运行结果:
在Win10中运行
win10 定时运jar 包 在任务计划程序中创建任务
新建触发器
新建操作,在程序或脚本输入执行的jar命令,点击确定
然后可以看见,创建好的任务
向大神看齐