//ActController.java
@RequestMapping("/get_login_total_days_prize")
@ResponseBody
public Res getLoginTotalDaysPrize(@RequestParam(value = "data", required = false) String data, @ModelAttribute("userInfo") UserInfo userInfo){
Res res = new Res();
try {
Map reqDataMap = JsonUtils.convertJsonToMap(data);
int days = 0;
if (reqDataMap.get("days") != null) {
days = Integer.valueOf(reqDataMap.get("days").toString());
}
res = actLoginService.getLoginTotalDaysPrize(userInfo, days);
}catch(ServiceException e){
res.setMsg(e.getMessage());
}
return userTaskService.refreshRes(res, userInfo);
}
//ActService
@Transactional
public Res getLoginTotalDaysPrize(UserInfo userInfo, int days) throws ServiceException {
//每个用户操作一个乐观锁,如果当前操作已存在锁则直接返回
Res res = new Res(userInfo.getUserId());
try {
ActLogin actLogin = actLoginDao.get(userInfo.getUserId());
List totalDaysPrize = JsonUtils.convertJsonToList(actLogin.getTotalDaysPrize());
if (totalDaysPrize.contains(days)) {
throw new ServiceException(messages.getMessage("LoginPrize.GetPrize"));// 奖品已领过了
}
if (actLogin.getTotalDays() < days) {
throw new ServiceException(messages.getMessage("LoginPrize.GetPrizeTimesErr"));// 未达到连续登录天数
}
//省略一些操作...
Map tdpMap = new HashMap();
//省略一些操作...
res.addResponse("total_days_prize", tdpMap);
res.commit();
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new ServiceException(e);
}
return res;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//ActController.java
@RequestMapping("/get_login_total_days_prize")
@ResponseBody
public Res getLoginTotalDaysPrize(@RequestParam(value = "data", required = false) String data, @ModelAttribute("userInfo") UserInfo userInfo){
Res res = new Res();
try {
Map reqDataMap = JsonUtils.convertJsonToMap(data);
int days = 0;
if (reqDataMap.get("days") != null) {
days = Integer.valueOf(reqDataMap.get("days").toString());
}
res = actLoginService.getLoginTotalDaysPrize(userInfo, days);
}catch(ServiceException e){
res.setMsg(e.getMessage());
}
return userTaskService.refreshRes(res, userInfo);
}
//Res.java
public class Res {
private boolean res = false;
private String msg = "";
private int userId;
private int lockKey;
private Map data = new HashMap();
private UserLockDao userLockDao = null;
private Messages messages;
//省略一些方法...
}
这些代码的目的很明显,ActController调用ActService的方法,并将返回类型为Res的结果输出到Web端。但这些代码写得确实烂.(别喷我)。这里分析并优化上述代码。
完成ActController到ActService的方法调用至少也要创建2个Res对象,ActController创建一个,ActService创建一个,这样将会生成大量的垃圾。
我们完全可以将Res作为ActService方法函数的入参,并在方法执行完后返回。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//ActController.java
@RequestMapping("/get_login_total_days_prize")
@ResponseBody
public Res getLoginTotalDaysPrize(@RequestParam(value = "data", required = false) String data, @ModelAttribute("userInfo") UserInfo userInfo){
Res res = new Res();
try {
Map reqDataMap = JsonUtils.convertJsonToMap(data);
int days = 0;
if (reqDataMap.get("days") != null) {
days = Integer.valueOf(reqDataMap.get("days").toString());
}
//将Res作为入参 res=actLoginService.getLoginTotalDaysPrize(res,userInfo, days);
}catch(ServiceException e){
res.setMsg(e.getMessage());
}
return userTaskService.refreshRes(res, userInfo);
}
//ActService.java
@Transactional
public Res getLoginTotalDaysPrize(Res res,UserInfo userInfo, int days) throws ServiceException {
try {
//每个用户操作一个乐观锁,如果当前操作已存在锁则直接返回
res.lock(userInfo.getUserId());
ActLogin actLogin = actLoginDao.get(userInfo.getUserId());
List totalDaysPrize = JsonUtils.convertJsonToList(actLogin.getTotalDaysPrize());
if (totalDaysPrize.contains(days)) {
throw new ServiceException(messages.getMessage("LoginPrize.GetPrize"));// 奖品已领过了
}
if (actLogin.getTotalDays() < days) {
throw new ServiceException(messages.getMessage("LoginPrize.GetPrizeTimesErr"));// 未达到连续登录天数
}
//省略一些操作
Map tdpMap = new HashMap();
//省略一些操作
res.addResponse("total_days_prize", tdpMap);
res.commit();
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new ServiceException(e);
}
return res;
}
阅读(1317) | 评论(0) | 转发(0) |