Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package consumidor3;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.concurrent.TimeoutException;
- import com.rabbitmq.client.AMQP;
- import com.rabbitmq.client.BuiltinExchangeType;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.Consumer;
- import com.rabbitmq.client.DefaultConsumer;
- import com.rabbitmq.client.Envelope;
- public class Consumidor3 {
- private final static String NOMBRE_EXCHANGE = "fallas";
- public static void main(String[] args) throws IOException, TimeoutException {
- // Conectarse con RabbitMQ
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- // Crear un canal de comunicación
- Channel channel = connection.createChannel();
- // Declaramos un Exchange de tipo Topic
- channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
- // Solicitar la creación de una cola y asociarle los temas de los mensajes a
- // recibir
- String COLA_CONSUMER = channel.queueDeclare().getQueue();
- Scanner ent = new Scanner(System.in);
- System.out.println("Introduce lejos o cerca:");
- String texto = ent.nextLine();
- ent.close();
- channel.queueBind(COLA_CONSUMER, NOMBRE_EXCHANGE, texto);
- // Crear un manejador de mensajes
- 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("Recibido: '" + message + "'");
- }
- };
- channel.basicConsume(COLA_CONSUMER, true, consumer);
- }
- }
Add Comment
Please, Sign In to add comment