Kyrexar

[IAP] Consumidor2

Oct 16th, 2019 (edited)
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package consumidor2;
  2.  
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.concurrent.TimeoutException;
  6.  
  7. import org.dom4j.Document;
  8. import org.dom4j.DocumentHelper;
  9. import org.dom4j.Element;
  10. import org.json.JSONException;
  11. import org.json.JSONObject;
  12.  
  13. import com.rabbitmq.client.AMQP;
  14. import com.rabbitmq.client.BuiltinExchangeType;
  15. import com.rabbitmq.client.Channel;
  16. import com.rabbitmq.client.Connection;
  17. import com.rabbitmq.client.ConnectionFactory;
  18. import com.rabbitmq.client.Consumer;
  19. import com.rabbitmq.client.DefaultConsumer;
  20. import com.rabbitmq.client.Envelope;
  21.  
  22. public class Consumidor2 {
  23.     private final static String NOMBRE_EXCHANGE = "deportes";
  24.  
  25.     public static void main(String[] args) throws IOException, TimeoutException {
  26.         // Conectarse con RabbitMQ
  27.         ConnectionFactory factory = new ConnectionFactory();
  28.         factory.setHost("localhost");
  29.         Connection connection = factory.newConnection();
  30.  
  31.         // Crear un canal de comunicación
  32.         Channel channel = connection.createChannel();
  33.  
  34.         // Declaramos un Exchange de tipo Topic
  35.         channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  36.  
  37.         // Solicitar la creación de una cola y asociarle los temas de los mensajes a
  38.         // recibir
  39.         String COLA_CONSUMER = channel.queueDeclare().getQueue();
  40.  
  41.         Scanner ent = new Scanner(System.in);
  42.        
  43.         System.out.println("Introduce deportes separados por comas:");
  44.  
  45.         String texto = ent.nextLine();
  46.         String deportes[] = texto.split(",");
  47.        
  48.         ent.close();
  49.  
  50.         for (int i = 0; i < deportes.length; i++) {
  51.             channel.queueBind(COLA_CONSUMER, NOMBRE_EXCHANGE, deportes[i].trim());
  52.         }
  53.  
  54.         // Crear un manejador de mensajes
  55.         Consumer consumer = new DefaultConsumer(channel) {
  56.             @Override
  57.             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
  58.                     byte[] body) throws IOException {
  59.                 String message = new String(body, "UTF-8");
  60.                 System.out.println("Recibido: '" + message + "'");
  61.  
  62.                 try {
  63.                     JSONObject json;
  64.                     json = new JSONObject(message);
  65.                     Document document = DocumentHelper.createDocument();
  66.                     Element root = document.addElement("Mensajes");
  67.                     Element mensaje = root.addElement("Mensaje");
  68.                     mensaje.addElement("Fecha").addText(json.get("fecha").toString());
  69.                     mensaje.addElement("Texto").addText(json.get("texto").toString());
  70.                     System.out.println(document.asXML());
  71.                 } catch (JSONException e) {
  72.                     e.printStackTrace();
  73.                 }
  74.             }
  75.         };
  76.         channel.basicConsume(COLA_CONSUMER, true, consumer);
  77.     }
  78. }
Add Comment
Please, Sign In to add comment