ConnectionFactoryfactory=newConnectionFactory();
factory.setHost("localhost");
Connectionconnection=factory.newConnection();
Channelchannel=connection.createChannel();
StringexchangeName="my.topic.exchange";
StringqueueName1="my.topic.queue1";
StringqueueName2="my.topic.queue2";
channel.exchangeDeclare(exchangeName, "topic", true);
channel.queueDeclare(queueName1, true, false, false, null);
channel.queueDeclare(queueName2, true, false, false, null);
channel.queueBind(queueName1, exchangeName, "*.orange.*");
channel.queueBind(queueName2, exchangeName, "*.*.rabbit");
Stringmessage1="A quick orange rabbit";
Stringmessage2="A lazy brown dog";
channel.basicPublish(exchangeName, "quick.orange.rabbit", null, message1.getBytes());
channel.basicPublish(exchangeName, "lazy.brown.dog", null, message2.getBytes());
Consumerconsumer1=newDefaultConsumer(channel) {
publicvoidhandleDelivery(StringconsumerTag, Envelopeenvelope, AMQP.BasicPropertiesproperties, byte[] body) throwsIOException {
Stringmessage=newString(body, "UTF-8");
System.out.println("Consumer1 received message: "+message);
}
};
Consumerconsumer2=newDefaultConsumer(channel) {
publicvoidhandleDelivery(StringconsumerTag, Envelopeenvelope, AMQP.BasicPropertiesproperties, byte[] body) throwsIOException {
Stringmessage=newString(body, "UTF-8");
System.out.println("Consumer2 received message: "+message);
}
};
channel.basicConsume(queueName1, true, consumer1);
channel.basicConsume(queueName2, true, consumer2);