springboot如何在项目启动时初始化资源-ApplicationRunner

 实现ApplicationRunner,并重写run方法即可

@Component
public class InitPortJob implements ApplicationRunner {
 
    private final static Logger logger = LoggerFactory.getLogger(InitPortJob.class);
    //数量待调整
    public static BlockingQueue<Integer>  portQueue = new LinkedBlockingQueue<Integer>(10000);
    
    //@Value("${sip.begin.port}")
    private int beginPort=30000;
 
    //@Value("${sip.end.port}")
    private int endPort=40000;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("InitPort Begin MinPort:{};MaxPort:{};",beginPort,endPort);
        for (int tempPort = beginPort; tempPort < endPort; tempPort++) {
            if(tempPort%2 == 0) {
                portQueue.offer(tempPort);
            }
        }
    }
}

 

posted @ 2021-10-26 22:19  Vincent-yuan  阅读(104)  评论(0编辑  收藏  举报