Advertisement
Kyrexar

[IAP] Logger

Oct 27th, 2019 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. package logger;
  2.  
  3. import java.io.IOException;
  4. import java.util.concurrent.TimeoutException;
  5.  
  6. import com.rabbitmq.client.AMQP;
  7. import com.rabbitmq.client.BuiltinExchangeType;
  8. import com.rabbitmq.client.Channel;
  9. import com.rabbitmq.client.Connection;
  10. import com.rabbitmq.client.ConnectionFactory;
  11. import com.rabbitmq.client.Consumer;
  12. import com.rabbitmq.client.DefaultConsumer;
  13. import com.rabbitmq.client.Envelope;
  14.  
  15. public class Logger {
  16.     private final static String NOMBRE_EXCHANGE = "ajuntament";
  17.  
  18.     public static void main(String[] args) throws IOException, TimeoutException {
  19.         // Conectarse con RabbitMQ
  20.         ConnectionFactory factory = new ConnectionFactory();
  21.         factory.setHost("localhost");
  22.         Connection connection = factory.newConnection();
  23.  
  24.         // Crear un canal de comunicaci�n
  25.         Channel channel = connection.createChannel();
  26.  
  27.         // Declaramos un Exchange de tipo Topic
  28.         channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  29.  
  30.         // Solicitar la creaci�n de una cola y asociarle los temas de los mensajes a recibir
  31.         String COLA_CONSUMER = channel.queueDeclare().getQueue();
  32.  
  33.         channel.queueBind(COLA_CONSUMER, NOMBRE_EXCHANGE, "logger");
  34.  
  35.         // Crear un manejador de mensajes
  36.         Consumer consumer = new DefaultConsumer(channel) {
  37.             @Override
  38.             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
  39.                     byte[] body) throws IOException {
  40.                 String message = new String(body, "UTF-8");
  41.                 System.out.println("Recibido: '" + message + "'");
  42.             }
  43.         };
  44.         channel.basicConsume(COLA_CONSUMER, true, consumer);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement