Overall project introduction:
https://blog.csdn.net/wenjieyatou/article/details/80190886
Coupon Item 1 Introduction:https://blog.csdn.net/wenjieyatou/article/details/80191083
Coupon Item 2 Introduction:
https://blog.csdn.net/wenjieyatou/article/details/80203860
Coupon project three introduction:
https://blog.csdn.net/wenjieyatou/article/details/80206069
The following is the introduction of the four coupon items:
branch 1.4
Purpose: Issue coupons through asynchronous and message queuesSpecific solutions:
1: Issue coupons asynchronously, which can improve the utilization of CPU, and at the same time ensure stability through message queues, and avoid exceptions that lead to the successful return of the front-end coupons, but An exception occurred
during creation. When issuing 500 coupons, the efficiency was improved by about 0.5s2: Refactor the code once
Principle: Modify the big method to small methods, each small method handles a business, such as acquisition, then the responsibility of this method is to acquire activities, and each small method should have a return value as much as possible, which can increase the readability of the code
package com.peiyu.mem.manager.impl; import com.migr.common.util.JsonUtil; import com.peiyu.mem.dao.CouponDao; import com.peiyu.mem.domian.entity.Coupon; import com.peiyu.mem.manager.CouponManager; import com.peiyu.mem.rabbitmq.produces.MqSenderHandler; import com.peiyu.mem.utils.ListUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; import java.util.List; /** * Created by Administrator on 2016/12/7. */ @Service public class CouponManagerImpl implements CouponManager { private Logger log = Logger.getLogger(CouponManagerImpl.class); @Autowired private PlatformTransactionManager transactionManager; @Autowired private CouponDao couponDao; @Autowired private MqSenderHandler mqSenderHandler; @Override public boolean insertCoupons(final List<Coupon> coupons) { final long start1 = System.currentTimeMillis(); TransactionTemplate template = new TransactionTemplate(transactionManager); return template.execute(new TransactionCallback<Boolean>() { @Override public Boolean doInTransaction(TransactionStatus transactionStatus) { try { if (coupons.size() <= 5000) { couponDao.insertBatchCoupons(coupons); } if (coupons.size() > 5000) { List<List<Coupon>> tempCoupons = ListUtil.splitList(coupons, 5000); for (List<Coupon> item : tempCoupons) { try { couponDao.insertBatchCoupons(item); } catch (Exception e) { String data = JsonUtil.g.toJson(item);//Submit the caught exception to the message queue for processing mqSenderHandler.sendMessage("spring.makeCoupons.queueKey", data); continue; } } } long end1 = System.currentTimeMillis(); log.info("Add" + coupons.size() + "Coupon consumption time:" + (end1 - start1) + "milliseconds"); return true; } catch (Exception e) { log.error("Add coupon exception: " + e); transactionStatus.setRollbackOnly(); return false; } } }); } @Override public boolean updateCoupons(final List<Coupon> coupons) { TransactionTemplate template = new TransactionTemplate(transactionManager); return template.execute(new TransactionCallback<Boolean>() { @Override public Boolean doInTransaction(TransactionStatus transactionStatus) { try { if (CollectionUtils.isNotEmpty(coupons)) { couponDao.updateBatchCouponsState(coupons); return true; } return false; } catch (Exception e) { transactionStatus.setRollbackOnly(); String data=JsonUtil.g.toJson(coupons);//Process the captured exception message queue mqSenderHandler.sendMessage("spring.updateCouponState.queueKey",data); return false; } } }); } }