java socket 模擬http請求問題


    public class CheckDetailService {
        private static Logger logger = Logger.getLogger(CheckDetailTest.class);
        //任務隊列
        private List<WorkTask> taskList = new ArrayList<WorkTask>();
       
        public CheckDetailService(){

            initTaskList();
        }
       
        public void initTaskList(){
            List<Domain> list = new DomainDaoImpl().getDomains();

            for(Domain domain : list){html

            //往任務隊列裏添加任務多線程

                taskList.add(new WorkTask(domain));
            }
        }
       
        public List<Future<CheckDetail>> execute(){

            List<Future<CheckDetail>> futureList = new ArrayList<Future<CheckDetail>>();app

            //jdk自帶線程池,分配10個線程dom

            ExecutorService service = Executors.newFixedThreadPool(10);

            try {socket

            //線程池中多線程執行tasklist任務隊列,執行時間爲5分鐘,5分鐘執行不完就中斷執行ui

                futureList = service.invokeAll(taskList,5,TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                logger.error("正在執行的任務意外中斷.",e);
            }finally{
                service.shutdown();
            }
            return futureList;
        }
        //每個任務
        class WorkTask implements Callable<CheckDetail>{
            private Domain domain;
           
            public WorkTask(Domain domain){
                this.domain = domain;
            }
            public CheckDetail call(){
                int stateCode = 0;
                int state = 1;
                StringBuilder header = new StringBuilder();
                BufferedReader br = null;
                BufferedWriter bw = null;
                StringBuilder accept = new StringBuilder("GET / HTTP/1.1\r\n");
                accept.append("User-Agent: Java/1.6.0_20\r\n");
                accept.append("Host: "+domain.getDomainname()+":"+domain.getPort()+"\r\n");
                accept.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
                accept.append("Connection: Close\r\n");
                accept.append("\r\n");
                try {

                    Socket socket = new Socket();this

                    //socekt鏈接ip,port,5秒鐘後沒有鏈接指定ip則跑出SocketTimeOutException異常,表示鏈接超時線程

                    socket.connect(new InetSocketAddress(InetAddress.getByName(domain.getIp()), domain.getPort()),5);                 socket.setSoTimeout(5);                 br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GBK"));                 bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));                 bw.write(accept.toString());                 bw.flush();                 String firstLine = br.readLine();                 if(firstLine != null){                     stateCode = Integer.parseInt(firstLine.split(" ")[1]);                     header.append(firstLine);                 }                 state = (stateCode == 200)? 1 : 2;                 String line = "";                 while(!"".equals((line = br.readLine()))){                     header.append(line+"\n");                 }             } catch (Exception e) {                 if(e instanceof SocketTimeoutException){                     state = 3;                     logger.info("檢查域名:"+domain.getDomainname()+":"+domain.getPort()+" 鏈接超時.");                 }else{                     state = 2;                     logger.info("檢查域名:"+domain.getDomainname()+":"+domain.getPort()+" 失敗.");                 }             }finally{                 try {                     if(bw != null)                     bw.close();                     if(br != null)                     br.close();                 } catch (IOException e) {                     logger.error("關閉輸入輸出流失敗.",e);                 }             }             CheckDetail cDetail = new CheckDetail();             cDetail.setCheckDate(new Date());             cDetail.setCheckStatus(state);             cDetail.setStatusCode(stateCode);             cDetail.setHeader(header.toString());             cDetail.setDomain(domain.getDomainname());             cDetail.setIp(domain.getIp());             cDetail.setPort(domain.getPort());             return cDetail;         }     } }
    相關文章
    相關標籤/搜索