redis缓存:
引入:
org.springframework.boot
spring-boot-starter-data-redis
并在此引用下排除:
g:io.lettuce
a:lettuce-core
不使用异步客户端lettuce,自己在引用一个jedis客户端
redis.clients
jedis
配置:
spring.redis.jedis.pool
.min-idle=5
.max-active=10
.max-idle=10
.max-wait=2000
spring.redis
.port=6379
.host=192.168.1.1
.password=123
spring.cache
.type=REDIS
.cache-names=redisCache
.redis.time-to-live=600000 (毫秒)
使用:StringSerializer 解析key
@PostConstruct
public void initRedisTemplate() {
RedisSerializer<String> strSerializer = redisTemplate.getStringSerializer();
redisTemplate.setKeySerializer(strSerializer);
redisTemplate.setHashKeySerializer(strSerializer);
}
使用:
@Cacheable(value = "redisCache", key = "'redis_user_'+#userName")
@Transactional
public DatabaseUser getUserByName(String userName) {}
@Cacheable(value = "redisCache", key = "'redis_user_role_'+#userName")
public List<DatabaseRole> findRolesByUserName(String userName) {
StringRedisTemplate 这个类 继承 RedisTemplate
rt.opsForValue().set("key1","我的测试ke1");
rt.opsForHash().put("hash","field","hvalue");
异步线程池:
@Configuration
@EnableAsync(1,开启)
public class AsyncConfig implements AsyncConfigurer {
// 定义线程池
@Override
public Executor getAsyncExecutor() {
// 定义线程池 (ThreadPoolTaskExecutor)
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// A:核心线程数
taskExecutor.setCorePoolSize(10);
// B:线程池最大线程数
taskExecutor.setMaxPoolSize(30);
// C:线程队列最大线程数
taskExecutor.setQueueCapacity(2000);
// 初始化
taskExecutor.initialize();
return taskExecutor;
}
}
2,在方法上用: @Async // 声明使用异步调用
active MQ
引用:
org.apache.activemq
activemq-pool
org.springframework.boot
spring-boot-starter-activemq
# ActiveMQ地址
spring.activemq.broker-url=tcp://localhost:61616
# 配置用户名和密码
spring.activemq.user=
.password=
# 是否使用发布订阅模式,默认是为false,即是用的是点对点的模式
spring.jms.pub-sub-domain=true
# 是否启用连接池
spring.activemq.pool.enabled=true
# 连接池最大连接数配置
spring.activemq.pool.max-connections=50
#信任所有类(把类转用ActiveMQ传递)
spring.activemq.packages.trust-all=true
# 默认发送目的地址(用默认也监控它)
spring.jms.template.default-destination=activemq.default.destination
使用(发送):
@Autowired
private JmsTemplate jmsTemplate = null;
jmsTemplate.convertAndSend(message);
使用(监听):
@JmsListener(destination = "${spring.jms.template.default-destination}")
public void receiveMsg(String message) {
}
默认用的是:SimpleMessageConverter 如需要:Jackson2JsonMessageConverter,SerializerMessageConverter 需配置:JmsTemplate
发送接收实体类:
实体类:implements Serializable
// 使用自定义地址发送对象
jmsTemplate.convertAndSend("我的目的地", user);
@JmsListener(destination = "我的地址")
public void receiveUser(User user) {}
RabbitMQ
docker启动:
docker pull rabbitmq:management
docker run -d -p 5672:5672 -p 15672:15672
--name rabbitmq rabbitmq:management
--name:指定容器名称
账号密码guest,guest
AMQP常用的消息协议
引用:
org.springframework.boot
spring-boot-starter-amqp
配置:
spring.rabbitmq
.host=localhost
.port=5672
.username=admin
.password=123
#是否确认发送的消息已经被消费。 发送消息方可监听 发送消息到消费端是否成功。
成功 则会 根据设置 进行回调。
.publisher-confirms=true
创建队列:
@Bean
public Queue createQueueMsg() {
// 创建字符串消息队列,boolean值代表是否持久化消息
return new Queue(msgQueueName, true);
}
@Bean
public Queue createQueueUser() {
// 创建用户消息队列,boolean值代表是否持久化消息
return new Queue(userQueueName, true);
}
使用(发送者和回调):
implements ConfirmCallback, RabbitMqService
@Autowired
private RabbitTemplate rabbitTemplate = null;
// 设置回调
rabbitTemplate.setConfirmCallback(this);
// 发送消息,通过msgRouting确定队列
rabbitTemplate.convertAndSend("字符串routing", msg);
rabbitTemplate.convertAndSend("对象routing", user);
// 回调确认方法
@Override
public void confirm(CorrelationData correlationData,
boolean ack, String cause) {
if (ack) {
消息成功
} else {
消费失败: + cause
}
}
使用(监听者):
@Component
public class Receiver {
// 定义监听字符串队列名称
@RabbitListener(queues = { "字符串routing" })
public void receiveMsg(String msg) {
收到消息: + msg
}
// 定义监听用户队列名称
@RabbitListener(queues = { "对象routing" })
public void receiveUser(User user) {
收到用户 + user
}
}
、
=========================================================
定时任务。
1.@EnableScheduling
2.@Scheduled 去配置如何定时
// 每隔1秒执行一次
@Scheduled(fixedRate = 1000)
@Async //异步执行
public void job1() {}
//IoC容器初始化后,第一次延迟3秒,每隔1秒执行一次
@Scheduled(initialDelay = 3000, fixedRate = 1000)
@Scheduled(cron = "0 * 11 * * ?") 秒0 分(任意) 时11 天(任意) 月(任意) 星期(不指定)。即:11:00到11:59点每一分钟执行一次
Scheduled 属性:
@Scheduled(cron = "", zone = "",
fixedDelay = 0L, fixedDelayString = "",
initialDelay = 300L,initialDelayString = "",
fixedRate = 1L, fixedRateString = ""
)
cron 表达式
zone 设定区域时间
fixedDelay 上个任务完成,到下个任务开始的间隔
+String ,同上,并且支持SpEL
fixedRate 从上个任务开始(并不是完成)到下一个任务开始的间隔
initialDelay ioc容器初始化后,首次任务执行延迟时间,单位毫秒
cron学习 (秒 分 时 天 月 星期 年)
* 任意值
?不指定,用来处理天 和 星期 冲突
- 指定时间区域
/ 指定时间间隔执行
L 最后的
# 第几个
, 列举多个项
- 秒 分 时 天 月 星期 年
- 0 0 0 * * ? 每天 00:00 触发。
- 0 15 23 ? * * 23点15分0秒
- 0 15 0 * * ? 0:15触发
- 0 15 10 * * ? * 10:15触发
- 0 30 10 * * ?2018 2018年10:30分每天执行
-
- 0 * 23 * * ? 每天23点0秒 任意执行=23:00——23:59 1分钟触发一次
- 0 0/3 23 * * ? 23:00——23:59分, 每3分钟执行一次。3分钟1间隔 23点
- 0 0/3 20,23 * * ? 比上面又多了一个 20点--21点
- 0 0-5 21 * * ? 21点 0 — 5 分钟的 0秒执行
- 0 10,44 14 ? 3 WED 3月的每周3 14:10或14点44触发
- 0 0 23 ? * MON-FRI 周一到周五23点执行 (国人认为的周一到周四)
- 0 30 23 ? * 6L 17到20年 任意月的 最后一个周5 23点30分
- 0 15 22 ? * 6#3 6数字,肯定代表周5了。每月第三周周五的22:15触发 ()
月末,季末,年末,统计 表
main 主类上加入: @EnableScheduling
@Scheduled 去配置如何定时
@Service
public class ScheduleServiceImpl {
// 计数器
int count1 = 1;
int count2 = 1;
// 每隔1秒执行一次
@Scheduled(fixedRate = 1000)
// 使用异步执行
@Async
public void job1() {
System.out.println("【" +Thread.currentThread().getName()+"】"
+ "【job1】每秒钟执行一次,执行第【" + count1 + "】次");
count1 ++;
}
// 每隔1秒执行一次
@Scheduled(fixedRate = 1000)
// 使用异步执行
@Async
public void job2() {
System.out.println("【" +Thread.currentThread().getName()+"】"
+ "【job2】每秒钟执行一次,执行第【" + count2 + "】次");
count2 ++;
}
int count3 = 1;
int count4 = 1;
// Spring IoC容器初始化后,第一次延迟3秒,每隔1秒执行一次
@Scheduled(initialDelay = 3000, fixedRate = 1000)
@Async
public void job3() {
System.out.println("【" + Thread.currentThread().getName() + "】"
+ "【job3】每秒钟执行一次,执行第【" + count3 + "】次");
count3++;
}
// 11:00到11:59点每一分钟执行一次
@Scheduled(cron = "0 * 11 * * ?") 秒0 分(任意) 时11 天(任意) 月(任意) 星期(不指定)
@Async
public void job4() {
System.out.println("【" + Thread.currentThread().getName()
+ "】【job4】每分钟执行一次,执行第【" + count4 + "】次");
count4 ++;
}
}
@Scheduled(cron = "", zone = "",
fixedDelay = 0L, fixedDelayString = "",
initialDelay = 300L,initialDelayString = "",
fixedRate = 1L, fixedRateString = ""
)
cron 表达式
zone 设定区域时间
fixedDelay 上个任务完成,到下个任务开始的间隔
fixedDelayString 同上,使用的是字符串,可以使用SpEL来引入配置文件的配置
initialDelay ioc容器初始化后,首次任务执行延迟时间,单位毫秒
initialDelayString 同上,使用SpEL来引入配置文件的配置
fixedRate 从上个任务开始(并不是完成)到下一个任务开始的间隔,单位为毫秒
fixedRateString 同上,可以使用SpEL来引入配置文件的配置
可以通过表达式,更加灵活地配置运行的方式。
秒 分 时 天 月 星期 年 (年可以不配置)
0 0 0 ?* WED
通配符 | 描述 |
---|---|
* | 任意值 |
? | 不指定,用来处理天 和 星期 冲突 |
- | 指定时间区域 |
/ | 指定时间间隔执行 |
L | 最后的 |
# | 第几个 |
, | 列举多个项 |
秒 分 时 天 月 星期 年
0 0 0 * * ? 每天 00:00 触发。 0秒 0分 0时 任意天 任意月 星期不指定
0 15 23 ? * * 23点15分0秒 天不指定,月星期任意
0 15 0 * * ? 0秒15分0小时 任意天任意月 星期不指定 0:15触发
0 15 10 * * ? * 0秒15分10小时 任意天任意月 星期不指定 任意年 10:15触发
0 30 10 * * ?2018 0秒30分10小时 天月任意星期不指定 2018年。2018年10:30分每天执行
0 * 23 * * ? 0秒 分钟任意 23小时 天月任意 星期不指定。 每天23点0秒 分钟任意执行=23:00——23:59 1分钟触发一次
0 0/3 23 * * ? 23:00——23:59分, 每3分钟执行一次。0秒 3分钟1间隔 23点
0 0/3 20,23 * * ? 比上面又多了一个 8点–9点
0 0-5 21 * * ? 21点 0 — 5 分钟的 0秒执行
0 10,44 14 ? 3 WED 0秒10或44分钟 14点 天不指定 3 月 周3 。3月的每周3 14:10或14点44触发
0 0 23 ? * MON-FRI 周一到周五 0秒0分钟23点 天不指定,年任意。周一到周五23点执行
0 30 23 ? * 6L 2017-2020 17到20年 任意月的 最后一个周5 23点30分
0 15 22 ? * 6#3 0秒15分钟22点 天不指定 月任意 ,星期是第三周周5 。每月第三周周五的22:15触发