Skip to content

Commit 720abf2

Browse files
committed
add README
1 parent 9a814d9 commit 720abf2

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

uia.nms/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
NMS
2+
===
3+
4+
## ActiveMQ
5+
### Topic
6+
Publisher
7+
```java
8+
NmsEndPoint endPoint = new NmsEndPoint(null, null, "tcp://localhost", "61616");
9+
NmsProducer pub = new AmqTopicFactory().createProducer(endPoint);
10+
pub.start();
11+
pub.send(
12+
"NMS.AMQ.TEST", // name
13+
"data", // label of the message
14+
"Hello judy", // message
15+
false); // persistent flag
16+
pub.stop();
17+
```
18+
19+
Subscriber
20+
```java
21+
NmsEndPoint endPoint = new NmsEndPoint(null, null, "tcp://localhost", "61616");
22+
NmsConsumer sub = new AmqTopicFactory().createConsumer(endPoint);
23+
sub.addLabel("data"); // receive messages with label 'data'
24+
sub.addMessageListener(new NmsMessageListener() {
25+
26+
@Override
27+
public void messageReceived(NmsConsumer sub, MessageHeader header, MessageBody body) {
28+
System.out.println(body.getContent().get("data"));
29+
}
30+
});
31+
32+
sub.start("NMS.AMQ.TEST"); // listen queue 'NMS.AMQ.TEST'.
33+
```
34+
35+
36+
### Queue
37+
Producer
38+
```java
39+
NmsEndPoint endPoint = new NmsEndPoint(null, null, "tcp://localhost", "61616");
40+
NmsProducer pro = new AmqQueueFactory().createProducer(endPoint);
41+
pro.start();
42+
pro.send(
43+
"NMS.AMQ.TEST", // name
44+
"data", // label of the message
45+
"Hello Judy", // message
46+
false); // persistent flag
47+
pro.stop();
48+
```
49+
50+
Consumer
51+
```java
52+
NmsEndPoint endPoint = new NmsEndPoint(null, null, "tcp://localhost", "61616");
53+
NmsConsumer con = new AmqQueueFactory().createConsumer(endPoint);
54+
con.addLabel("data"); // receive messages with label 'data'
55+
con.addMessageListener(new NmsMessageListener() {
56+
57+
@Override
58+
public void messageReceived(NmsConsumer sub, MessageHeader header, MessageBody body) {
59+
System.out.println("got:" + body.getContent().get("data"));
60+
}
61+
62+
});
63+
64+
con.start("NMS.AMQ.TEST"); // listen queue 'NMS.AMQ.TEST'.
65+
```
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*******************************************************************************
2+
* Copyright 2018 UIA
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership.
7+
* The ASF licenses this file to You under the Apache License, Version 2.0
8+
* (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*******************************************************************************/
19+
package uia.nms.amq;
20+
21+
import org.junit.Test;
22+
23+
import uia.nms.MessageBody;
24+
import uia.nms.MessageHeader;
25+
import uia.nms.NmsConsumer;
26+
import uia.nms.NmsEndPoint;
27+
import uia.nms.NmsMatching;
28+
import uia.nms.NmsMessageListener;
29+
import uia.nms.NmsProducer;
30+
31+
public class AmqTopicProducerTest implements NmsMatching {
32+
33+
private NmsEndPoint endPoint;
34+
35+
private int index;
36+
37+
public AmqTopicProducerTest() {
38+
this.endPoint = new NmsEndPoint(null, null, "tcp://localhost", "61616");
39+
}
40+
41+
@Test
42+
public void testReply1() throws Exception {
43+
final NmsConsumer sub = new AmqTopicFactory().createConsumer(this.endPoint);
44+
sub.addLabel("data");
45+
sub.addMessageListener(new NmsMessageListener() {
46+
47+
@Override
48+
public void messageReceived(NmsConsumer sub, MessageHeader header, MessageBody body) {
49+
String name = body.getContent().get("data");
50+
System.out.println("message=" + name + ", response=" + header.responseSubject + ", cid=" + header.correlationID);
51+
String reply = "Hello " + name;
52+
53+
NmsProducer subpub = sub.createProducer();
54+
subpub.send(header.responseSubject, "data", reply, false, header.correlationID);
55+
}
56+
});
57+
sub.start("NMS.AMQ.TEST");
58+
59+
final NmsProducer pub = new AmqTopicFactory().createProducer(this.endPoint);
60+
pub.start();
61+
String result = pub.send(
62+
"NMS.AMQ.TEST",
63+
"data",
64+
"Judy",
65+
false,
66+
3000);
67+
System.out.println("Get reply: " + result);
68+
69+
Thread.sleep(1000);
70+
pub.stop();
71+
sub.stop();
72+
}
73+
74+
@Test
75+
public void testReply2() throws Exception {
76+
final NmsConsumer sub = new AmqTopicFactory().createConsumer(this.endPoint);
77+
sub.addLabel("data");
78+
sub.addMessageListener(new NmsMessageListener() {
79+
80+
@Override
81+
public void messageReceived(NmsConsumer sub, MessageHeader header, MessageBody body) {
82+
String name = body.getContent().get("data");
83+
System.out.println("message=" + name + ", response=" + header.responseSubject + ", cid=" + header.correlationID);
84+
String reply = "Hello " + name;
85+
86+
NmsProducer subpub = sub.createProducer();
87+
subpub.send(header.responseSubject, "data", reply, false, header.correlationID);
88+
subpub.send(header.responseSubject, "data", reply + " how are you", false, header.correlationID);
89+
}
90+
});
91+
92+
sub.start("NMS.AMQ.TEST");
93+
94+
this.index = 0;
95+
final NmsProducer pub = new AmqTopicFactory().createProducer(this.endPoint);
96+
pub.start();
97+
String result = pub.send(
98+
"NMS.AMQ.TEST",
99+
"data",
100+
"Judy",
101+
false,
102+
3000,
103+
"NMS.AMQ.TEST.REPLY", // set a specific reply name.
104+
this); // match helper
105+
System.out.println("Reply: " + result);
106+
107+
Thread.sleep(1000);
108+
pub.stop();
109+
sub.stop();
110+
}
111+
112+
113+
@Test
114+
public void testReply3() throws Exception {
115+
final NmsConsumer sub1 = new AmqTopicFactory().createConsumer(this.endPoint);
116+
sub1.addLabel("data");
117+
sub1.addMessageListener(new NmsMessageListener() {
118+
119+
@Override
120+
public void messageReceived(NmsConsumer sub, MessageHeader header, MessageBody body) {
121+
NmsProducer subpub = sub.createProducer();
122+
String name = body.getContent().get("data");
123+
System.out.println("sub1> message=" + name + ", response=" + header.responseSubject + ", cid=" + header.correlationID);
124+
String reply = "Hello " + name;
125+
subpub.send(header.responseSubject, "data", reply, false, header.correlationID);
126+
subpub.send(header.responseSubject, "data", reply + " how are you", false, header.correlationID);
127+
}
128+
});
129+
sub1.start("NMS.AMQ.TEST");
130+
131+
final NmsConsumer sub2 = new AmqTopicFactory().createConsumer(this.endPoint);
132+
sub2.addLabel("data");
133+
sub2.addMessageListener(new NmsMessageListener() {
134+
135+
@Override
136+
public void messageReceived(NmsConsumer sub, MessageHeader header, MessageBody body) {
137+
NmsProducer subpub = sub.createProducer();
138+
String name = body.getContent().get("data");
139+
System.out.println("sub2> message=" + name + ", response=" + header.responseSubject + ", cid=" + header.correlationID);
140+
String reply = "Good day " + name;
141+
subpub.send(header.responseSubject, "data", reply, false, header.correlationID);
142+
subpub.send(header.responseSubject, "data", reply + " how are you", false, header.correlationID);
143+
}
144+
});
145+
sub2.start("NMS.AMQ.TEST");
146+
147+
this.index = 0;
148+
final NmsProducer pub = new AmqTopicFactory().createProducer(this.endPoint);
149+
pub.start();
150+
String result = pub.send(
151+
"NMS.AMQ.TEST",
152+
"data",
153+
"Judy",
154+
false,
155+
3000,
156+
"NMS.AMQ.TEST.REPLY", // set a specific reply name.
157+
this); // match helper
158+
System.out.println("Reply: " + result);
159+
160+
Thread.sleep(1000);
161+
pub.stop();
162+
sub1.stop();
163+
sub2.stop();
164+
}
165+
166+
@Override
167+
public boolean check(String message) {
168+
boolean result = this.index++ > 0;
169+
System.out.println("match> " + message + ", " + result);
170+
return result;
171+
}
172+
}

0 commit comments

Comments
 (0)