@Test public void testExecuteSeckillProcedure() throws Exception { long seckillId = 1001; long phone = 13631231234L; Exposer exposer = seckillService.exportSeckillUrl(seckillId); if (exposer.isExposed()) { String md5 = exposer.getMd5(); SeckillExecution execution = seckillService.executeSeckillProcedure(seckillId, phone, md5); logger.info(execution.getStateInfo()); } }
@Override public SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5) { if (md5 == null || !md5.equals(getMD5(seckillId))) { return new SeckillExecution(seckillId, SeckillStateEnum.DATA_REWRITE); } Date killTime = new Date(); Map<String, Object> map = new HashMap<String, Object>(); map.put("seckillId", seckillId); map.put("phone", userPhone); map.put("killTime", killTime); map.put("result", null); try { seckillDao.killByProcedure(map); int result = MapUtils.getInteger(map, "result", -2); if (result == 1) { SuccessKilled sk = successKilledDao.queryByIdWithSeckill(seckillId, userPhone); return new SeckillExecution(seckillId, SeckillStateEnum.SUCCESS, sk); } else { return new SeckillExecution(seckillId, SeckillStateEnum.stateOf(result)); } } catch (Exception e) { logger.error(e.getMessage(), e); return new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR); } }
SeckillServiceImpl implements SeckillService { @Override public SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5) { if (md5 == null || !md5.equals(getMD5(seckillId))) { return new SeckillExecution(seckillId, SeckillStateEnum.DATA_REWRITE); } Date killTime = new Date(); Map<String, Object> map = new HashMap<String, Object>(); map.put("seckillId", seckillId); map.put("phone", userPhone); map.put("killTime", killTime); map.put("result", null); try { seckillDao.killByProcedure(map); int result = MapUtils.getInteger(map, "result", -2); if (result == 1) { SuccessKilled sk = successKilledDao.queryByIdWithSeckill(seckillId, userPhone); return new SeckillExecution(seckillId, SeckillStateEnum.SUCCESS, sk); } else { return new SeckillExecution(seckillId, SeckillStateEnum.stateOf(result)); } } catch (Exception e) { logger.error(e.getMessage(), e); return new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR); } } }
SeckillServiceImpl implements SeckillService { @Override public SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5) { if (md5 == null || !md5.equals(getMD5(seckillId))) { return new SeckillExecution(seckillId, SeckillStateEnum.DATA_REWRITE); } Date killTime = new Date(); Map<String, Object> map = new HashMap<String, Object>(); map.put("seckillId", seckillId); map.put("phone", userPhone); map.put("killTime", killTime); map.put("result", null); try { seckillDao.killByProcedure(map); int result = MapUtils.getInteger(map, "result", -2); if (result == 1) { SuccessKilled sk = successKilledDao.queryByIdWithSeckill(seckillId, userPhone); return new SeckillExecution(seckillId, SeckillStateEnum.SUCCESS, sk); } else { return new SeckillExecution(seckillId, SeckillStateEnum.stateOf(result)); } } catch (Exception e) { logger.error(e.getMessage(), e); return new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR); } }  }
SeckillServiceImpl implements SeckillService { @Override public SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5) { if (md5 == null || !md5.equals(getMD5(seckillId))) { return new SeckillExecution(seckillId, SeckillStateEnum.DATA_REWRITE); } Date killTime = new Date(); Map<String, Object> map = new HashMap<String, Object>(); map.put("seckillId", seckillId); map.put("phone", userPhone); map.put("killTime", killTime); map.put("result", null); try { seckillDao.killByProcedure(map); int result = MapUtils.getInteger(map, "result", -2); if (result == 1) { SuccessKilled sk = successKilledDao.queryByIdWithSeckill(seckillId, userPhone); return new SeckillExecution(seckillId, SeckillStateEnum.SUCCESS, sk); } else { return new SeckillExecution(seckillId, SeckillStateEnum.stateOf(result)); } } catch (Exception e) { logger.error(e.getMessage(), e); return new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR); } }  @Override List<Seckill> getSeckillList(); @Override Seckill getById(long seckillId); @Override Exposer exportSeckillUrl(long seckillId); @Override @Transactional /** * 使用注解控制事务方法的优点： 1.开发团队达成一致约定，明确标注事务方法的编程风格 * 2.保证事务方法的执行时间尽可能短，不要穿插其他网络操作，RPC/HTTP请求或者剥离到事务方法外部 * 3.不是所有的方法都需要事务，如只有一条修改操作，只读操作不需要事务控制 */ SeckillExecution executeSeckill(long seckillId, long userPhone, String md5); @Override SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5); }
SeckillServiceImpl implements SeckillService { @Override public SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5) { if (md5 == null || !md5.equals(getMD5(seckillId))) { return new SeckillExecution(seckillId, SeckillStateEnum.DATA_REWRITE); } Date killTime = new Date(); Map<String, Object> map = new HashMap<String, Object>(); map.put("seckillId", seckillId); map.put("phone", userPhone); map.put("killTime", killTime); map.put("result", null); try { seckillDao.killByProcedure(map); int result = MapUtils.getInteger(map, "result", -2); if (result == 1) { SuccessKilled sk = successKilledDao.queryByIdWithSeckill(seckillId, userPhone); return new SeckillExecution(seckillId, SeckillStateEnum.SUCCESS, sk); } else { return new SeckillExecution(seckillId, SeckillStateEnum.stateOf(result)); } } catch (Exception e) { logger.error(e.getMessage(), e); return new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR); } }  @Override List<Seckill> getSeckillList(); @Override Seckill getById(long seckillId); @Override Exposer exportSeckillUrl(long seckillId); @Override @Transactional /** * 使用注解控制事务方法的优点： 1.开发团队达成一致约定，明确标注事务方法的编程风格 * 2.保证事务方法的执行时间尽可能短，不要穿插其他网络操作，RPC/HTTP请求或者剥离到事务方法外部 * 3.不是所有的方法都需要事务，如只有一条修改操作，只读操作不需要事务控制 */ SeckillExecution executeSeckill(long seckillId, long userPhone, String md5); @Override SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5);  }