package com.njzscloud.common.sichen.support;

import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

@Slf4j
@Getter
@ToString
public class FnHandle {
    private final String fn;
    private final String defineName;
    private final Object obj;
    private final Method method;

    public FnHandle(String fn, Object obj, Method method) {
        this.fn = fn;
        this.obj = obj;
        this.method = method;
        this.defineName = obj.getClass().getCanonicalName() + "#" + method.getName();
    }

    public boolean exec() {
        try {
            if (obj == null || method == null) {
                return false;
            }
            method.invoke(obj);
            return true;
        } catch (Throwable e) {
            log.error("任务执行失败：{}", defineName, e);
            return false;
        }
    }
}
