【Excel_To_DB】SpringBoot+EasyPoi+Redis消息队列实现Excel批量异步导入数据库(一)
【Excel_To_DB】SpringBoot+EasyPoi+Redis消息队列实现Excel批量异步导入数据库(二)
【Excel_To_DB】SpringBoot+EasyPoi+Redis消息队列实现Excel批量异步导入数据库(三)
【效果演示】:JavaWeb毕业设计项目-足球队管理系统(四)引入Excel_To_DB项目+源码
【码云地址】:https://gitee.com/ydc_coding
Redis实现消息队列:
环境依赖:
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
说明:这里我通过使用fastjson将bean转换成String(也就是json字符串),存入redis中,而没有选择用谷歌的Protostuff,虽然效率上有所降低,但Protostuff序列化后的数据存入redis中,不具备可读性。用更好的可读性换那一块效率,我觉得在当前这个业务环境中更合适一些。
另一个使用Protostuff的Demo:【redis-demo】使用Jedis api 实现后端缓存优化
redis数据库配置:
#redis
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=666666
# Redis数据库索引(默认为0)
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=2000
#spring-session 使用
消息接收者:
package com.ydc.excel_to_db.redis;
import com.ydc.excel_to_db.domain.ExcelModel;
import com.ydc.excel_to_db.service.ImportService;
import com.ydc.excel_to_db.util.Constant;
import com.ydc.excel_to_db.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description: 消息接收者,将其在ExcelToDbApplication.java中注入消息监听容器(MessageListenerAdapter)中
* @Author: 杨东川【http://blog.csdn.net/yangdongchuan1995】
* @Date: Created in 2018-2-6
*/
@Service
public class Receiver {
@Autowired
ImportService importService;
@Autowired
RedisDao redisDao;
private static final Logger log = LoggerFactory.getLogger(Receiver.class);
/**
* @Description: 用于接收单个对象,将对象同步至数据库,如果同步失败,则存入redis中
* @Param: [message] “fastjson”转换后的json字符串
* @Retrun: void
*/
public void receiveSingle(String message) throws InterruptedException {
// 将json字符串转换成实体对象
ExcelModel excelModel = JsonUtil.stringToBean(message, ExcelModel.class);
// 尝试同步数据库并返回同步结果
boolean result = importService.save(excelModel);
if (!result)
// 同步失败,将其存入redis中
redisDao.leftPushKey(Constant.failToDBKey, excelModel);
else
// 同步成功,输出至日志中
log.info("成功插入数据库的数据:" + excelModel.getCol2());
// 加上-1,其实也就是做减1操作
redisDao.incrOrDecr(Constant.succSizeTempKey, -1);
}
/**
* @Description: 用于接收对象集合,将集合遍历拆分成单个对象并进行发布
* @Param: [message] “fastjson”转换后的json字符串
* @Retrun: void
*/
public void receiveList(String message) throws InterruptedException {
// 将json字符串转换成对象集合
List<ExcelModel> list = JsonUtil.stringToList(message, ExcelModel.class);
// 遍历集合,并依次将其发布
for (ExcelModel excelModel : list) {
redisDao.publish(Constant.receiveSingle, excelModel);
}
}
}
配置消息监听器:
package com.ydc.excel_to_db;
import com.ydc.excel_to_db