All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xxl.job.spring.boot.XxlJobAutoBindingAndMetricsSpringExecutor Maven / Gradle / Ivy

The newest version!
package com.xxl.job.spring.boot;

import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.spring.boot.metrics.MetricMethodJobHandler;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

public class XxlJobAutoBindingAndMetricsSpringExecutor extends XxlJobAutoBindingSpringExecutor {

    private MeterRegistry registry;
    private Collection tags;

    public XxlJobAutoBindingAndMetricsSpringExecutor(MeterRegistry registry, XxlJobTemplate xxlJobTemplate, Collection tags) {
        super(xxlJobTemplate);
        this.registry = registry;
        this.tags = Objects.isNull(tags) ? Collections.emptyList() : tags;
    }

    @Override
    protected void registJobHandler(XxlJob xxlJob, Object bean, Method executeMethod) {
        if (xxlJob == null) {
            return;
        }

        String name = xxlJob.value();
        //make and simplify the variables since they'll be called several times later
        Class clazz = bean.getClass();
        String methodName = executeMethod.getName();
        if (name.trim().length() == 0) {
            throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + clazz + "#" + methodName + "] .");
        }
        if (loadJobHandler(name) != null) {
            throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
        }

        // execute method
        /*if (!(method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(String.class))) {
            throw new RuntimeException("xxl-job method-jobhandler param-classtype invalid, for[" + bean.getClass() + "#" + method.getName() + "] , " +
                    "The correct method format like \" public ReturnT execute(String param) \" .");
        }
        if (!method.getReturnType().isAssignableFrom(ReturnT.class)) {
            throw new RuntimeException("xxl-job method-jobhandler return-classtype invalid, for[" + bean.getClass() + "#" + method.getName() + "] , " +
                    "The correct method format like \" public ReturnT execute(String param) \" .");
        }*/

        executeMethod.setAccessible(true);

        // init and destroy
        Method initMethod = null;
        Method destroyMethod = null;

        if (xxlJob.init().trim().length() > 0) {
            try {
                initMethod = clazz.getDeclaredMethod(xxlJob.init());
                initMethod.setAccessible(true);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + clazz + "#" + methodName + "] .");
            }
        }
        if (xxlJob.destroy().trim().length() > 0) {
            try {
                destroyMethod = clazz.getDeclaredMethod(xxlJob.destroy());
                destroyMethod.setAccessible(true);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + clazz + "#" + methodName + "] .");
            }
        }

        // registry jobhandler
        registJobHandler(name, new MetricMethodJobHandler(registry, bean, executeMethod, initMethod, destroyMethod, tags));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy