Advertisement
Kyrexar

[IAP] Transformador

Oct 27th, 2019 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. package transformador;
  2.  
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.concurrent.TimeoutException;
  6.  
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10. import org.json.XML;
  11.  
  12. import com.rabbitmq.client.AMQP;
  13. import com.rabbitmq.client.BuiltinExchangeType;
  14. import com.rabbitmq.client.Channel;
  15. import com.rabbitmq.client.Connection;
  16. import com.rabbitmq.client.ConnectionFactory;
  17. import com.rabbitmq.client.Consumer;
  18. import com.rabbitmq.client.DefaultConsumer;
  19. import com.rabbitmq.client.Envelope;
  20.  
  21. public class Transformador {
  22.     private final static String NOMBRE_EXCHANGE = "ajuntament";
  23.  
  24.     public static void main(String[] args) throws IOException, TimeoutException, JSONException {
  25.         // Conectarse con RabbitMQ
  26.         ConnectionFactory factory = new ConnectionFactory();
  27.         factory.setHost("localhost");
  28.         Connection connection = factory.newConnection();
  29.  
  30.         // Crear un canal de comunicaci�n de entrada
  31.         Channel channelEnt = connection.createChannel();
  32.         Channel channelOut = connection.createChannel();
  33.  
  34.         // Declaramos un Exchange de tipo Topic
  35.         channelEnt.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  36.         channelOut.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  37.  
  38.         // Solicitar la creaci�n de una cola y asociarle los temas de los mensajes a
  39.         // recibir
  40.         String COLA_CONSUMER = channelEnt.queueDeclare().getQueue();
  41.  
  42.         Scanner ent = new Scanner(System.in);
  43.         System.out.println("Introduce trafico o bicis: ");
  44.         String texto = ent.nextLine();
  45.         ent.close();
  46.  
  47.         channelEnt.queueBind(COLA_CONSUMER, NOMBRE_EXCHANGE, texto);
  48.  
  49.         // Crear un manejador de mensajes
  50.         Consumer consumer = new DefaultConsumer(channelEnt) {
  51.             @Override
  52.             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
  53.                     byte[] body) throws IOException {
  54.                 String message = new String(body, "UTF-8");
  55.                 // System.out.println("Recibido: '" + message + "'");
  56.                 System.out.println("Recibido, entrando en " + texto);
  57.  
  58.                 String punto, tipo, descripcion;
  59.                 int latitud, longitud;
  60.                 JSONArray coleccion;
  61.                 try {
  62.                     coleccion = new JSONArray(message);
  63.                     // Le introduce los valores del mensaje
  64.                     if (texto == "trafico") { // es un json
  65.                         System.out.println("Mensaje de trafico, parseando");
  66.  
  67.                         for (int i = 0; i < coleccion.length(); i++) {
  68.                             JSONObject obj = coleccion.getJSONObject(i);
  69.  
  70.                             punto = obj.get("denominacion").toString();
  71.                             tipo = "datos de trafico";
  72.                             latitud = obj.getInt("coordinates[0]");
  73.                             longitud = obj.getInt("coordinates[1]");
  74.                             // 0=fluido, 1=denso, 2=congestionado, 3=cortado
  75.                             String[] estados = { "fluido", "denso", "congestionado", "cortado" };
  76.                             int desc = obj.getInt("estado");
  77.                             descripcion = estados[desc];
  78.  
  79.                             // Mensaje canonico
  80.                             JSONObject json = new JSONObject();
  81.                             json.put("punto", punto);
  82.                             json.put("tipo", tipo);
  83.                             json.put("latitud", latitud);
  84.                             json.put("longitud", longitud);
  85.                             json.put("descripcion", descripcion);
  86.  
  87.                             System.out.println(json.toString());
  88.  
  89.                             channelOut.basicPublish(NOMBRE_EXCHANGE, "logger", null, json.toString().getBytes());
  90.                         }
  91.                     } else if (texto == "bicis") { // es un xml
  92.                         JSONObject xml = XML.toJSONObject(message);
  93.                         channelOut.basicPublish(NOMBRE_EXCHANGE, texto, null, xml.toString().getBytes());
  94.                     }
  95.                 } catch (JSONException e) {
  96.                     // TODO Auto-generated catch block
  97.                     e.printStackTrace();
  98.                 }
  99.  
  100.             }
  101.         };
  102.         channelEnt.basicConsume(COLA_CONSUMER, true, consumer);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement