package rabbitmq.m6_rpc;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
public class RpcServer {
public static void main(String[] args) throws Throwable {
ConnectionFactory f = new ConnectionFactory();
f.setHost("192.168.64.140");
f.setUsername("admin");
f.setPassword("admin");
Channel c = f.newConnection().createChannel();
c.queueDeclare("rpc_queue",false,false,false,null);
DeliverCallback deliverCallback = new DeliverCallback() {
public void handle(String consumerTag, Delivery message) throws IOException {
String msg = new String(message.getBody());
String replyTo = message.getProperties().getReplyTo();
String cid = message.getProperties().getCorrelationId();
System.out.println(msg+", "+replyTo+", "+cid);
long result = fbnq(Integer.parseInt(msg));
BasicProperties props =
new BasicProperties.Builder()
.correlationId(cid)
.build();
c.basicPublish("", replyTo, props, (""+result).getBytes());
System.out.println("已向客户端发回结果");
}
};
CancelCallback cancelCallback = new CancelCallback() {
public void handle(String consumerTag) throws IOException {
}
};
c.basicConsume("rpc_queue", true, deliverCallback, cancelCallback);
}
static long fbnq(int n) {
if (n == 1 || n == 2) {
return 1;
}
return fbnq(n-1) + fbnq(n-2);
}
}
package rabbitmq.m6_rpc;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestABQ {
static BlockingQueue<Double> q = new ArrayBlockingQueue<>(10);
public static void main(String[] args) {
new Thread() {
public void run() {
System.out.println("\n线程1 - 按回车放入数据");
new Scanner(System.in).nextLine();
double d = Math.random();
q.offer(d);
System.out.println("线程1 - 放入数据:"+d);
}
}.start();
new Thread() {
public void run() {
try {
System.out.println("线程2 - 正在获取数据");
Double d = q.take();
System.out.println("线程2 - 已经获取数据: "+d);
} catch (InterruptedException e) {
}
}
}.start();
}
}
package rabbitmq.m6_rpc;
import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
public class RpcClient {
static BlockingQueue<String> q = new ArrayBlockingQueue<>(10);
public static void main(String[] args) throws Exception {
while (true) {
System.out.print("求第几个斐波那契数:");
int n = new Scanner(System.in).nextInt();
long r = f(n);
System.out.println(r);
}
}
private static long f(int n) throws Exception {
ConnectionFactory f = new ConnectionFactory();
f.setHost("192.168.64.140");
f.setUsername("admin");
f.setPassword("admin");
Channel c = f.newConnection().createChannel();
c.queueDeclare("rpc_queue",false,false,false,null);
String queue = c.queueDeclare().getQueue();
String cid = UUID.randomUUID().toString();
BasicProperties props =
new BasicProperties.Builder()
.replyTo(queue)
.correlationId(cid)
.build();
c.basicPublish("", "rpc_queue", props, (""+n).getBytes());
System.out.println("已发送调用");
System.out.println("主线程不必等待, 继续执行其他运算......");
System.out.println("直到需要结果时, 再取结果");
DeliverCallback deliverCallback = new DeliverCallback() {
public void handle(String consumerTag, Delivery message) throws IOException {
String msg = new String(message.getBody());
String cid2 = message.getProperties().getCorrelationId();
if (cid.equals(cid2)) {
q.offer(msg);
}
}
};
CancelCallback cancelCallback = new CancelCallback() {
public void handle(String consumerTag) throws IOException {
}
};
c.basicConsume(queue, true, deliverCallback, cancelCallback);
return Long.parseLong(q.take());
}
}