package com.engine.htsc.doc.common;

import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
 * 执行对象的某个方法
 * @author Administrator
 *
 */
public class AsyncThread extends Thread {
    private static Logger log = LoggerFactory.getLogger(AsyncThread.class);
    private Object async;
    private String method;
    private Object param[];
    private String key;

    public AsyncThread(Object o,String method,Object[] param){
        this.async =o;
        this.method = method;
        this.param  = param;
    }

    @Override
    public void run() {
        long time1 = System.currentTimeMillis();
        log.info("开始ClassName:"+async.getClass().getName()+"||method:"+method);
        if(null==async||StringUtils.isEmpty(method)){
            return;
        }
        Object a =async+method;
        synchronized (a) {
            try {
                Method[] methods= async.getClass().getDeclaredMethods();
                for (Method invokeMethod : methods) {
                    if(invokeMethod.getName().equals(method)){
                        Class<?>[] parameterTypes = invokeMethod.getParameterTypes();
                        if(null!=parameterTypes){
                            log.info("方法："+method+",参数个数:"+parameterTypes.length);
                            //参数不相等，继续查找
                            if(null==param||param.length!=parameterTypes.length){
                                continue;
                            }
                        }else{
                            //参数不相等，继续查找
                            if(null!=param&&param.length!=0){
                                continue;
                            }
                        }
                        invokeMethod.setAccessible(true);
                        invokeMethod.invoke(async, param);
                        break;
                    }
                }
//				Class c [] = new Class[param.length];
//				for (int i = 0; i < param.length; i++) {
//					c[i]  = param[i].getClass();
//				}
//	 			Method invokeMethod= async.getClass().getDeclaredMethod(method,c);
//
//	 			Method[] methods= async.getClass().getDeclaredMethods();
            } catch(Exception e) {
                e.printStackTrace();
                log.error(e.getMessage(), e);
            }
        }
        log.info("结束ClassName:"+async.getClass().getName()+"||method:"+method+"||耗时："+(System.currentTimeMillis()-time1));
        if(StringUtils.isNotEmpty(key)){
            ThreadPoolUtil.hashMap_thread.put(key, "1");
        }
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

}