Advertisement
dig090

ThreadedConnector

Sep 4th, 2013
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package com.j256.ormlite;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.InetAddress;
  8. import java.net.InetSocketAddress;
  9. import java.net.Socket;
  10. import java.net.SocketAddress;
  11. import java.net.UnknownHostException;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.concurrent.Callable;
  17. import java.util.concurrent.ExecutionException;
  18. import java.util.concurrent.ExecutorService;
  19. import java.util.concurrent.Executors;
  20. import java.util.concurrent.Future;
  21.  
  22. public class ThreadedConnector implements Callable<String> {
  23.  
  24.     private static int PORT = 80;
  25.  
  26.     private String hostip;
  27.     private String hostname;
  28.    
  29.     Foo(String hostname, String hostip) {
  30.         this.hostname = hostname;
  31.         this.hostip = hostip;
  32.     }
  33.  
  34.     public String call() throws IOException {
  35.         return GetData();
  36.     }
  37.  
  38.     private String GetData() throws IOException {
  39.         Socket so = new Socket();
  40.         SocketAddress sa = null;
  41.         PrintWriter out = null;
  42.         BufferedReader in = null;
  43.         try {
  44.             sa = new InetSocketAddress(InetAddress.getByName(hostip), PORT);
  45.         } catch (UnknownHostException e1) {
  46.             e1.printStackTrace();
  47.         }
  48.         try {
  49.             so.connect(sa, 10000);
  50.             out = new PrintWriter(so.getOutputStream(), true);
  51.             out.println("\1IDC_UPDATE\1");
  52.             in = new BufferedReader(new InputStreamReader(so.getInputStream()));
  53.             String line = in.readLine();
  54.             String[] response = line.split("\1");
  55.             if (response.length <= 2) {
  56.                 System.out.println("invalid response: " + line);
  57.                 return hostname + "|-1";
  58.             }
  59.             so = null;
  60.             try {
  61.                 Integer.parseInt(response[2]);
  62.             } catch (NumberFormatException e) {
  63.                 System.out.println("Number format exception");
  64.                 return hostname + "|-1";
  65.             }
  66.             return hostname + "|" + response[2];
  67.         } catch (IOException e) {
  68.             System.err.println(e);
  69.             return hostname + "|-1";
  70.         } finally {
  71.             if (in != null) {
  72.                 in.close();
  73.             }
  74.             if (out != null) {
  75.                 out.close();
  76.             }
  77.             if (so != null) {
  78.                 so.close();
  79.             }
  80.         }
  81.     }
  82.  
  83.     public static void main(String[] args) {
  84.         Map<String, String> names = new HashMap<String, String>();
  85.         names.put("corp-dev8.marketplacerewards.com", "10.1.2.3");
  86.         names.put("foo1", "200.1.193.28");
  87.         names.put("foo2", "200.1.193.29");
  88.         names.put("corp-dev7.marketplacerewards.com", "10.1.2.3");
  89.         names.put("foo3", "200.1.193.30");
  90.         names.put("corp-dev6.marketplacerewards.com", "10.1.2.3");
  91.  
  92.         ExecutorService pool = Executors.newFixedThreadPool(30);
  93.         List<Future<String>> list = new ArrayList<Future<String>>();
  94.         for (Map.Entry<String, String> entry : names.entrySet()) {
  95.             Callable<String> worker = new Foo(entry.getKey(), entry.getValue());
  96.             Future<String> submit = pool.submit(worker);
  97.             list.add(submit);
  98.         }
  99.         pool.shutdown();
  100.         for (Future<String> future : list) {
  101.             try {
  102.                 System.out.println("Received: " + future.get());
  103.                 // ........ PROCESS DATA HERE!..........//
  104.             } catch (InterruptedException e) {
  105.                 e.printStackTrace();
  106.             } catch (ExecutionException e) {
  107.                 e.printStackTrace();
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement