From c40b30d9ef321735b8ae9933cfce48975ab6ed41 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Fri, 16 Apr 2021 12:41:19 +0530 Subject: [PATCH] RPC Sample with custom reply queue --- README.md | 3 + rpc/ruby-server/simple-server.rb | 13 +- rpc/simple/rx-rpc-explicit-return-dest.html | 150 ++++++++++++++++++++ rpc/simple/rx-rpc-server.html | 6 +- rpc/simple/rx-rpc.html | 6 +- 5 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 rpc/simple/rx-rpc-explicit-return-dest.html diff --git a/README.md b/README.md index 45f6153..7553c85 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ See the client at: - [rpc/simple/rx-rpc.html](rpc/simple/rx-rpc.html). - Click the button `Submit Problem` many times to see each one getting submitted as independent requests. +- There is an alternate client sample at + [rpc/simple/rx-rpc-explicit-return-dest.html](rpc/simple/rx-rpc-explicit-return-dest.html). + This uses custom reply queue. This client is only tested with the simple ruby server. You will need to run at least one of the following servers: diff --git a/rpc/ruby-server/simple-server.rb b/rpc/ruby-server/simple-server.rb index 3a2a2f1..150af2a 100644 --- a/rpc/ruby-server/simple-server.rb +++ b/rpc/ruby-server/simple-server.rb @@ -15,7 +15,6 @@ # rpc-server-thread-pool.rb ############################################################################### - amqp_conn = Bunny.new amqp_conn.start @@ -29,7 +28,7 @@ # Process the request, compute the response operands = JSON.parse(payload) - result = {result: operands['x'].to_i + operands['y'].to_i} + result = { result: operands['x'].to_i + operands['y'].to_i } response_body = result.to_json # Completed processing @@ -37,5 +36,13 @@ default_exchange = channel.default_exchange - default_exchange.publish response_body, routing_key: metadata[:reply_to], correlation_id: metadata[:correlation_id] + reply_to = metadata[:reply_to] + + # if the RPC client uses explicit queue name for replies, it needs to be translated from STOMP + # semantics to AMQP. In this just removing the prefix /queue/ is sufficient + # See https://www.rabbitmq.com/stomp.html#d for details + reply_to = reply_to.sub(%r{^/queue/}, '') + + puts "Sent with routing key: #{reply_to}" + default_exchange.publish response_body, routing_key: reply_to, correlation_id: metadata[:correlation_id] end diff --git a/rpc/simple/rx-rpc-explicit-return-dest.html b/rpc/simple/rx-rpc-explicit-return-dest.html new file mode 100644 index 0000000..db5d9ab --- /dev/null +++ b/rpc/simple/rx-rpc-explicit-return-dest.html @@ -0,0 +1,150 @@ + + + + + Simple RPC Application using RxStompRPC and RxJS + + + +
+ + +
+ + +
+ + + + + + + + + + + + diff --git a/rpc/simple/rx-rpc-server.html b/rpc/simple/rx-rpc-server.html index 4096124..d791660 100644 --- a/rpc/simple/rx-rpc-server.html +++ b/rpc/simple/rx-rpc-server.html @@ -13,6 +13,10 @@

Simple RPC Server

  • Adjust the configuration as per your STOMP broker.
  • +
  • + This example uses queue destination of RabbitMQ. The queue must be created before running the sample. + The ruby server creates the queue if it is not there. +
  • The server endpoint will wait for a random period (max 1000ms) before responding.
  • @@ -110,4 +114,4 @@

    Simple RPC Server

    }); - \ No newline at end of file + diff --git a/rpc/simple/rx-rpc.html b/rpc/simple/rx-rpc.html index f27c833..52070f7 100644 --- a/rpc/simple/rx-rpc.html +++ b/rpc/simple/rx-rpc.html @@ -16,6 +16,10 @@ API Reference +
  • + This example uses queue destination of RabbitMQ. The queue must be created before running the sample. + The ruby server creates the queue if it is not there. +
  • Start the server by opening RPC Server in another browser tab/window.
  • Alternatively run any of the Ruby servers, see README for details.
  • @@ -123,4 +127,4 @@ }) - \ No newline at end of file +