0
`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启动和调度程序来更新我的数据库中的某些东西。就像下面的代码FanSchedule()工作得很好。但其他方法工作不正确。我试图将这些状态设置为true。但有时候,这些变化的状态会变成假,持续5秒并变回真。请帮助我解决这个问题非常感谢。Spring boot + jpa + scheduler
更多
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=?
只有风扇不受休眠更新。我认为当Hibernate更新它时,将这些状态更改为false?
您是否尝试添加@Transactional来运行()? –
@ zakariaamine我已经添加它,但它不起作用。所以我删除了 –