TradeSubscribe.java
4.5 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
package com.cjs.site.biz.info;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.cjs.site.biz.ec.EcEntrustAllHandler;
import com.cjs.site.biz.ec.EcEntrustAttentionHandler;
import com.cjs.site.biz.ec.EcEntrustDetailHandler;
import com.cjs.site.biz.ec.EcEntrustHandler;
import com.cjs.site.biz.ec.EcEntrustInfoBiz;
import com.cjs.site.model.entrust.EcEntrustInfo;
import com.cjs.site.util.lang.JsonUtil;
import com.cjs.site.util.redis.JedisTemplate;
import redis.clients.jedis.JedisPubSub;
/**
* Redis消息订阅
*
* @author tongyufu
*
*/
@Service
@Lazy(false)
public class TradeSubscribe implements InitializingBean {
Logger log = LogManager.getLogger();
@Autowired
private TradeBiz tradeBiz;
@Autowired
private EcEntrustInfoBiz entrustInfoBiz;
@Autowired
private JedisTemplate jedisTemplate;
@Override
public void afterPropertiesSet() throws Exception {
//订阅行情。订阅通道:trade:topic
jedisTemplate.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
try {
tradeBiz.sendToClient(message, TradeHandler.WEBSOCKET_SESSIONS);
} catch (IOException e) {
log.error("推送行情失败:" + message, e);
}
}
}, "trade:topic");
//订阅最新委托交易。entrust:last:topic
jedisTemplate.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
try {
message = entrustInfoBiz.processData(message, "entrust");
tradeBiz.sendToClient(message, EcEntrustHandler.WEBSOCKET_SESSIONS);
} catch (IOException e) {
log.error("推送最新发布失败:" + message, e);
}
}
}, "entrust:last:topic");
//订阅最新委托成交。entrust:bargin:topic
jedisTemplate.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
try {
message = entrustInfoBiz.processData(message, "bargin");
tradeBiz.sendToClient(message, EcEntrustHandler.WEBSOCKET_SESSIONS);
} catch (IOException e) {
log.error("推送最新成交失败:" + message, e);
}
}
}, "entrust:bargin:topic");
//订阅官网买卖的全部商品。entrust:entrusting:topic
jedisTemplate.subscribe(new JedisPubSub() {
@SuppressWarnings("unchecked")
@Override
public void onMessage(String channel, String message) {
try {
//全部商品
tradeBiz.sendToClient(message, EcEntrustAllHandler.WEBSOCKET_SESSIONS);
//自选
List<EcEntrustInfo> entrusts = JsonUtil.fromJson(message, ArrayList.class,
EcEntrustInfo.class);
entrustInfoBiz.sendToClient(EcEntrustAttentionHandler.WEBSOCKET_SESSIONS,
entrusts);
} catch (IOException e) {
log.error("推送行情失败:" + message, e);
}
}
}, "entrust:entrusting:topic");
//订阅最新藏品信息。entrust:detail:topic:*
jedisTemplate.psubscribe(new JedisPubSub() {
@SuppressWarnings("unchecked")
@Override
public void onPMessage(String pattern, String channel, String message) {
try {
Map<String, Object> entrust = JsonUtil.fromJson(message, HashMap.class);
String otcCode = (String) entrust.get("mch_code");
tradeBiz.sendToClient(message,
EcEntrustDetailHandler.WEBSOCKET_SESSIONS.get(otcCode));
} catch (IOException e) {
log.error("推送商品详细信息失败:" + message, e);
}
}
}, "entrust:detail:topic:*");
}
}