2017-06-30 59 views
0

我發民Java的REST API服務,現在我需要讓服務器和移動設備發送消息之間的TCP連接,我發現的RabbitMQ是一個好主意,但我在amqp協議中確實是新手。問題是如何將消息從服​​務器發送到從同一隊列中讀取字節的兩個客戶端。我的代碼RabbitMQ的,在同一時間發送消息給兩個客戶端

public class RabitSecClient { 
    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(); 

     channel.queueDeclare(QUEUE_NAME, false, false, false, null); 
     System.out.println(" [*] Waiting for messages2"); 

     Consumer consumer = new DefaultConsumer(channel) { 
      @Override 
      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) 
        throws IOException { 
       String message = new String(body, "UTF-8"); 
       System.out.println(" [x] Received 2 '" + message + "'"); 
      } 
     }; 

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

我執行此代碼兩次進行測試,當我發送消息只有第一個客戶端拿它。是什麼原因?

+0

看一看扇出隊列和結賬https://www.rabbitmq.com/getstarted.html – jr593

回答

相關問題