定时任务+定制化job

不考虑实用性,把要执行的任务放到网上

1.使用spring自带的ThreadPoolTaskScheduler

@Component
public class SchedulerConfig {

    // 创建线程
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.setPoolSize(20);
        executor.setThreadNamePrefix("taskExecutor-");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        return executor;
    }

}

2.使用自定义classLoader,把类从网上读取到本地并且加载


public class MyClassLoad  extends  ClassLoader {

    private String url ;
    public MyClassLoad(String url ) {
        this.url = url ;
    }

    @SneakyThrows

    @Override
    protected Class findClass(String name) throws ClassNotFoundException {

        Class clazz ;
        this.url = classNameToPath(name);
        byte[] classInfo = getClassInfo();
        clazz = defineClass(name, classInfo, 0, classInfo.length);
        return clazz;
    }


    public byte[] getClassInfo() throws IOException {
        @Cleanup
        InputStream inputStream  = null ;

        URL u = new URL(this.url);

        int length = - 1;
        byte[] bytes = new byte[1024*4];

        inputStream = u.openStream();
        @Cleanup
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();


        while((length=inputStream.read(bytes))!=-1){

            byteArrayOutputStream.write(bytes,0,length);
        }
        return byteArrayOutputStream.toByteArray();

    }
    private String classNameToPath(String name) {

        return url + "/"+name.replace(".","/")+".class";
    }
 
}

3.创建一个Util通过名称获取类

  public static Runnable getRunAble(String name){
        try {

            MyClassLoad myClassLoad = new MyClassLoad("http://****/");
            Class ax = null;
            ax = myClassLoad.loadClass(name);

            return (Runnable) ax.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return  null;
    }

4.Controller

package com.boolib.taskscheduler2.web;

import com.boolib.taskscheduler2.util.ClassUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ScheduledFuture;

@RestController
public class WebController {


    private final  String pre ="com.boolib.job.";
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    ScheduledFuture schedule ;



    @GetMapping("start")
    public String start(@RequestParam("classname") String classname,@RequestParam("cron") String cron){
        schedule = threadPoolTaskScheduler.schedule(ClassUtil.getRunAble(build(classname)), new CronTrigger(cron));
        return  "ok";
    }


    @GetMapping("stop")
    public String stop( ){
        schedule.cancel(true);
        return  "ok";
    }




//    @GetMapping("suspend")
//    public String suspend( ){
//        schedule.cancel(true);
//        return  "ok";
//    }
//
//    @GetMapping("resupend")
//    public String resupend(@RequestParam("classname") String classname,@RequestParam("cron") String cron ){
//
//        return  "ok";
//    }

    private String build(String name ){
        System.out.println(name);
        return pre+name;
    }
}

你可能感兴趣的:(定时任务+定制化job)