EcEntrustInnovateHandler.java
3.14 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
package com.cjs.site.biz.ec;
import com.cjs.site.dao.user.interact.UserAttentionDao;
import com.cjs.site.model.entrust.EcEntrustInfo;
import com.cjs.site.model.user.UserAttentionInfo;
import com.cjs.site.util.lang.JsonUtil;
import com.cjs.site.util.redis.JedisTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Service("ecEntrustInnovateHandler")
public class EcEntrustInnovateHandler extends TextWebSocketHandler
{
public static Map<WebSocketSession, List<UserAttentionInfo>> WEBSOCKET_SESSIONS = new HashMap();
Logger log = LogManager.getLogger();
@Autowired
private JedisTemplate jedisTemplate;
@Autowired
private UserAttentionDao userAttentionDao;
@Autowired
private EcEntrustInfoBiz ecEntrustInfoBiz;
@Value("${${env}.innovate.otcCodes}")
private String innovateOtcCodes;
public void afterConnectionEstablished(WebSocketSession session) throws Exception { super.afterConnectionEstablished(session);
}
protected void handleTextMessage(WebSocketSession session, TextMessage message)
throws Exception
{
super.handleTextMessage(session, message);
try
{
String userId = (String)message.getPayload();
if (StringUtils.isBlank(userId)) {
return;
}
String[] codes = this.innovateOtcCodes.split(",");
String payload = this.jedisTemplate.get("entrust:entrusting");
if (StringUtils.isNotBlank(payload)) {
if (payload.indexOf("[") != -1) {
payload = payload.substring(payload.indexOf("["));
}
List entrusts = (List)JsonUtil.fromJson(payload, ArrayList.class,
EcEntrustInfo.class);
this.ecEntrustInfoBiz.sendToInnovate(session, entrusts, codes);
}
} catch (Exception e) {
this.log.error("官网买卖,用户关注藏品连接出错。", e);
throw e;
}
}
public void afterConnectionClosed(WebSocketSession session, CloseStatus status)
throws Exception
{
super.afterConnectionClosed(session, status);
WEBSOCKET_SESSIONS.remove(session);
}
public void handleTransportError(WebSocketSession session, Throwable exception)
throws Exception
{
super.handleTransportError(session, exception);
this.log.error("WebScoket传输出错[" + session.getId() + "]:" + exception.getMessage());
if (session.isOpen()) {
session.close();
}
WEBSOCKET_SESSIONS.remove(session);
}
}