Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package productor2;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
- import java.util.concurrent.TimeoutException;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.rabbitmq.client.BuiltinExchangeType;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class Productor2 {
- private final static String NOMBRE_EXCHANGE = "deportes";
- public static void main(String[] args) throws IOException, TimeoutException, JSONException {
- // 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 un Exchange de tipo Topic
- channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
- // Enviar mensajes con tema de enrutamiento
- Scanner ent = new Scanner(System.in);
- String message = "R.Sociedad 2 – 3 Valencia";
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Date date = new Date();
- String deporte,fecha,texto;
- while (!message.equals("salir")) {
- System.out.println("Introduce un deporte (futbol, baloncesto o ruby):");
- message = ent.nextLine();
- deporte = message;
- fecha = format.format(date);
- System.out.println("Introduce texto:");
- texto = ent.nextLine();
- JSONObject obj = new JSONObject();
- obj.put("fecha", fecha);
- obj.put("texto", texto);
- channel.basicPublish(NOMBRE_EXCHANGE, deporte, null, obj.toString().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