AttributeHandler.java
6.13 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.dili.titan.handler;
import com.dili.titan.domain.Attribute;
import com.dili.titan.domain.AttributeValue;
import com.dili.titan.domain.RedisKeyConstant;
import com.dili.titan.domain.log.LogHelper;
import com.dili.titan.domain.log.LogTypeEnum;
import com.dili.titan.mq.sender.TopicProducer;
import com.dili.titan.redis.JedisClient;
import com.dili.titan.service.impl.AttributeService;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
/**
* <B>Description</B> TODO 属性监控<br />
* <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
* 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
* <B>Company</B> 地利集团
* @createTime 2014年9月22日 下午2:49:22
* @author yangjianjun
*/
@Component
public class AttributeHandler implements BaseHandler {
private ObjectMapper mapper = new ObjectMapper();
@Resource(name = "attributeService")
private AttributeService attributeService;
@Resource(name = "jedisClient")
private JedisClient jedisClient;
@Resource(name = "topicProducer")
private TopicProducer topicProducer;
@Override
public void handle(String message) throws Exception {
try {
JsonNode node = mapper.readTree(message);
long attrId = node.get("attrId").asLong();
int operType = node.get("oper").asInt();
switch (operType) {
case 1:
add(attrId);
topicProducer.sendMQAttribute(attrId, operType);
break;
case 2:
update(attrId);
topicProducer.sendMQAttribute(attrId, operType);
break;
case 3:
delete(attrId);
topicProducer.sendMQAttribute(attrId, operType);
break;
default:
LogHelper.error(LogTypeEnum.ATTRIBUTE,
"Attribute operType error");
break;
}
} catch (Exception e) {
LogHelper.error(LogTypeEnum.ATTRIBUTE, e, "handle error!",
e.getMessage());
throw e;
}
}
/**
* 新增属性
*
* @param attrId
*/
private void add(long attrId) throws Exception {
putAddUpdateAttr(attrId);
putAddUpdateAttrValue(attrId);
}
/**
* 修改属性
*
*/
private void update(long attrId) throws Exception {
putAddUpdateAttr(attrId);
putAddUpdateAttrValue(attrId);
}
private void delete(long attrId) throws Exception {
delAttr(attrId);
delAttrValue(attrId);
}
private void putAddUpdateAttr(long attrId) throws Exception {
try {
Attribute attribute = attributeService.getAttributeById(attrId);
String json = mapper.writeValueAsString(attribute);
jedisClient.hput(RedisKeyConstant.ATTRIBUTE,
String.valueOf(attribute.getAttrId()), json,JedisClient.expireTime);
List<Attribute> attributes = attributeService.getAllAttribute();
for (Attribute a : attributes) {
try {
String jsons = mapper.writeValueAsString(a);
jedisClient.hput(RedisKeyConstant.ATTRIBUTE,
String.valueOf(a.getAttrId()), jsons,JedisClient.expireTime);
putAddUpdateAttrValue(a.getAttrId());
} catch (Exception e) {
LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
"load attribute error!attrId={}", attrId);
throw e;
}
}
} catch (Exception e) {
LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
"load addAttr error!attrId={}", attrId);
throw e;
}
}
private void putAddUpdateAttrValue(long attrId) throws Exception {
try {
List<AttributeValue> values = attributeService
.getAttributeValues(attrId);
String json = mapper.writeValueAsString(values);
jedisClient.hput(RedisKeyConstant.ATTR_VALUE, String.valueOf(attrId), json,JedisClient.expireTime);
} catch (Exception e) {
LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
"load addAttrValue error!attrId={}", attrId);
throw e;
}
}
private void delAttr(long attrId) throws Exception {
try {
jedisClient.hdel(RedisKeyConstant.ATTRIBUTE, String.valueOf(attrId));
putAddUpdateAttr(attrId);
} catch (Exception e) {
LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
"load delAttr error!attrId={}", attrId);
throw e;
}
}
private void delAttrValue(long attrId) throws Exception {
try {
jedisClient.hdel(RedisKeyConstant.ATTR_VALUE, String.valueOf(attrId));
putAddUpdateAttrValue(attrId);
} catch (Exception e) {
LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
"load delAttrValue error!attrId={}", attrId);
throw e;
}
}
/**
* 应用启动时自动执行load方法,加载所有的属性信息
*
*/
@PostConstruct
public void load() {
LogHelper.info(LogTypeEnum.ATTRIBUTE, "begin load attribute info");
List<Attribute> attributes = attributeService.getAllAttribute();
for (Attribute a : attributes) {
try {
String json = mapper.writeValueAsString(a);
jedisClient.hput(RedisKeyConstant.ATTRIBUTE, String.valueOf(a.getAttrId()),
json,JedisClient.expireTime);
putAddUpdateAttrValue(a.getAttrId());
} catch (Exception e) {
LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
"load attribute error!attrId={}", a.getAttrId());
}
}
LogHelper.info(LogTypeEnum.ATTRIBUTE, "load attribute info done");
}
}