2016-12-24 83 views
0

的很慢的表現我已經遇到了春天引導消費者的煩惱。我比較了兩位消費者的工作。 首先消費者:彈簧AMQP comsumer

import com.rabbitmq.client.*; 
import java.io.IOException; 

public class Recv { 
    private final static String QUEUE_NAME = "hello"; 

    public static void main(String[] argv) throws Exception { 
     ConnectionFactory factory = new ConnectionFactory(); 
     factory.setHost("localhost"); 
     Connection connection = factory.newConnection(); 
     Channel channel = connection.createChannel(); 

     System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); 

     Consumer consumer = new DefaultConsumer(channel) { 
      @Override 
      public void handleDelivery(String consumerTag, Envelope envelope, 
        AMQP.BasicProperties properties, byte[] body) throws IOException { 
      } 
     }; 

     channel.basicConsume(QUEUE_NAME, true, consumer); 
    } 
} 

二次消費:

@Controller 
public class Consumer { 

    @RabbitListener(queues = "hello") 
    public void processMessage(Message message) { 
    } 
} 

有用於安裝彈簧引導消費者沒有配置文件,一切都默認。 在我的電腦第一個作品的速度快10倍。可能是什麼問題?

回答

0

爲Spring AMQP消費者默認預取(basicQos)爲1,這意味着只有1消息是傑出的,在消費者在任何一個時間;配置rabbitListenerContainerFactory @Bean到prefetchCount設置更大的東西。

您必須覆蓋默認的啓動配置@Bean

+0

感謝。它解決了這個問題。 – GoodYar

+0

謝謝。我看到你是新來的 - 如果你「接受」答案(點擊表決按鈕左下方的勾號/複選標記),它將幫助其他人尋找答案。 –