gpt4 book ai didi

java - Spring Boot + jpa + 调度器

转载 作者:行者123 更新时间:2023-12-02 13:37:02 24 4
gpt4 key购买 nike

  `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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com