Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.j256.ormlite;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.net.SocketAddress;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- public class ThreadedConnector implements Callable<String> {
- private static int PORT = 80;
- private String hostip;
- private String hostname;
- Foo(String hostname, String hostip) {
- this.hostname = hostname;
- this.hostip = hostip;
- }
- public String call() throws IOException {
- return GetData();
- }
- private String GetData() throws IOException {
- Socket so = new Socket();
- SocketAddress sa = null;
- PrintWriter out = null;
- BufferedReader in = null;
- try {
- sa = new InetSocketAddress(InetAddress.getByName(hostip), PORT);
- } catch (UnknownHostException e1) {
- e1.printStackTrace();
- }
- try {
- so.connect(sa, 10000);
- out = new PrintWriter(so.getOutputStream(), true);
- out.println("\1IDC_UPDATE\1");
- in = new BufferedReader(new InputStreamReader(so.getInputStream()));
- String line = in.readLine();
- String[] response = line.split("\1");
- if (response.length <= 2) {
- System.out.println("invalid response: " + line);
- return hostname + "|-1";
- }
- so = null;
- try {
- Integer.parseInt(response[2]);
- } catch (NumberFormatException e) {
- System.out.println("Number format exception");
- return hostname + "|-1";
- }
- return hostname + "|" + response[2];
- } catch (IOException e) {
- System.err.println(e);
- return hostname + "|-1";
- } finally {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- if (so != null) {
- so.close();
- }
- }
- }
- public static void main(String[] args) {
- Map<String, String> names = new HashMap<String, String>();
- names.put("corp-dev8.marketplacerewards.com", "10.1.2.3");
- names.put("foo1", "200.1.193.28");
- names.put("foo2", "200.1.193.29");
- names.put("corp-dev7.marketplacerewards.com", "10.1.2.3");
- names.put("foo3", "200.1.193.30");
- names.put("corp-dev6.marketplacerewards.com", "10.1.2.3");
- ExecutorService pool = Executors.newFixedThreadPool(30);
- List<Future<String>> list = new ArrayList<Future<String>>();
- for (Map.Entry<String, String> entry : names.entrySet()) {
- Callable<String> worker = new Foo(entry.getKey(), entry.getValue());
- Future<String> submit = pool.submit(worker);
- list.add(submit);
- }
- pool.shutdown();
- for (Future<String> future : list) {
- try {
- System.out.println("Received: " + future.get());
- // ........ PROCESS DATA HERE!..........//
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement