|
| 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