Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aplicacionHTTP;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.concurrent.TimeoutException;
- import org.json.JSONArray;
- 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 AplicacionHTTP {
- private final static String NOMBRE_EXCHANGE = "fallas";
- public static void main(String[] args) throws IOException, JSONException, TimeoutException {
- // GET
- String url = "http://mapas.valencia.es/lanzadera/puntoInteres/fallasvalencia?radio=4000&lang=es&lat=39465212&lon=-374521&filtros=2";
- String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
- String authorization = "Basic aW5mb3JtYXR1cHY6dHlBMGJxVWU=";
- // Creamos URL
- URL objUrl = new URL(url);
- // Creamos conexión HTTP
- HttpURLConnection con = (HttpURLConnection) objUrl.openConnection();
- // Definimos operación HTTP
- con.setRequestMethod("GET");
- // Añadimos encabezados
- con.setRequestProperty("authorization", authorization);
- con.setRequestProperty("user-agent", userAgent);
- // Procesamos la respuesta
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- String fallas = response.toString();
- JSONArray coleccion=new JSONArray(fallas);
- //System.out.println(fallas);
- // 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
- for( int i=0 ; i<coleccion.length() ; i++ ) {
- JSONObject falla=coleccion.getJSONObject(i);
- int distancia = falla.getInt("distancia");
- if( distancia>1000 ) {
- channel.basicPublish(NOMBRE_EXCHANGE, "lejos", null, falla.toString().getBytes());
- }else {
- channel.basicPublish(NOMBRE_EXCHANGE, "cerca", null, falla.toString().getBytes());
- }
- }
- // Cerrar canal y conexión, si no se quiere enviar más
- channel.close();
- connection.close();
- }
- }
Add Comment
Please, Sign In to add comment