2017-01-04 8 views
0

У меня есть список ключевых слов, и я хочу отправить сообщение каждому из издателя.Java RabbitMQ - Как переключить ключевое слово в обмене Темами?

Когда я пытаюсь их пропустить, мой подписчик получает каждое сообщение, независимо от того, подписано оно на него или нет.

public class BrokerProducer { 
public static String[] topics = {"Beauty","Meat","Soft Drinks","Fruits and Vegetables","Alcoholic Drinks"}; 


/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args){ 

    try { 
     CreateProducer(); 
     /* 
     Broker RabbitMQ en 155.54.204.46 
     (diana.inf.um.es) 
     – Port 5672 
     – Username master 
     – Password master*/ 
     // TODO code application logic here 
    } catch (IOException ex) { 
     Logger.getLogger(BrokerProducer.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (TimeoutException ex) { 
     Logger.getLogger(BrokerProducer.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    System.out.print("success"); 
} 

private static void CreateProducer() throws IOException, TimeoutException{ 
    ConnectionFactory factory = new ConnectionFactory(); 
    factory.setUsername("master"); 
    factory.setPassword("master"); 
    factory.setHost("diana.inf.um.es"); 
    factory.setPort(5672); 
    Connection conn = factory.newConnection(); 

    Channel channel = conn.createChannel(); 

    channel.exchangeDeclare("SupermarketExchange1", "topic"); 
    //channel.basicPublish("SupermarketExchange1", "testrouting.*", null, "testmessage".getBytes()); 
    //channel.basicPublish("SupermarketExchange1", "testrouting.Fruits and Vegetables", null, "testmessage".getBytes()); 

    for(String top: topics){ 
     //channel.exchangeDeclare("SupermarketExchange1", "topic"); 
     publish(channel, "SupermarketExchange1", top); 
     System.out.println(top); 
     //channel.exchangeDelete("SupermarketExchange1"); 
    } 
} 

private static void publish(Channel channel, String Exchange, String Topic) throws IOException{ 
    String Message = "You are subscribed to"+ Topic; 

    channel.basicPublish(Exchange,"testrouting."+ Topic, null, Message.getBytes()); 

} 

}

+0

Похоже на потребителя подписался на все лады маршрута. Прочитайте [this] (https://www.rabbitmq.com/tutorials/tutorial-four-java.html) и [это] (https://www.rabbitmq.com/tutorials/tutorial-five-java.html) статьи. Они объясняют, как работает маршрутизация в RabbitMQ. –

+0

Этого не должно быть, я могу опубликовать свой абонентский код. Ключ маршрутизации, кажется, «testrouting.Meat, но он все еще получает все. – Jon

ответ

0

Абонент:

public static void CreateConsumer(String subTopic) throws TimeoutException, IOException { 
    ConnectionFactory factory = new ConnectionFactory(); 
    factory.setUsername("master"); 
    factory.setPassword("master"); 
    factory.setHost("diana.inf.um.es"); 
    factory.setPort(5672); 
    Connection conn = factory.newConnection(); 

    Channel channel = conn.createChannel(); 
    channel.exchangeDeclare("SupermarketExchange1", "topic"); 
    //channel.basicPublish("SupermarketExchange", "testrouting", null, "testmessage".getBytes()); 
    channel.queueDeclare("IncomingQueue2", false, false,false,null); 
    System.out.println("createconsumer"); 
    channel.queueBind("IncomingQueue2", "SupermarketExchange1", "testrouting."+ subTopic); 

    JonathanConsumer consum = new JonathanConsumer(channel); 
    channel.basicConsume("IncomingQueue2",false, consum); 


} 

и handledelivery:

public class JonathanConsumer extends DefaultConsumer { 
    public JonathanConsumer(Channel channel) { 
     super(channel); 
    } 

@Override 
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { 
    super.handleDelivery(consumerTag, envelope, properties, body); //To change body of generated methods, choose Tools | Templates. 
    System.out.println("Handle delivery"); 

    Log.d("MyTag", new String(body)); 
    System.out.println(new String(body)); 
} 
}