springboot 啓動加載數據

package com.eyee.community.config;

import com.eyee.community.mapper.mysql.PostingMapper;
import com.eyee.community.queue.BeanContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * @Auther: Jhon Li
 * @Date: 2018/12/8 09:57
 * @Description:
 */
@Component
public class MyStartupRunner implements CommandLineRunner {

    private PostingMapper postingMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void run(String... args) throws Exception {
        this.postingMapper = BeanContext.getApplicationContext().getBean(PostingMapper.class);
        List<String> stringList= ( List<String>)redisTemplate.opsForList().leftPop("unloadPostingIdList");
        if (CollectionUtils.isEmpty(stringList)){
            stringList= postingMapper.selectUnLoadAllPostingId();
            for (String s : stringList) {
                redisTemplate.opsForList().leftPush("unloadPostingIdList",s );
            }
            redisTemplate.expire("unloadPostingIdList",1000,SECONDS);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章