/
BasicTest.java
276 lines (239 loc) · 9.47 KB
/
BasicTest.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.hry.spring.rabbitmq.basic;
import com.hry.spring.rabbitmq.basic.header.HeaderRecv;
import com.hry.spring.rabbitmq.basic.header.HeaderSend;
import com.hry.spring.rabbitmq.basic.helloworld.HelloworldRecv;
import com.hry.spring.rabbitmq.basic.helloworld.HelloworldSend;
import com.hry.spring.rabbitmq.basic.publishsubscribe.Publish;
import com.hry.spring.rabbitmq.basic.publishsubscribe.Subscribe;
import com.hry.spring.rabbitmq.basic.routing.RoutingRecv;
import com.hry.spring.rabbitmq.basic.routing.RoutingSend;
import com.hry.spring.rabbitmq.basic.rpc.RpcClient;
import com.hry.spring.rabbitmq.basic.rpc.RpcServer;
import com.hry.spring.rabbitmq.basic.topics.TopicsRecv;
import com.hry.spring.rabbitmq.basic.topics.TopicsSend;
import com.hry.spring.rabbitmq.basic.workqueues.WorkQueuesRecv;
import com.hry.spring.rabbitmq.basic.workqueues.WorkQueuesSend;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by huangrongyou@yixin.im on 2018/1/10.
*/
public class BasicTest {
// 测试线程池
private ExecutorService executorService = Executors.newFixedThreadPool(10);
// rabbitmq的IP地址
private final String rabbitmq_host = "10.240.80.147";
// rabbitmq的用户名称
private final String rabbitmq_user = "hry";
// rabbitmq的用户密码
private final String rabbitmq_pwd = "hry";
@Test
public void helloworld() throws InterruptedException {
// 接收端
executorService.submit(() -> {
HelloworldRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd);
});
Thread.sleep(5* 100);
// 发送端
executorService.submit(() -> {
HelloworldSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd);
});
// sleep 10s
Thread.sleep(10 * 1000);
}
@Test
public void workqueues() throws InterruptedException {
// 接收端
int recNum = 2;
while(recNum-- > 0) {
final int recId = recNum;
executorService.submit(() -> {
WorkQueuesRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, recId);
});
}
Thread.sleep(5* 100);
// 发送端
int sendNum = 4;
while(sendNum-- > 0){
executorService.submit(() -> {
WorkQueuesSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd);
});
}
// sleep 10s
Thread.sleep(10 * 1000);
}
@Test
public void publishsubscribe() throws InterruptedException {
// 接收端
int recNum = 2;
while(recNum-- > 0) {
final int recId = recNum;
executorService.submit(() -> {
Subscribe.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, recId);
});
}
Thread.sleep(5* 100);
// 发送端
int sendNum = 2;
while(sendNum-- > 0){
executorService.submit(() -> {
Publish.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd);
});
}
// sleep 10s
Thread.sleep(10 * 1000);
}
@Test
public void routing_1() throws InterruptedException {
// 接收端 1:绑定 orange 值
executorService.submit(() -> {
String[] colours = {"orange"};
RoutingRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, colours);
});
// 接收端 2:绑定 black、green 值
executorService.submit(() -> {
String[] colours = {"black","green"};
RoutingRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, colours);
});
Thread.sleep(5* 100);
// 发送端 : 发送 black,只有接收端1收到
executorService.submit(() -> {
String routing = "orange";
RoutingSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, routing);
});
// 发送端 : 发送 green、black,只有接收端2收到
executorService.submit(() -> {
String routing = "green";
RoutingSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, routing);
});
// sleep 10s
Thread.sleep(10 * 1000);
}
@Test
public void routing_2() throws InterruptedException {
// 接收端:同时创建两个接收端,同时绑定black
int recNum = 2;
while(recNum-- > 0) {
// 接收端:绑定 black 值
executorService.submit(() -> {
String[] colours = {"black"};
RoutingRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, colours);
});
}
Thread.sleep(5* 100);
// 发送端1 : 发送 black,所有的接收端都会收到
executorService.submit(() -> {
String routing = "black";
RoutingSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, routing);
});
// 发送端2 : 发送 green,所有的接收端都不会收到
executorService.submit(() -> {
String routing = "green";
RoutingSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, routing);
});
// sleep 10s
Thread.sleep(10 * 1000);
}
@Test
public void topics() throws InterruptedException {
// 消费者1:绑定 *.orange.* 值
executorService.submit(() -> {
String[] bindingKeys = {"*.orange.*"};
TopicsRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, bindingKeys);
});
// 消费者2:绑定 "*.*.rabbit" 和 "lazy.#"值
executorService.submit(() -> {
String[] bindingKeys = {"*.*.rabbit", "lazy.#"};
TopicsRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, bindingKeys);
});
Thread.sleep(5* 100);
// 生产者1 : 发送 black,所有的接收端都会收到
executorService.submit(() -> {
String routing = "quick.orange.rabbit";
TopicsSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, routing);
});
// 生产者2 : 发送 green,所有的接收端都不会收到
executorService.submit(() -> {
String routing = "lazy.pink.rabbit";
TopicsSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, routing);
});
// sleep 10s
Thread.sleep(10 * 1000);
}
@Test
public void rpc() throws InterruptedException {
// rpc服务端
executorService.submit(() -> {
RpcServer.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd);
});
// rpc客户端
executorService.submit(() -> {
RpcClient.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, "rpc test");
});
// sleep 10s
Thread.sleep(10 * 1000);
}
@Test
public void header() throws InterruptedException {
// 消费者1:绑定 format=pdf,type=report
executorService.submit(() -> {
Map<String,Object> headers = new HashMap();
headers.put("format","pdf");
headers.put("type","report");
headers.put("x-match","all");
HeaderRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, headers);
});
// 消费者2:绑定 format=pdf,type=log
executorService.submit(() -> {
Map<String,Object> headers = new HashMap();
headers.put("format","pdf");
headers.put("type","log");
headers.put("x-match","any");
HeaderRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, headers);
});
// 消费者3:绑定 format=zip,type=report
executorService.submit(() -> {
Map<String,Object> headers = new HashMap();
headers.put("format","zip");
headers.put("type","report");
headers.put("x-match","all");
// headers.put("x-match","any");
HeaderRecv.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, headers);
});
Thread.sleep(2* 1000);
System.out.println("=============消息1===================");
// 生产者1 : format=pdf,type=reprot,x-match=all
executorService.submit(() -> {
Map<String,Object> headers = new HashMap();
headers.put("format","pdf");
headers.put("type","report");
// headers.put("x-match","all");
HeaderSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, headers);
});
Thread.sleep(5* 100);
System.out.println("=============消息2===================");
// 生产者2 : format=pdf,x-match=any
executorService.submit(() -> {
Map<String,Object> headers = new HashMap();
headers.put("format","pdf");
// headers.put("x-match","any");
HeaderSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, headers);
});
Thread.sleep(5* 100);
System.out.println("=============消息3===================");
// 生产者1 : format=zip,type=log,x-match=all
executorService.submit(() -> {
Map<String,Object> headers = new HashMap();
headers.put("format","zip");
headers.put("type","log");
// headers.put("x-match","all");
HeaderSend.execute(rabbitmq_host, rabbitmq_user, rabbitmq_pwd, headers);
});
// sleep 10s
Thread.sleep(10 * 1000);
}
}