/
RmqClient.java
224 lines (196 loc) · 6.23 KB
/
RmqClient.java
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListener;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.consumer.ConsumeFromWhere;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageExt;
/**
* Created by maotz on 2015-05-09.
* 消息队列客户端
*/
public class RmqClient implements IRmqClient {
private final static Logger logger = LoggerFactory.getLogger(RmqClient.class);
private DefaultMQProducer mqProducer = null;
private DefaultMQPushConsumer mqConsumer = null;
private String mqGroupName = "group";
private String mqInstanceName = "instance";
private String mqNameServers;
private boolean active;
private final boolean enableRecv;
private final boolean enableSend;
private final RmqListeners listeners = new RmqListeners();
public RmqClient(boolean _enable_recv, boolean _enable_send){
enableRecv = _enable_recv;
enableSend = _enable_send;
}
private final MessageListener mqListener = new MessageListenerConcurrently() {
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
for(MessageExt msg : msgs)
onReceive(msg);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
};
protected void onReceive(MessageExt _msg){
listeners.onRmqMsg(_msg);
}
/**
* 生成消费者
* @throws Exception
*/
private void makeConsumer() throws Exception{
if(null == mqConsumer){
mqConsumer = new DefaultMQPushConsumer(mqGroupName);
mqConsumer.setNamesrvAddr( mqNameServers );
mqConsumer.setInstanceName(mqInstanceName);
mqConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
mqConsumer.registerMessageListener(mqListener);
mqConsumer.start();
logger.info("Consumer-> GroupName:{} InstanceName:{} <{}> is ok.", mqGroupName, mqInstanceName, mqNameServers);
}
}
/**
* 生成生产者
* @throws Exception
*/
private void makeProducer() throws Exception{
if(null == mqProducer){
mqProducer = new DefaultMQProducer(mqGroupName);
mqProducer.setNamesrvAddr(mqNameServers);
mqProducer.setInstanceName(mqInstanceName);
mqProducer.setProducerGroup(mqGroupName);
mqProducer.setRetryAnotherBrokerWhenNotStoreOK(true);
mqProducer.start();
logger.info("Producer-> GroupName:{} InstanceName:{} <{}> is ok.", mqGroupName, mqInstanceName, mqNameServers);
}
}
/**
* 初始化
* @param _group 分组
*/
public void setGroup(String _group){
mqGroupName = _group;
}
/**
* 设置实例名
* @param _instance 实例名
*/
public void setInstance(String _instance){
mqInstanceName = _instance;
}
/**
*
* @param _mqNameServers 服务器地址
*/
public void setup(String _mqNameServers){
mqNameServers = _mqNameServers;
}
/**
* 添加监听器
* @param _listener 监听器
*/
public void addListener(IRmqListener _listener){
listeners.registerListener(_listener);
}
/**
* 开启链接,准备工作
*/
private void start() throws Exception{
if(enableSend)
makeProducer();
if(enableRecv)
makeConsumer();
}
/**
* 停止工作
*/
private void stop(){
if(enableSend)
mqConsumer.shutdown();
if(enableRecv)
mqProducer.shutdown();
}
/**
* 启动,关闭
* @param _active 开关
*/
public void setActive(boolean _active) throws Exception{
if(active!=_active) {
active = _active;
if(active)
start();
else
stop();
}
}
public void subscribe(String _topic) throws Exception{
subscribe(_topic, "*");
}
/**
* 订阅
* @param _topic 主题
* @param _subExpression 条件
*/
public void subscribe(String _topic, String _subExpression) throws Exception{
if(active & enableRecv){
if(null==_subExpression || _subExpression.length()==0)
_subExpression = "*";
mqConsumer.subscribe(_topic, _subExpression);
}
}
/**
* 发送数据
* @param _topic 主题
* @param _body 内容
* @throws Exception
*/
public SendResult send(String _topic, String _body) throws Exception{
return send( _topic, "", "", 0, _body.getBytes() );
}
/**
* 发送数据
* @param _topic
* @param _flag
* @param _body
* @return
* @throws Exception
*/
public SendResult send(String _topic, int _flag, byte[] _body) throws Exception{
return send( _topic, "", "", _flag, _body);
}
/**
* 发送数据
* @param topic 主题
* @param tags 标记
* @param keys 关键字
* @param flag flag
* @param body 内容
* @throws Exception
*/
public SendResult send(String topic, String tags, String keys, int flag, byte[] body) throws Exception{
if(active & enableSend){
Message msg = new Message(topic, tags, keys, flag, body, true);
return mqProducer.send(msg);
}
return null;
}
/**
* 样例
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RmqClient mqClient = new RmqClient(false, true);
mqClient.setGroup("ConsumerApp");
mqClient.setInstance("Consumer");
mqClient.setup("10.10.1.42:9876");
mqClient.addListener(new RmqAdapter());
mqClient.setActive(true);
}
}