package org.dromara.settlement.common.http;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;

/**
 * 异步请求回调
 * @author 张涛
 * @date 2016年12月11日
 */
public class SynResponse implements FutureCallback<HttpResponse>{
	private HttpPost postMethod;
	/**
	 * 异步请求线程
	 */
	private SynRequestThread synThread;

	public SynResponse(SynRequestThread synThread,HttpPost postMethod){
		this.synThread = synThread;
		this.postMethod = postMethod;
	}

	@Override
	public void completed(HttpResponse paramT) {
		// TODO 自动生成的方法存根
		releaseConnection();
	}

	@Override
	public void cancelled() {
		// TODO 自动生成的方法存根
		releaseConnection();
	}

	@Override
	public void failed(Exception paramException) {
		// TODO 自动生成的方法存根
		releaseConnection();
	}

	/**
	 * 释放连接
	 * @author 张涛
	 * @date 2017年4月10日
	 */
	private void releaseConnection(){
		if(postMethod != null)
			postMethod.releaseConnection();
		int num = synThread.synReqCount.decrementAndGet();
		if (num <= 0)
			synThread.synReqOpen.countDown();
	}
}
