目的:到服务器请求数据
Client的代码如下:
package yy.rabbitstudy.rpc;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import yy.rabbitstudy.util.RabbitMQUtil;
public class RPCClient {
private Channel channel;
private String requestQueueName = "rpc_queue";
private String replyQueueName;
private QueueingConsumer consumer;
public RPCClient() throws Exception {
channel=RabbitMQUtil.getChannel();
replyQueueName = channel.queueDeclare().getQueue();
consumer = new QueueingConsumer(channel);
channel.basicConsume(replyQueueName, true, consumer);
}
public String call(String message) throws Exception {
String response = null;
String corrId = java.util.UUID.randomUUID().toString();
BasicProperties props = new BasicProperties
.Builder()
.correlationId(corrId)
.replyTo(replyQueueName)
.build();
channel.basicPublish("", requestQueueName, props, message.getBytes());
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
if (delivery.getProperties().getCorrelationId().equals(corrId)) {
response = new String(delivery.getBody());
break;
}
}
return response;
}
public static void main(String[] args) throws Exception {
RPCClient client=new RPCClient();
String response = client.call("15");
System.out.println(response);
}
}
Server的代码如下:
package yy.rabbitstudy.rpc;
import java.io.IOException;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;
import yy.rabbitstudy.util.RabbitMQUtil;
public class RPCServer {
private static final String RPC_QUEUE_NAME = "rpc_queue";
public static void main(String[] args) {
Channel channel = RabbitMQUtil.getChannel();
try {
channel.queueDeclare(RPC_QUEUE_NAME,false,false,false,null);
channel.basicQos(1);
QueueingConsumer consumer=new QueueingConsumer(channel);
channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
System.out.println(" [x] Awaiting RPC requests");
while(true){
QueueingConsumer.Delivery delivery=consumer.nextDelivery();
BasicProperties properties = delivery.getProperties();
BasicProperties replyProps=new BasicProperties
.Builder()
.correlationId(properties.getCorrelationId())
.build();
String message=new String(delivery.getBody());
int n=Integer.parseInt(message);
String response=""+fib(n);
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes());
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ShutdownSignalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ConsumerCancelledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
RabbitMQUtil.close();
}
}
private static int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
}