java 实现多线程httppost请求

package ;

import java.util.concurrent.CountDownLatch;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * 基于httpClient3的多线程版本,使用spring管理生命周期
 *
 * @author chenyuehua
 *
 */
public class MultiThreadHttpClient3 extends HttpClient implements DisposableBean, InitializingBean {

    private static final Logger log = Logger.getLogger(MultiThreadHttpClient3.class);
    public static int DEFAULT_TIMEOUT = 1000; // 默认超时1秒
    public static int DEFAULT_MAX_CONNECTIONS = 8000;// 默认连8000
    public static int DEFAULT_PERHOST_MAX_CONNECTIONS = 2000;// 默认连2000
    public static int DEFAULT_READ_TIMEOUT = 1000;
    public static int DEFAULT_LOCK_TIMEOUT = 1000; // 默认锁等待1000毫秒

    /**
     * 连接超时时间
     */
    private int timeout = DEFAULT_TIMEOUT;

    /**
     * 读超时时间
     */
    private int readTimeout = DEFAULT_READ_TIMEOUT;

    /**
     * http池超时时间
     */
    private int waitTimeout = DEFAULT_LOCK_TIMEOUT;

    /**
     * 每个主机的连接数
     */
    private int perhostMaxConnection = DEFAULT_PERHOST_MAX_CONNECTIONS;

    /**
     * 总的连接数
     */
    private int totalMaxConnection = DEFAULT_MAX_CONNECTIONS;


    /**
     * 多线程连接管理
     */
    private MultiThreadedHttpConnectionManager connectionManager;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (connectionManager == null) {
            connectionManager = new MultiThreadedHttpConnectionManager();

            // Protocol https = new Protocol("https",);
            log.info("设置最大并发连接数:" + totalMaxConnection);
            log.info("设置主机最大并发连接数:" + perhostMaxConnection);
            HttpConnectionManagerParams params = connectionManager.getParams();

            params.setConnectionTimeout(timeout);
            params.setDefaultMaxConnectionsPerHost(perhostMaxConnection);
            params.setMaxTotalConnections(totalMaxConnection);
            params.setSoTimeout(readTimeout);

            this.setHttpConnectionManager(connectionManager);

        }
        getParams().setConnectionManagerTimeout(waitTimeout);
    }

    @Override
    public void destroy() throws Exception {
        if (connectionManager != null) {
            connectionManager.shutdown();
            connectionManager = null;
        }
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public int getPerhostMaxConnection() {
        return perhostMaxConnection;
    }

    public void setPerhostMaxConnection(int perhostMaxConnection) {
        this.perhostMaxConnection = perhostMaxConnection;
    }

    public int getTotalMaxConnection() {
        return totalMaxConnection;
    }

    public void setTotalMaxConnection(int totalMaxConnection) {
        this.totalMaxConnection = totalMaxConnection;
    }

    public int getWaitTimeout() {
        return waitTimeout;
    }

    public void setWaitTimeout(int waitTimeout) {
        this.waitTimeout = waitTimeout;
    }

    public static void main(String[] args) throws Exception {
        //final GetMethod get = new GetMethod("http://www.baidu.com");
        final MultiThreadHttpClient3 client3 = new MultiThreadHttpClient3();
        //client3.setTimeout(1);
        //client3.setReadTimeout(1);
        client3.afterPropertiesSet();

        int size = 2000;

        for (int i = 0; i < 10; i++) {
            final CountDownLatch latch = new CountDownLatch(size);

            for (int y = 0; y < size; y++) {
                Thread thread = new Thread("worker-" + y) {
                    @Override
                    public void run() {
                        try {
                            final GetMethod get = new GetMethod("http://www.baidu.com");
                            client3.executeMethod(get);
                            get.releaseConnection();
                            System.out.println(Thread.currentThread().getName() + ":" + get.getStatusCode());
                        } catch (Exception exception) {
                            exception.getStackTrace();
                        }
                        latch.countDown();
                    }
                };
                thread.start();
            }

            latch.await();
            System.out.println("执行完毕!");

        }

        //String message = get.getResponseBodyAsString();
        //System.out.println(message);
    }

}

public class demo {

    private MultiThreadHttpClient3 httpClient;

 

    @Override
 
    public JingBeanResponse method(Request request, String url, String token) throws Exception {
        PostMethod post = null;
        try{
            post = new PostMethod(url);
            post.addRequestHeader(new Header("Content-Type","application/json;charset=UTF-8"));
            post.addRequestHeader(new Header("token",token));
            /*post.setRequestEntity(new StringRequestEntity(
                    JsonUtil.PojoTwoJson(request, JingBeanRequest.class),
                    "application/json","UTF-8"));*/
            String jsonStr=GsonUtils.toJson(request, JingBeanRequest.class, "yyyy-MM-dd HH:mm:ss");
            post.setRequestEntity(new StringRequestEntity(jsonStr, "application/json","UTF-8"));
            LOG.info("http call start, "+request.toString());
            long startTime=System.nanoTime();
            int status = httpClient.executeMethod(post);
            LOG.info("http call time:"+(System.nanoTime()-startTime)+",url:"+url);
            LOG.info("http call result:"+status+","+request.toString());
            JingBeanResponse response = null;
            if (status == HttpStatus.SC_OK){
                String responseStr = new String(post.getResponseBody(),"UTF-8");
                LOG.info(responseStr);
                response= (JingBeanResponse)
                        GsonUtils.fromJson(responseStr, JingBeanResponse.class);
            }else{
                LOG.info("http call failed, "+request.toString());
                return null;
            }
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            if (post != null){
                post.releaseConnection();
            }
        }
    }

    public MultiThreadHttpClient3 getHttpClient() {
        return httpClient;
    }

    public void setHttpClient(MultiThreadHttpClient3 httpClient) {
        this.httpClient = httpClient;
    }
}

你可能感兴趣的:(java,java)