Kyrexar

[IAP] Productor1

Oct 16th, 2019 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. package productor;
  2.  
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.concurrent.TimeoutException;
  6.  
  7. import com.rabbitmq.client.Channel;
  8. import com.rabbitmq.client.Connection;
  9. import com.rabbitmq.client.ConnectionFactory;
  10.  
  11. /**
  12.  * @author adsosmar
  13.  * @version pract1
  14.  */
  15. public class Productor {
  16.     private final static String NOMBRE_COLA = "saludo";
  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 la cola para producir mensajes
  28.         channel.queueDeclare(NOMBRE_COLA, false, false, false, null);
  29.  
  30.         // Enviar mensajes
  31.         String message = "Hola Mundo";
  32.         Scanner ent = new Scanner(System.in);
  33.        
  34.         while(!message.equals("salir")) {
  35.             message = ent.nextLine();
  36.             channel.basicPublish("", NOMBRE_COLA, null, message.getBytes());
  37.         }
  38.        
  39.         ent.close();
  40.        
  41.         // Cerrar canal y conexión, si no se quiere enviar más
  42.         channel.close();
  43.         connection.close();
  44.     }
  45. }
Add Comment
Please, Sign In to add comment