导入SpringBoot整合quartz的坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
定义具体要执行的任务,继承QuartzJobBean
public class QuartzTaskBean extends QuartzJobBean {
@Override
protected void executeInternal(jobExecutionContext context) throws JobExecutionException {
System.out.println("quartz job run...");
}
}
定义工作明细与触发器,并绑定对应关系
@Confifuration
public class QuartzConfig {
@Bean
public JobDetail printJobDetil(){
return JobBuilder.newJob(QuartzTaskBean.class).storeDurably().build();
}
@Bean
public Trigger printJobTrigger() {
CronScheduleBuilder croScheduleBuilder = CronScheduleBuilder.cronSchedule("0/3 * * * * ?");
return TriggerBuilder.newTrigger().forJob(printJobDetail()).withSchedule(cronScheduleBuilder).build();
}
}
总结:
SpringBoot整合Quartz
开启定时任务功能
@SpringBootApplication
@EnableScheduling
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class,args);
}
}
设置定时执行的任务,并设定执行周期
@Component
public class ScheduledBean {
@Scheduled(cron = "0/5 * * * * ?")
public void printtLog() {
System.out.println(Thread.currentThread().getName()+":run...");
}
}
定时任务相关配置
spring:
task:
scheduling:
# 任务调度线程池大小 默认1
pool:
size: 1
# 调度线程名称前缀 默认scheduling-
thred-name-prefix: ssm_
shutdown:
# 线程池关闭时等待所有任务完成
await-termination: false
# 调度线程关闭前最大等待时间,确保最后一定关闭
await-termination-period: 10s
总结:
Spring Task
导入SpringBoot整合JavaMail的坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置JavaMail
spring:
mail:
host: smtp.qq.com
username: ********@qq.com
password: ********
@Service
public class SendMailServiceImpl implement SendMailService {
private String from = "*************@qq.com"; //发送人
private String to = "**************@126.com"; //接收人
private String subject = "测试邮件"; //邮件主题
private String text = "测试邮件正文"; //邮件内容
@Autowired
private JavaMailSender javaMailSender;
@Override
pubilic void sendMail() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(from);
mailessage.setTo(to);
mailMessage.setSubjet(subject);
mailMessage.setText(text);
javaMailSender.send(mailMessage);
}
}
总结:
SpringBoot整合JavaMail发送简单邮件
从126邮件发到qq邮箱
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置JavaMail
spring:
mail:
host: smtp.126.com
username: ********@126.com
password: ********
@Service
public class SendMailServiceImpl implement SendMailService {
private String from = "*************@qq.com"; //发送人
private String to = "**************@126.com"; //接收人
private String subject = "测试邮件"; //邮件主题
private String context = "点开有惊喜"; //邮件内容
@Autowired
private JavaMailSender javaMailSender;
@Override
pubilic void sendMail() {
try {
MimeMessag message = javaMailSender.creatMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, muitipart: true);
helper.setFrom(to+"(路西法98)");
helper.setTo(from );
helper.setSubjet(subject);
helper.setText(context, html: true);
//添加附件
File f1 = new File("D:\\workspace\\a.jar");
File f2 = new File("D:\\workspace\\b.png")
helper.addAttachment(f1.getName(),f1);
helper.addAttachment("我是图片名称.png",f2);
javaMailSender.send(message );
} catch (Exception e) {
e.printStackTrace();
}
}
}
总结: SpringBoot整合JavaMail发送邮件技巧
企业级应用中广泛使用的三种异步消息传递技术
JMS(Java Message Service): 一个规范,等同于JDBC规范,提供了与消息服务相关的API接口
JMS消息模型
JMS消息种类
JMS实现:ActiveMQ、Redis、HornetMQ、RabbitMQ、RocketMQ(没有安全遵守JM5规范)
AMQP(advanced message queuing protocol): 一种协议(高级消息队列协议,也是消息代理规范),规范了网络交换的数据格式,兼容JM5
优点:具有跨平台性,服务器供应商,生产者,消费者可以使用不同的语言来实现
AMQP消息模型
AMQP消息种类: byte[]
AMQP实现: RabbitMQ、StormMQ、RocketMQ
MQTT(Message Queueing Telemetry Transport) 消息队列遥测传输,专为小设备设计,是物联网(IOT)生态系统中主要成分之一
Kafka,一种高吞吐量的分布式发布订阅消息系统,提供实时消息功能。
总结:
1.消息概念及作用
2.JMS
3.AMQP
4.MQTT
下载地址:https://activemq.apache.org/components/classic/download/
安装:解压缩
启动服务
activemq.bat
访问服务器
http://127.0.0.1:8161/
总结:
1.ActiveMQ下载与安装
2.ActiveMQ服务启动(控制台)
配置ActiveMQ(采用默认配置)
spring:
activemq:
broker-url: tcp://localhost:61616
jms:
pub-sub-domain: true
template:
default-destination: itheima
生产与消费信息(使用默认消息存储队列)
@Service
public class MessageServiceActivemqImpl implements MessageService {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMessage(String id) {
System.out.println("使用Active将待发送消息的订单纳入处理队列,id:"+id);
jmsMessagingTemplate.convertAndSend(id);
}
public String doMessage() {
return jmsMessagingTemplate.receiveAndConvert(String.class);
}
}
生产与消费信息(指定消息存储队列)
@Service
public class MessageServiceActivemqImpl implements MessageService {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMessage(String id) {
System.out.println("使用Active将待发送消息的订单纳入处理队列,id:"+id);
jmsMessagingTemplate.convertAndSend("order.sm.queue.id",id);
}
public String doMessage() {
return jmsMessagingTemplate.receiveAndConvert("order.sm.queue.id",String.class);
}
}
使用消费监听器对消息队列监听
@Component
public class MessageListener {
@JmsListener(destination = "order.sm.queue.id")
public void receive(String id) {
System.out.println("已完成短信发送业务,id"+id);
}
}
流程性业务消息消费完转入下一个消息队列
@Component
public class MessageListener {
@JmsListener(destination = "order.sm.queue.id")
@SendTo("order.other.queue.id")
public String receive(String id) {
System.out.println("已完成短信发送业务,id:"+id);
return "new:"+id;
}
}
总结:
SpringBoot整合ActiveMQ
RabbitMQ基于Erlang语言编写,需要安装Erlang
Erlang:
下载地址:https://www.erlang.org/downloads
安装:一键傻瓜式安装,安装完毕需要重启,需要依赖Windows组件
环境变量配置
ERLANG_HOME
PATH
RabbitMQ:
下载地址:https://rabbitmq.com/install-windows.html
安装:一键傻瓜式安装
启动服务
rabbitmq-service.bat start
关闭服务
rabbitmq-service.bat stop
服务管理可视化(插件形式)
查看已安装的插件列表
rabbitmq-plugins.bat list
开启服务管理插件
rabbitmq-plugins.bat enable rabbitmq_management
访问服务器
http://localhost:15672
服务端口:5672,管理后台端口:15672
用户名&密码:guest
总结:
1.Erlang下载与安装(环境变量配置)
2.RabbitMQ下载与安装
3.RabbitMQ服务启动(服务)
4.RabtitMQ服务管理
导入SpringBoot整合Rabbit MQ坐标
<dependency>
<groupId>org.springftamework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置RabbitMQ(采用默认配置)
spring:
rabbitmq:
host: localhost
port: 5672
定义消息队列(direct)
@Configuration
public class RabbitDirectConfig {
@Bean
public Queue queue() {
return new Queue("simple_queue");
}
}
@Configuration
public class RabbitDirectConfig {
@Bean
public Queue queue() {
// durable:是否持久化,默认false
// exclusive:是否当前连接专用,默认false,连接关闭后队列即被删除
// autoDelete:是否自动删除,当生产者或消费者不再使用此队列,自动删除
return new Queue("simple_queue",true,false,false);
}
}
@Configuration
public class RabbitDirectConfig {
@Bean
public Queue directQueue() {
return new Queue("direct_queue");
}
@Bean
public Queue directQueue() {
return new Queue("direct_queue2");
}
@Bean
public DirectExchange directExchange(){
return new DirectExchange("directExchange");
}
@Bean
public Binding bindingDirect(){
return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct");
@Bean
public Binding bindingDirect2(){
return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct2");
}
生产与消费消息(direct)
@Service
public class MessageServiceRabbitmqDirectImpl implements MessageService {
@Autowired
private AmqpTemplate amqpTemplate;
@Override
public void sendMessage(String id){
System,out.println("使用Rabitmq将待发送的订单纳入处理列队,id:"+id);
amqpTemplate.convertAndSend("directExchange","direct",id);
}
}
使用消费监听器对消息队列监听(direct)
@Component
public class RabbitMessageListener {
@RabbitListener(queues = "direct_queue")
public void receive(String id){
System.out.println("已完成短信发送业务,id:"+id);
}
}
使用多消息监听器对消息队列监听进行消息轮循处理(direct)
@Component
public class RabbitMessageListener2 {
@RabbitListener(queues = "direct_queue")
public void receive(String id) {
System.out.println("已完成短信发送业务(two),id:"+id);
}
}
总结:
SpringBoot整合RabbitMQ直连交换机模式
定义消息队列(topic)
@Configuration
public class RabbitTopicConfig {
@Bean
public Queue topicQueue(){
return new Queue("topic_queue");
}
@Bean
public Queue topicQueue2(){
return new Queue("topic_queue2");
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("topicExchange");
}
@Bean
public Binding bindingTopic(){
return BindingBuilder.bind(topicQueue()).to(topicExchange()).with("topic.*.*");
}
@Bean
public Binding bindingTopic2(){
return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
}
}
绑定键匹配规则
(星号):用来表示一个单词,且单词是必须出现的
#(井号):用来表示任意数量
匹配键 | topic.. | topic.# |
---|---|---|
topic.order.id | true | true |
order.topic.id | false | false |
topic.sm.order.id | false | true |
topic.sm.id | false | true |
topic.id.order | true | true |
topic.id | false | true |
topic.order | false | true |
生产与消费信息(topic)
@service
public class MessageServiceRabbitmqTopicmp1 implements MessageService {
@Autowired
private AmqpTemplate amqpTemplate;
@Ovrride
public void sendMessage(String id){
System.out.println("使用Rabbitmq将待发送短信的订单纳入处理队列,id:"+id);
amqpTemplate.convertAndSend("topicExchange","topic.order.id",id);
}
}
使用消息监听器对消息队列监听(topic)
@Component
public class RabbitTopicMessageListener {
@RabbitListener(queues = "topic_queue")
public void receive(String id) {
System.out.println("已完成短信发送业务,id:"+id);
}
@RabbitListener(queues = "topic_queue2")
public void receive(String id) {
System.out.println("已完成短信发送业务(two),id:"+id);
}
}
总结:
SpringBoot整合RabbitMQ主题交换机模式
下载地址:https://rocketmq.apache.org/
安装:解压缩
默认服务端口:9876
环境变量配置
ROCKETMQ_HOME
PATH
NAMESRV_ADDR(建议):127.0.0.1:9876
启动命名服务器
mqnamesrv
启动broker
mqbroker
服务器功能测试:生产者
tools org.apache.rocketmq.example.quickstart.Producer
服务器功能测试:消费者
tools org.apache.rocketmq.example.quickstart.Consumer
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
配置RocketMQ(采用默认配置)
rocketmq:
name-server: localhost:9876
producer:
group: group_rocketmq
生产消息
@Service
public class MessageServiceRocketmqImpl implements MessageService {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Override
public void sendMessage(String id) {
rocketMQTemplate.convertAndSend("order_sm_id",id);
System.out.println("使用Rabbitmq将待发送短信的订单纳入处理队列,id",id);
}
}
生产异步消息
@Service
public class MessageServiceRocketmqImpl implements MessageService {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Override
public void sendMessage(String id) {
SendCallback callback = new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println("消息发送成功");
}
@Override
public void onException(Throwable throwable) {
System.out.println("消息发送失败!!!");
}
};
System.out.println("使用Rabbitmq将待发送短信的订单纳入处理队列,id:"+id);
rocketMQTemplate.asyncSend("order_sm_id",id,callback);
}
}
使用消息监听器对消息队列监听
@Component
@RocketMQMessageListener(topic="order_sm_id",consumerGroup="group_rocketmq")
public class RocketmqMessageListener implements RocketMQListener<String> {
@Override
public void onMessage(String id) {
System.out.println("已完成短信发送业务,id"+id);
}
}
总结:
SpringBoot整合RocketMQ
下载地址: http://kafka.apache.org/downloads
windows系统下3.0.0版本存在BUG,建议使用2.X版本
安装:解压缩
启动zookeeper
zookeeper-server-start.bat ..\..\config\zookeeper.properties
默认端口:2181
启动kafka
kafka-server-start.bat ..\..\config\server.properties
默认端口:9092
创建topic
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic itheima
查看topic
kafka-topics.bat --zookeeper 127.0.0.1:2181 --list
删除topic
kafka-topics.bat --delete --zookeeper localhost:2181 --topic itheima
生产者功能测试
kafka-console-producer.bat --broker-list localhost:9092 --topic itheima
消费者功能测试
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic itheima --from-beginning
总结:
1.Kafka下载与安装(环境变量配置)
2.zookeeper启动(控制台)
3.kafka服务启动(控制台)
4.topic维护
5.消费生产消费测试
导入SpringBoot整合Kafka坐标
<dependency>
<groupId?org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
配置Kafka(采用默认配置)
spring:
kafka:
bootstrap-server: localhost:9092
consumer:
group-id: order
生产消息
@Service
public class MessageServiceKafkaImpl implements MessageService {
@Autowired
private KafkaTemplate<String,String> kafkaTemplate;
@Override
public void sendMessage(String id) {
System.out.println("使用kafka将待发送短信的订单纳入处理队列,id:"+id);
kafkaTemplate.send("kafka_topic",id);
}
}
使用消息监听器对消息队列监听
@Component
public class KafkaMessageListener {
@KafkaListener(topic = {"kafka_topic"})
public void onMessage(ConsumerRecord<?,?> record) {
System.out.println("已完成短信发送业务,id:"+record.value());
}
}
总结:
SpringBoot整合Kafka
大总结:
1.消息
2.ActiveMQ
3.RabbitMQ
4.RocketMQ
5.Kafka