如何使用Spring Task Scheduler在失败的情况下重新触发计划的作业

问题描述

以下是使用Spring Boot任务计划程序创建的任务计划程序。 sqltoelasticsearch是一种基于cron触发器在内部计划作业1中调用方法。在sqltoelasticsearch方法中,我使用@Retryable批注设置延迟20秒引发错误时的最大尝试次数,但未触发。实际上,我的意图是在10分钟后仅一次失败的情况下重新触发该方法或作业。还在Springboot主应用程序页面中启用了@Enablescheduling和@Enableretry。

private void scheduleJob1(TaskScheduler scheduler) {
        job1 = scheduler.schedule(new Runnable() {
            @Override
            public void run() {
                if (job1Flag == false) {
                    sqltoelasticsearch();
                    System.out.println(Thread.currentThread().getName() + " The Task1 executed at " + new Date());
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // Todo Auto-generated catch block
                    e.printstacktrace();
                }
            }
        },new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                String cronExp = "* 38 11 * * ?";// Can be pulled from a db
                return new crontrigger(cronExp).nextExecutionTime(triggerContext);
            }
        });
    }

@Retryable(maxAttempts = 4,value = Exception.class,backoff = @Backoff(delay = 20000))
    public void sqltoelasticsearch() {
        try {
            HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8040/getsqldata")
                    .openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setConnectTimeout(20 * // minutes to sleep
                    60 * // seconds to a minute
                    1000); // set timeout to 1 second
            BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String output;
            JSONObject outStream = new JSONObject();
            while ((output = reader.readLine()) != null) {
                JSONObject outPut = new JSONObject(output);
                outStream = outPut;
            }
            int outputStatuscode = outStream.getInt("statusCode");
            if ((con.getResponseCode() == HttpURLConnection.HTTP_OK) && outputStatuscode == 200) {

                job1Flag = true;

                // setting the flag true to mark it complete
            } else {
                throw new Exception();

            }
        } catch (Exception ex) {
            ex.printstacktrace();
        }

    }

我们如何使用@Retryable或其他更好的解决方案来实现这一目标

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)