阻塞队列的使用与改进

平台询价阻塞队列介绍
博客提及之前的阻塞队列,具体为平台询价阻塞队列,大小为50,内容为appId,还给出了转载来源。

之前的阻塞队列:

  存放消息的队列:

   /** 平台询价阻塞队列,大小为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);
}
}
}
}
}
解决方法是,将使用的程序改为新的线程继续跑,与主程序分离开。

转载于:https://www.cnblogs.com/fanqie15/p/11075787.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值