Remote procedure call (RPC) 官网server代码翻译及说明



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 queue

  • durable - 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 queue

  • Returns:

  • 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 unlimited

  • Throws:

  • 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 queue

  • autoAck - true if the server should consider messages acknowledged once delivered; false if the server should expect explicit acknowledgements

  • callback - an interface to the consumer object

  • Returns:

  • 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会再给该消费者发送一条消息。



转载于:https://my.oschina.net/u/2326611/blog/605593

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值