mirror of
https://gitee.com/52itstyle/spring-boot-seckill.git
synced 2025-12-30 10:22:26 +00:00
:sparkles:异步处理while防止阻塞启动线程
This commit is contained in:
@@ -8,7 +8,7 @@ import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
* 创建者 科帮网
|
||||
* 创建者 爪哇笔记
|
||||
* 创建时间 2018年5月12日
|
||||
* API接口测试:http://localhost:8080/seckill/swagger-ui.html
|
||||
* 跑之前 一定要看文库:https://gitee.com/52itstyle/spring-boot-seckill/wikis
|
||||
|
||||
@@ -22,6 +22,6 @@ public class SwaggerConfig {
|
||||
// 预览地址:swagger-ui.html
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder().title("Spring 中使用Swagger2构建文档").termsOfServiceUrl("https://blog.52itstyle.com")
|
||||
.contact(new Contact("科帮网 ", "https://blog.52itstyle.com/", "345849402@qq.com")).version("1.1").build();
|
||||
.contact(new Contact("爪哇笔记 ", "https://blog.52itstyle.vip/", "345849402@qq.com")).version("1.1").build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
if(SpringUtil.applicationContext == null) {
|
||||
SpringUtil.applicationContext = applicationContext;
|
||||
}
|
||||
System.out.println("========科帮网(https://blog.52itstyle.com)========");
|
||||
System.out.println("========爪哇笔记(https://blog.52itstyle.vip)========");
|
||||
}
|
||||
|
||||
//获取applicationContext
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.itstyle.seckill.queue.jvm;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
@@ -14,18 +16,27 @@ import com.itstyle.seckill.service.ISeckillService;
|
||||
*/
|
||||
@Component
|
||||
public class TaskRunner implements ApplicationRunner{
|
||||
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(TaskRunner.class);
|
||||
|
||||
@Autowired
|
||||
private ISeckillService seckillService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments var) throws Exception{
|
||||
while(true){
|
||||
//进程内队列
|
||||
SuccessKilled kill = SeckillQueue.getMailQueue().consume();
|
||||
if(kill!=null){
|
||||
seckillService.startSeckil(kill.getSeckillId(), kill.getUserId());
|
||||
}
|
||||
}
|
||||
public void run(ApplicationArguments var){
|
||||
new Thread(() -> {
|
||||
LOGGER.info("提醒队列启动成功");
|
||||
while(true){
|
||||
try {
|
||||
//进程内队列
|
||||
SuccessKilled kill = SeckillQueue.getMailQueue().consume();
|
||||
if(kill!=null){
|
||||
seckillService.startSeckil(kill.getSeckillId(), kill.getUserId());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user