java - Spring Boot + jpa + 调度器

标签 java spring spring-boot scheduled-tasks spring-data-jpa

  `package com.example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.example.model.Fan;
import com.example.model.Led;
import com.example.model.Nutrient;
import com.example.model.Water;
import com.example.service.FanScheduleService;
import com.example.service.FanService;
import com.example.service.LedScheduleService;
import com.example.service.LedService;
import com.example.service.NutrientScheduleService;
import com.example.service.NutrientService;
import com.example.service.WaterScheduleService;
import com.example.service.WaterService;


@EnableScheduling
@Service
//cron = "${scheduling.job.cron}"
public class ScheduleJob {
    @Scheduled(fixedDelay=500,initialDelay=500)

    public void run() throws ParseException {
        LedSchedule();
        FanSchedule();
        NutrientSchedule();
        WaterSchedule();

    }
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private FanService fanService;
    @Autowired
    private FanScheduleService fanScheduleService;
    @Autowired
    private LedService ledService;
    @Autowired
    private LedScheduleService ledScheduleService;
    @Autowired
    private NutrientService nutrientService;
    @Autowired
    private NutrientScheduleService nutrientScheduleService;
    @Autowired
    private WaterService waterService;
    @Autowired
    private WaterScheduleService waterScheduleService;

    public void FanSchedule() throws ParseException{
        boolean test = true;
        Date start = null;
        Date end = null;
        Fan fan = new Fan();
        for (int i = 0; i < fanScheduleService.findAll().size(); i++) {
            String start_date = fanScheduleService.findAll().get(i).getFan_activestartdate();
            String end_date = fanScheduleService.findAll().get(i).getFan_activeenddate();
            start = dateFormat.parse(start_date);
            end = dateFormat.parse(end_date);

            fan = fanService.findById(fanScheduleService.findAll().get(i).getFan().getFan_id());
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                test = true;
            } else if( end.before(new Date())) {
                test = false;
                }
        }
        while(test==true){
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                fan.setFan_status(true);

            }else{
                fan.setFan_status(false);

            }

            try {
                fanService.update(fan);

            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            break;
        }
    }

    public void LedSchedule() throws ParseException{
        boolean test = true;
        Date start = null;
        Date end = null;
        Led led = new Led();
        for (int i = 0; i < ledScheduleService.findAll().size(); i++) {
            String start_date = ledScheduleService.findAll().get(i).getLed_activestartdate();
            String end_date = ledScheduleService.findAll().get(i).getLed_activeenddate();
            start = dateFormat.parse(start_date);
            end = dateFormat.parse(end_date);

            led = ledService.findById(ledScheduleService.findAll().get(i).getLed().getLed_id());
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                test = true;

            } else if( end.before(new Date())) {
                test = false;
                }
        }
        while(test==true){
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                led.setLed_status(true);

            }else{
                led.setLed_status(false);

            }

            try {
                ledService.update(led);

            } catch (Exception e) {

                System.out.println(e.getMessage());
            }
            break;
        }
    }

    public void NutrientSchedule() throws ParseException{
        boolean test = true;
        Date start = null;
        Date end = null;
        Nutrient nutrient = new Nutrient();
        for (int i = 0; i < nutrientScheduleService.findAll().size(); i++) {
            String start_date = nutrientScheduleService.findAll().get(i).getNutrient_activestartdate();
            String end_date = nutrientScheduleService.findAll().get(i).getNutrient_activeenddate();
            start = dateFormat.parse(start_date);
            end = dateFormat.parse(end_date);

            nutrient = nutrientService.findById(nutrientScheduleService.findAll().get(i).getNutrient().getNutrient_id());
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                test = true;
            } else if( end.before(new Date())) {
                test = false;
                }
        }
        while(test==true){
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                nutrient.setNutrient_status(true);

            }else{
                nutrient.setNutrient_status(false);

            }
            try {
                nutrientService.update(nutrient);

            } catch (Exception e) {

                System.out.println(e.getMessage());
            }
            break;
        }
    }

    public void WaterSchedule() throws ParseException{
        boolean test = true;
        Date start = null;
        Date end = null;
        Water water = new Water();
        for (int i = 0; i < waterScheduleService.findAll().size(); i++) {
            String start_date = waterScheduleService.findAll().get(i).getWater_activestartdate();
            String end_date = waterScheduleService.findAll().get(i).getWater_activeenddate();
            start = dateFormat.parse(start_date);
            end = dateFormat.parse(end_date);

            water = waterService.findById(waterScheduleService.findAll().get(i).getWater().getWater_id());
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                test = true;    
            } else if( end.before(new Date())) {
                test = false;
                }
        }
        while(test==true){
            if (start.before(new Date()) == true && end.after(new Date())==true ) {
                water.setWater_status(true);

            }else{
                water.setWater_status(false);

            }
            try {
                waterService.update(water);

            } catch (Exception e) {

                System.out.println(e.getMessage());
            }
            break;

大家好。我正在尝试使用 Spring boot 和 Scheduler 来更新我的数据库中的某些内容。像下面的代码 FanSchedule() 工作得很好。但其他方法无法正常工作。我正在尝试将这些状态设置为 true。但有时这些状态会在 5 秒内变为 false,然后又变回 true。请帮助我解决这个问题,非常感谢。

更多

Hibernate: update waterpump_device set temp_max=?, name=?, status=? where id=?
Hibernate: update led_device set name=?, status=?, temp_min=? where id=?
Hibernate: update nutrientpump_device set name=?, status=? where id=?

只有 Fan 不由 Hibernate 更新。我认为当 Hibernate 更新时会将这些状态更改为 false?

最佳答案

任务有超时时间,如果X秒内没有执行,调度程序将杀死该任务。您可以做的是使用 @Async 调用第二个方法,然后在那里执行您的代码。查看 Spring 文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-async

关于java - Spring Boot + jpa + 调度器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42932547/

相关文章:

java - 与注释为 @Transactional 的方法的调用者进行通信

java - Spring MVC 中带有抽象类的 @ModelAttribute

java - 数据库更新后运行的 PUT 和 PATCH 的 Spring 数据剩余验证

java - 使用@DataJpaTest 时未配置 JdbcTemplate

java - 如何正确使用枚举进行简单的价格计算 (Java)

java - 查找与正则表达式集匹配的字符

java - 100% CPU 利用率需要帮助分析线程转储

java - 从 Eclipse 插件使用 Jython

java - 强制 Spring Boot 不使用 EmbeddedWebApplicationContext?

java - 单个 @StreamListener 将不同的有效负载类型路由到通用服务类