阻塞队列的使用与改进

之前的阻塞队列:

  存放消息的队列:

   /** 平台询价阻塞队列,大小为50,内容为appId */

public final static BlockingQueue<String> GOODS_OFFER = new LinkedBlockingQueue<String>(50);

存放的程序:
SgGoodsOfferRuner.GOODS_OFFER.add(appId);

取出使用的程序:
@Component
@Slf4j
public class sgGoodsOfferRuner implements CommandLineRunner {

@Autowired
SgSubGoodsOfferService sgSubGoodsOfferService;

@Override
public void run(String... args) throws Exception {
String appId = ContantNotice.GOODS_OFFER.take();
log.info("发送代发仓报价开始,appId : " + appId);
sgSubGoodsOfferService.sendGoodsPrices(appId);
}
}

以上程序的问题是:只有在发的时候,使用程序才会跑,导致项目启动时卡住,启动不完全。解决方法是,将使用的程序改为新的线程继续跑,与主程序分离开。



之后的阻塞队列(取出的地方)
@Component
@Slf4j
public class SoPricesOfferRuner {

@Autowired
   SoPricesOfferService soPricesOfferService;
   @PostConstruct
public void postConstruct() {
new Thread(new SoPricesOfferThread()).start();
}

class SoPricessOfferThread implements Runnable {

public void run() {
log.info("SoPricesOfferThread:: run...");
while (true) {
try {
String appId = GOODS_OFFER.take();
                  soPricesOfferService.sendGoodsPrices(appId);
              } catch (Exception e) {
log.error("SoPricesOfferThread ERROR",e);
}
}
}
}
}
解决方法是,将使用的程序改为新的线程继续跑,与主程序分离开。
posted @ 2019-06-24 10:36  java&番茄小将  阅读(159)  评论(0)    收藏  举报