|
| 1 | +// Copyright (c) 2017 Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | +// info@rabbitmq.com. |
| 15 | + |
| 16 | + |
| 17 | +package com.rabbitmq.client.test; |
| 18 | + |
| 19 | +import com.rabbitmq.client.*; |
| 20 | +import org.junit.After; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | + |
| 31 | +public class RpcTest { |
| 32 | + |
| 33 | + Connection clientConnection, serverConnection; |
| 34 | + Channel clientChannel, serverChannel; |
| 35 | + String queue = "rpc.queue"; |
| 36 | + RpcServer rpcServer; |
| 37 | + |
| 38 | + |
| 39 | + @Before public void init() throws Exception { |
| 40 | + clientConnection = TestUtils.connectionFactory().newConnection(); |
| 41 | + clientChannel = clientConnection.createChannel(); |
| 42 | + serverConnection = TestUtils.connectionFactory().newConnection(); |
| 43 | + serverChannel = serverConnection.createChannel(); |
| 44 | + serverChannel.queueDeclare(queue, false, false, false, null); |
| 45 | + } |
| 46 | + |
| 47 | + @After public void tearDown() throws Exception { |
| 48 | + if(rpcServer != null) { |
| 49 | + rpcServer.terminateMainloop(); |
| 50 | + } |
| 51 | + if(serverChannel != null) { |
| 52 | + serverChannel.queueDelete(queue); |
| 53 | + } |
| 54 | + clientConnection.close(); |
| 55 | + serverConnection.close(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void rpc() throws Exception { |
| 60 | + rpcServer = new TestRpcServer(serverChannel, queue); |
| 61 | + new Thread(new Runnable() { |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + try { |
| 65 | + rpcServer.mainloop(); |
| 66 | + } catch (Exception e) { |
| 67 | + // safe to ignore when loops ends/server is canceled |
| 68 | + } |
| 69 | + } |
| 70 | + }).start(); |
| 71 | + |
| 72 | + RpcClient client = new RpcClient(clientChannel, "", queue, 1000); |
| 73 | + RpcClient.Response response = client.doCall(null, "hello".getBytes()); |
| 74 | + assertEquals("*** hello ***", new String(response.getBody())); |
| 75 | + assertEquals("pre-hello", response.getProperties().getHeaders().get("pre").toString()); |
| 76 | + assertEquals("post-hello", response.getProperties().getHeaders().get("post").toString()); |
| 77 | + client.close(); |
| 78 | + } |
| 79 | + |
| 80 | + private static class TestRpcServer extends RpcServer { |
| 81 | + |
| 82 | + public TestRpcServer(Channel channel, String queueName) throws IOException { |
| 83 | + super(channel, queueName); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected AMQP.BasicProperties preprocessReplyProperties(QueueingConsumer.Delivery request, AMQP.BasicProperties.Builder builder) { |
| 88 | + Map<String, Object> headers = new HashMap<String, Object>(); |
| 89 | + headers.put("pre", "pre-" + new String(request.getBody())); |
| 90 | + builder.headers(headers); |
| 91 | + return builder.build(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public byte[] handleCall(QueueingConsumer.Delivery request, AMQP.BasicProperties replyProperties) { |
| 96 | + String input = new String(request.getBody()); |
| 97 | + return ("*** " + input + " ***").getBytes(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected AMQP.BasicProperties postprocessReplyProperties(QueueingConsumer.Delivery request, AMQP.BasicProperties.Builder builder) { |
| 102 | + Map<String, Object> headers = new HashMap<String, Object>(builder.build().getHeaders()); |
| 103 | + headers.put("post", "post-" + new String(request.getBody())); |
| 104 | + builder.headers(headers); |
| 105 | + return builder.build(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments