rpc 双队列
来源:互联网 发布:刘国梁下海经商 知乎 编辑:程序博客网 时间:2024/05/29 15:07
需求分析:客户需要请求时候使用一个队列,响应的时候的使用一个队列
客户端:
package rabbitmq;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Connection;import com.rabbitmq.client.Channel;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.AMQP;import com.rabbitmq.client.Envelope;import java.io.IOException;import java.util.UUID;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.TimeoutException;public class RPCClient { private Connection connection; private Channel channel; private String requestQueueName = "req_queue"; private String responseQueueName = "res_queue"; public RPCClient() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.71.127"); factory.setUsername("wang"); factory.setPassword("wang"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(requestQueueName, false, false, false, null); channel.queueDeclare(responseQueueName, false, false, false, null); } public String call(String message) throws IOException, InterruptedException { String corrId = UUID.randomUUID().toString(); //将corrId、replyQueueName打包发送给consumer /*传输一条消息,AMQP协议预定义了14个属性,下面几个是使用比较频繁的几个属性: deliveryMode:配置一个消息是否持久化。(2表示持久化)这个在第二章中有说明。 contentType :用来描述编码的MIME类型。与html的MIME类型类似,例如,经常使用JSON编码是将此属性设置为一个很好的做法:application/json。 replyTo : 回调队列的名称。 correlationId:RPC响应请求的相关编号。*/ AMQP.BasicProperties props = new AMQP.BasicProperties .Builder() .correlationId(corrId) .replyTo(requestQueueName) .build(); //消息推送 channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8")); //队列阻塞 等待客户端响应 因为只等待一个响应结果所以是1 final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1); DefaultConsumer consumer= new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { if (properties.getCorrelationId().equals(corrId)) { response.offer(new String(body, "UTF-8")); } String message = new String(body, "UTF-8"); int n= Integer.parseInt(message); System.out.println(n); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(responseQueueName, true,consumer ); return response.take(); } public void close() throws IOException { connection.close(); } public static void main(String[] argv) { RPCClient fibonacciRpc = null; String response = null; try { fibonacciRpc = new RPCClient(); System.out.println(" [x] Requesting fib(30)"); response = fibonacciRpc.call("30"); System.out.println(" [.] Got '" + response + "'"); } catch (IOException | TimeoutException | InterruptedException e) { e.printStackTrace(); } finally { if (fibonacciRpc!= null) { try { fibonacciRpc.close(); } catch (IOException _ignore) {} } } }}
服务端:
package rabbitmq;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Connection;import com.rabbitmq.client.Channel;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.AMQP;import com.rabbitmq.client.Envelope;import java.io.IOException;import java.util.UUID;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.TimeoutException;public class RPCClient { private Connection connection; private Channel channel; private String requestQueueName = "req_queue"; private String responseQueueName = "res_queue"; public RPCClient() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.71.127"); factory.setUsername("wang"); factory.setPassword("wang"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(requestQueueName, false, false, false, null); channel.queueDeclare(responseQueueName, false, false, false, null); } public String call(String message) throws IOException, InterruptedException { String corrId = UUID.randomUUID().toString(); //将corrId、replyQueueName打包发送给consumer /*传输一条消息,AMQP协议预定义了14个属性,下面几个是使用比较频繁的几个属性: deliveryMode:配置一个消息是否持久化。(2表示持久化)这个在第二章中有说明。 contentType :用来描述编码的MIME类型。与html的MIME类型类似,例如,经常使用JSON编码是将此属性设置为一个很好的做法:application/json。 replyTo : 回调队列的名称。 correlationId:RPC响应请求的相关编号。*/ AMQP.BasicProperties props = new AMQP.BasicProperties .Builder() .correlationId(corrId) .replyTo(requestQueueName) .build(); //消息推送 channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8")); //队列阻塞 等待客户端响应 因为只等待一个响应结果所以是1 final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1); DefaultConsumer consumer= new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { if (properties.getCorrelationId().equals(corrId)) { response.offer(new String(body, "UTF-8")); } String message = new String(body, "UTF-8"); int n= Integer.parseInt(message); System.out.println(n); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(responseQueueName, true,consumer ); return response.take(); } public void close() throws IOException { connection.close(); } public static void main(String[] argv) { RPCClient fibonacciRpc = null; String response = null; try { fibonacciRpc = new RPCClient(); System.out.println(" [x] Requesting fib(30)"); response = fibonacciRpc.call("30"); System.out.println(" [.] Got '" + response + "'"); } catch (IOException | TimeoutException | InterruptedException e) { e.printStackTrace(); } finally { if (fibonacciRpc!= null) { try { fibonacciRpc.close(); } catch (IOException _ignore) {} } } }}
阅读全文
0 0
- rpc 双队列
- RabbitMQ消息队列(五):RPC远程调用
- RPC
- RPC
- RPC
- RPC
- RPC
- RPC
- RPC
- RPC
- RPC
- RPC
- RPC
- rpc
- rpc
- RPC
- RPC
- RPC
- 深入理解定位父级offsetParent及偏移大小
- Json对象与Json字符串互转(4种转换方式)
- C# ADO.NET之SQL Server数据库操作
- [蓝桥杯]算法提高 概率计算
- Nginx--11
- rpc 双队列
- 排列组合计数&POJ306
- laravel-admin的多级联动
- bootstrap fileinput 设定图片预览长宽
- 只用13个字符完成Python编程
- 小鑫数数儿
- sourceTree中使用的一些问题
- kaggle 激活问题
- asp.net控件快捷增删改查