Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package productor;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.concurrent.TimeoutException;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- /**
- * @author adsosmar
- * @version pract1
- */
- public class Productor {
- private final static String NOMBRE_COLA = "saludo";
- public static void main(String [] args) throws IOException, TimeoutException {
- // Conectarse con RabbitMQ
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- // Crear un canal de comunicación
- Channel channel = connection.createChannel();
- // Declaramos la cola para producir mensajes
- channel.queueDeclare(NOMBRE_COLA, false, false, false, null);
- // Enviar mensajes
- String message = "Hola Mundo";
- Scanner ent = new Scanner(System.in);
- while(!message.equals("salir")) {
- message = ent.nextLine();
- channel.basicPublish("", NOMBRE_COLA, null, message.getBytes());
- }
- ent.close();
- // Cerrar canal y conexión, si no se quiere enviar más
- channel.close();
- connection.close();
- }
- }
Add Comment
Please, Sign In to add comment