@Override @Async("asyncInvoiceExecutor") public void printInvoiceByType(List<InvoiceLog> invoiceLogList, Integer invoiceType, BillTaxControlInfo taxControlInfo) { for (InvoiceLog invoiceLog : invoiceLogList) { InvoiceLog invoiceLogResult = null; if (invoiceType == 1 || invoiceType == 2) { invoiceLogResult = commonInvoiceService.paperInvoiceAndPrint(invoiceLog, taxControlInfo); } else if (invoiceType == 3) { invoiceLogResult = commonInvoiceService.electronicRealTimeInvoice(invoiceLog, taxControlInfo); } logger.debug("======================= invoice print end...========================="+(invoiceLogResult == null)); if (invoiceLogResult != null) { invoiceLogService.updateByPrimaryKeySelective(invoiceLogResult); // updateInvoiceLog(invoiceLogResult); logger.debug("======================= update end...========================="); } } } @Transactional public int updateInvoiceLog(InvoiceLog invoiceLog){ return invoiceLogMapper.updateByPrimaryKeySelective(invoiceLog); }
如上代码所示,@Async首先得用在不同类中,所以下放到Service中的一个方法,这个方法有耗时操作(调用外部接口打印发票),打印完结果还要更新到数据库,为了避免大事物,将方法命名为printInvoiceByType(只读)。后面调用另一个service的方法更新记录结果,可是事物根本没提交,打印结果如下:
[18 14:28:25,176 DEBUG] [asyncInvoiceExecutor-1] rest.RestUtil - =============================调用返回结果:"{\"RetMsg\":\"4001-发票数据不合法 [A632,开票人长度不正确!]\",\"RetCode\":\"4001\"}"
[18 14:28:25,181 INFO ] [asyncInvoiceExecutor-1] impl.CommonInvoiceServiceImpl - 开具发票失败!===================>:4001-发票数据不合法 [A632,开票人长度不正确!]
[18 14:28:25,182 DEBUG] [asyncInvoiceExecutor-1] impl.InvoicingServiceImpl - ======================= invoice print end...=========================false
[18 14:28:25,183 DEBUG] [asyncInvoiceExecutor-1] InvoiceLogMapper.updateByPrimaryKeySelective - ==> Preparing: update bill_invoice_log SET invoice_status = ?, result_code = ?, result_msg = ? where invoice_id = ?
[18 14:28:25,183 DEBUG] [asyncInvoiceExecutor-1] InvoiceLogMapper.updateByPrimaryKeySelective - ==> Parameters: 9(Integer), 4001(String), 4001-发票数据不合法 [A632,开票人长度不正确!](String), 200011(Integer)
那个update end...也没打印,不知道怎么回事。事物是通过AOP切面配置的。
Thread dump 结果如下:
"asyncInvoiceExecutor-1@10943" prio=5 tid=0x29 nid=NA waiting
java.lang.Thread.State: WAITING
at sun.misc.Unsafe.park(Unsafe.java:-1)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
这到底怎么回事,阻塞了吗,也没报错。为什么产生这种现象。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。