Kyrexar

[IAP] Productor2

Oct 16th, 2019 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package productor2;
  2.  
  3. import java.io.IOException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Scanner;
  7. import java.util.concurrent.TimeoutException;
  8.  
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. import com.rabbitmq.client.BuiltinExchangeType;
  13. import com.rabbitmq.client.Channel;
  14. import com.rabbitmq.client.Connection;
  15. import com.rabbitmq.client.ConnectionFactory;
  16.  
  17. public class Productor2 {
  18.     private final static String NOMBRE_EXCHANGE = "deportes";
  19.  
  20.     public static void main(String[] args) throws IOException, TimeoutException, JSONException {
  21.         // Conectarse con RabbitMQ
  22.         ConnectionFactory factory = new ConnectionFactory();
  23.         factory.setHost("localhost");
  24.         Connection connection = factory.newConnection();
  25.  
  26.         // Crear un canal de comunicación
  27.         Channel channel = connection.createChannel();
  28.  
  29.         // Declaramos un Exchange de tipo Topic
  30.         channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  31.  
  32.         // Enviar mensajes con tema de enrutamiento
  33.         Scanner ent = new Scanner(System.in);
  34.         String message = "R.Sociedad 2 – 3 Valencia";
  35.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  36.         Date date = new Date();
  37.        
  38.         String deporte,fecha,texto;
  39.  
  40.         while (!message.equals("salir")) {
  41.             System.out.println("Introduce un deporte (futbol, baloncesto o ruby):");
  42.             message = ent.nextLine();
  43.             deporte = message;
  44.            
  45.             fecha = format.format(date);
  46.             System.out.println("Introduce texto:");
  47.             texto = ent.nextLine();
  48.  
  49.             JSONObject obj = new JSONObject();
  50.             obj.put("fecha", fecha);
  51.             obj.put("texto", texto);
  52.  
  53.             channel.basicPublish(NOMBRE_EXCHANGE, deporte, null, obj.toString().getBytes());
  54.         }
  55.  
  56.         ent.close();
  57.  
  58.         // Cerrar canal y conexión, si no se quiere enviar más
  59.         channel.close();
  60.         connection.close();
  61.     }
  62. }
Add Comment
Please, Sign In to add comment