import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.AMQP.BasicProperties;
public class RPCServer {
private static final String RPC_QUEUE_NAME = "rpc_queue";
private static int fib(int n) {
if (n ==0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
public static void main(String[] argv) {
Connection connection = null;
Channel channel = null;
try {
//创建链接工厂
ConnectionFactory factory = new ConnectionFactory();
//parameter: the default host to use for connections
factory.setHost("localhost");
//Create a new broker connection
connection = factory.newConnection();
//
Create a new channel, using an internally allocated channel number.
channel = connection.createChannel();
//Declare a queue
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
//Request a specific prefetchCount "quality of service" settings for this channel.
channel.basicQos(1);
// Create the QueueingConsumer and have it consume from the queue
QueueingConsumer consumer = new QueueingConsumer(channel);
//Start a non-nolocal, non-exclusive consumer, with a server-generated consumerTag
channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
System.out.println(" [x] Awaiting RPC requests");
while (true) {
String response = null;
//Main application-side API: wait for the next message delivery and return it.
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();
BasicProperties replyProps = new BasicProperties
.Builder()
.correlationId(props.getCorrelationId())
.build();
try {
String message = new String(delivery.getBody(),"UTF-8");
int n = Integer.parseInt(message);
System.out.println(" [.] fib(" + message + ")");
response = "" + fib(n);
}
catch (Exception e){
System.out.println(" [.] " + e.toString());
response = "";
}
finally {
channel.basicPublish( "", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (Exception ignore) {}
}
}
}
}
其中:
AMQP.Queue.DeclareOk queueDeclare(java.lang.String queue,
boolean durable,
boolean exclusive,
boolean autoDelete,
java.util.Map<java.lang.String,java.lang.Object> arguments)
throws java.io.IOException
Declare a queue
Parameters:
queue
- the name of the queuedurable
- true if we are declaring a durable queue (the queue will survive a server restart)exclusive
- true if we are declaring an exclusive queue (restricted to this connection)autoDelete
- true if we are declaring an autodelete queue (server will delete it when no longer in use)arguments
- other properties (construction arguments) for the queueReturns:
a declaration-confirm method to indicate the queue was successfully declared
Throws:
java.io.IOException
- if an error is encountered
basicQos
void basicQos(int prefetchCount)
throws java.io.IOException
Request a specific prefetchCount "quality of service" settings for this channel.
Parameters:
prefetchCount
- maximum number of messages that the server will deliver, 0 if unlimitedThrows:
java.io.IOException
- if an error is encountered
basicConsume
java.lang.String basicConsume(java.lang.String queue,
boolean autoAck, Consumer callback)
throws java.io.IOException
Start a non-nolocal, non-exclusive consumer, with a server-generated consumerTag.
Parameters:
queue
- the name of the queueautoAck
- true if the server should consider messages acknowledged once delivered; false if the server should expect explicit acknowledgementscallback
- an interface to the consumer objectReturns:
the consumerTag generated by the server
Throws:
java.io.IOException
- if an error is encountered
ConnectionFactory、Connection、Channel
ConnectionFactory、Connection、Channel都是RabbitMQ对外提供的API中最基本的对象。 Connection是RabbitMQ的socket链接,它封装了socket协议相关部分逻辑。ConnectionFactory为 Connection的制造工厂。
Channel是我们与RabbitMQ打交道的最重要的一个接口,我们大部分的业务操作是在Channel这个接口中完成的,包括定义Queue、定义Exchange、绑定Queue与Exchange、发布消息等
Prefetch count
如果有多个消费者同时订阅同一个Queue中的消息,Queue中的消息会被平摊给多个消费者。这时如果每个消息的处理时间不同,就有 可能会导致某些消费者一直在忙,而另外一些消费者很快就处理完手头工作并一直空闲的情况。我们可以通过设置prefetchCount来限制Queue每 次发送给每个消费者的消息数,比如我们设置prefetchCount=1,则Queue每次给每个消费者发送一条消息;消费者处理完这条消息后 Queue会再给该消费者发送一条消息。