AsyncTask
package com.copote.gateway.async;
import com.copote.common.constant.IndexDataKeyConst;
import com.copote.common.enums.MsgMxCategoryEnum;
import com.copote.common.utils.RedisUtil;
import com.copote.gateway.appinfo.query.Pagination;
import com.copote.gateway.dbsx.dao.DbsxDao;
import com.copote.gateway.dbsx.dao.GzdbDao;
import com.copote.gateway.dbsx.entity.DbsxEntity;
import com.copote.gateway.dbsx.query.DbsxQuery;
import com.copote.gateway.feedback.dao.ZjJcfkDao;
import com.copote.gateway.feedback.entity.ZjJcfkEntity;
import com.copote.gateway.goldenIdeaSharing.dao.ZjJdzgxDao;
import com.copote.gateway.goldenIdeaSharing.model.GoldenIdeaSharingModel;
import com.copote.gateway.index.model.IndexGzdbModel;
import com.copote.gateway.index.query.IndexGoldenIdeaQuery;
import com.copote.gateway.information.dao.ZjXxfbDao;
import com.copote.gateway.information.model.InformationModel;
import com.copote.gateway.information.query.InformationQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class AsyncTask {
@Autowired
private RedisUtil redisUtil;
@Autowired
private DbsxDao dbsxDao;
@Autowired
private ZjXxfbDao zjxxfbDao;
@Autowired
private ZjJcfkDao zjJcfkDao;
@Autowired
private GzdbDao gzdbDao;
@Autowired
private ZjJdzgxDao jdzgxDao;
private static Logger log = LoggerFactory.getLogger(AsyncTask.class);
@Async
public void updateIndeEffcTask(){
log.info("执行查询首页二分风采发布异步任务.....");
InformationQuery informationQuery = new InformationQuery();
informationQuery.setPageNum(1);
informationQuery.setPageSize(8);
informationQuery.setnFlid(MsgMxCategoryEnum.EFFC.getCode());
List<InformationModel> information1List = zjxxfbDao.selectIndexMsgByQuery(informationQuery);
List<Object> objList3 = (List) information1List;
if(redisUtil.hasKey(IndexDataKeyConst.INDEX_ERFC_KEY)){
redisUtil.lClear(IndexDataKeyConst.INDEX_ERFC_KEY);
}
redisUtil.lSet(IndexDataKeyConst.INDEX_ERFC_KEY, objList3);
}
IndexServiceImpl
package com.copote.gateway.index.service.impl;
import com.copote.common.ResultBody;
import com.copote.common.constant.FileConst;
import com.copote.common.constant.IndexDataKeyConst;
import com.copote.common.enums.MsgMxCategoryEnum;
import com.copote.common.enums.DbCategoryEnum;
import com.copote.common.utils.JsonUtil;
import com.copote.common.utils.RedisUtil;
import com.copote.gateway.appinfo.query.Pagination;
import com.copote.gateway.async.AsyncTask;
import com.copote.gateway.dbsx.dao.DbsxDao;
import com.copote.gateway.dbsx.dao.DbsxDetailDao;
import com.copote.gateway.dbsx.entity.DbsxDetailEntityKey;
import com.copote.gateway.dbsx.entity.DbsxEntity;
import com.copote.gateway.dbsx.query.DbsxQuery;
import com.copote.gateway.feedback.dao.ZjJcfkDao;
import com.copote.gateway.feedback.entity.ZjJcfkEntity;
import com.copote.gateway.feedback.model.ServiceCommitmentModel;
import com.copote.gateway.goldenIdeaSharing.dao.ZjJdzgxDao;
import com.copote.gateway.goldenIdeaSharing.model.GoldenIdeaSharingModel;
import com.copote.gateway.index.model.DbDetailModel;
import com.copote.gateway.index.model.IndexXXfbModel;
import com.copote.gateway.index.query.IndexGoldenIdeaQuery;
import com.copote.gateway.index.query.IndexJckfQuery;
import com.copote.gateway.index.service.IndexService;
import com.copote.gateway.information.dao.ZjXxfbDao;
import com.copote.gateway.information.model.InformationModel;
import com.copote.gateway.information.query.InformationQuery;
import com.copote.gateway.leaderxc.dao.LdXcDao;
import com.copote.gateway.leaderxc.entity.LdXcEntity;
import com.copote.gateway.system.dao.JgdmDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.*;
@Service
public class IndexServiceImpl implements IndexService {
@Autowired
private RedisUtil redisUtil;
@Autowired
private DbsxDetailDao dbsxDetailDao;
@Autowired
private DbsxDao dbsxDao;
@Autowired
private JgdmDao jgdmDao;
@Autowired
private ZjXxfbDao zjxxfbDao;
@Autowired
private ZjJcfkDao zjJcfkDao;
@Autowired
private LdXcDao ldxcDao;
@Autowired
FileConst fileConst;
@Autowired
private ZjJdzgxDao jdzgxDao;
@Autowired
private AsyncTask asyncTask;
private Logger log = LoggerFactory.getLogger(IndexServiceImpl.class);
@Override
public void generateIndexJsonData() {
asyncTask.updateIndexDbsxTask();
asyncTask.updateIndexJcfkTask();
asyncTask.updateIndexXxfbTask();
asyncTask.updateIndeEffcTask();
asyncTask.updateIndexXjdxTask();
asyncTask.updateIndexGzdbTask();
}
@Override
public ResultBody<Map<String, Object>> selectIndexData() {
Map<String, Object> dataMap = new HashMap<>();
List<Object> dbsxList = redisUtil.lGet(IndexDataKeyConst.INDEX_DBSX_KEY, 0, -1);
List<Object> jcfkList = redisUtil.lGet(IndexDataKeyConst.INDEX_JCFK_KEY, 0, -1);
List<Object> xxfbList = redisUtil.lGet(IndexDataKeyConst.INDEX_XXFB_KEY, 0, -1);
List<Object> erfenFcList = redisUtil.lGet(IndexDataKeyConst.INDEX_ERFC_KEY, 0, -1);
List<Object> xjdxList = redisUtil.lGet(IndexDataKeyConst.INDEX_XJDX_KEY, 0, -1);
List<Object> gzdbDbsxData = redisUtil.lGet(IndexDataKeyConst.INDEX_GZDB_DBSX_KEY, 0, -1);
dataMap.put("dbsxList", dbsxList);
dataMap.put("jcfkList", jcfkList);
dataMap.put("xxfbList", xxfbList);
dataMap.put("erfenFcList", erfenFcList);
dataMap.put("xjdxList", xjdxList);
dataMap.put("gzdbDbsxData", gzdbDbsxData);
List<Object> sdjsData = redisUtil.lGet(IndexDataKeyConst.INDEX_SDJS_KEY, 0, -1);
if(!CollectionUtils.isEmpty(sdjsData)){
dataMap.put("sdjsGztzData", sdjsData.get(0));
dataMap.put("sdjsXXfbData", sdjsData.get(1));
dataMap.put("sdjsGoldenIdeaData", sdjsData.get(2));
}
return new ResultBody<>(dataMap);
}
@PostConstruct
public void init(){
log.info("服务启动...");
generateIndexJsonData();
}
@PreDestroy
public void destroy(){
log.info("服务销毁...");
String[] redisKey = new String[]{
IndexDataKeyConst.INDEX_DBSX_KEY, IndexDataKeyConst.INDEX_JCFK_KEY,
IndexDataKeyConst.INDEX_XXFB_KEY, IndexDataKeyConst.INDEX_SDJS_KEY,
IndexDataKeyConst.INDEX_GZDB_DBSX_KEY};
redisUtil.del(redisKey);
}
}
IndexDataKeyConst
public class IndexDataKeyConst {
public static final String INDEX_DBSX_KEY = "indexDdtaList";
RedisUtil.java
package com.copote.common.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0)
expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public void lClear(String key) {
try {
redisTemplate.opsForList().trim(key, 0, 0);
redisTemplate.opsForList().leftPop(key);
} catch (Exception e) {
e.printStackTrace();
}
}
}