Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在春季运行Cron作业_Java_Spring - Fatal编程技术网

Java 如何在春季运行Cron作业

Java 如何在春季运行Cron作业,java,spring,Java,Spring,我在spring中创建了一个类ApplicationDataScheduler,它有一个带有@Scheduled注释的方法reportGenerator。此方法以excel的形式生成报告,excel包含表中的数据。因此,类ApplicationDataScheduler和方法reportGenerator在intelliJ中都显示为浅灰色,表示从未使用过类和方法。我需要在什么地方申报或打电话给他们吗? 代码如下: @Component public class ApplicationD

我在spring中创建了一个类ApplicationDataScheduler,它有一个带有@Scheduled注释的方法reportGenerator。此方法以excel的形式生成报告,excel包含表中的数据。因此,类ApplicationDataScheduler和方法reportGenerator在intelliJ中都显示为浅灰色,表示从未使用过类和方法。我需要在什么地方申报或打电话给他们吗? 代码如下:

    @Component 
public class ApplicationDataScheduler { 
@Autowired 
HibernateUtils hibernateUtils; 
 
@Scheduled(cron = "0 0 0 * * *") 
public void reportGenerator() throws IOException { 
List<Application> applications = hibernateUtils.getAllEntities(Application.class); 
XSSFWorkbook exportedData = convertToExcel(applications); 
FileOutputStream output = new FileOutputStream(new File("Applications.xlsx")); 
exportedData.write(output); 
output.close(); 
} 
 
private XSSFWorkbook convertToExcel(List<Application> applications) { 
XSSFWorkbook workbook = new XSSFWorkbook(); 
XSSFSheet sheet = workbook.createSheet("applications_data"); 
int row_index = 0; 
for (Application app : applications) { 
XSSFRow row = sheet.createRow(row_index++); 
fill_row(app, row); 
} 
return workbook; 
} 
 
private void fill_row(Application app, XSSFRow row) { 
XSSFCell cell = row.createCell(0); 
cell.setCellValue(app.getApplicationId()); 
 
cell = row.createCell(1); 
cell.setCellValue(app.getAppCode()); 
 
cell = row.createCell(2); 
cell.setCellValue(app.getApplicationSubmittedDate()); 
 
cell = row.createCell(3); 
cell.setCellValue(app.getRemarks()); 
 
cell = row.createCell(4); 
cell.setCellValue(app.getCreatedByUser()); 
 
cell = row.createCell(5); 
cell.setCellValue(app.getUpdatedByUser()); 
} 
} 
@组件
公共类ApplicationDataScheduler{
@自动连线
冬眠动物;
@已计划(cron=“0***”)
public void reportGenerator()引发IOException{
List applications=hibernateUtils.getAllenties(Application.class);
XSSFWorkbook exportedData=convertToExcel(应用程序);
FileOutputStream输出=新的FileOutputStream(新文件(“Applications.xlsx”);
exportedData.write(输出);
output.close();
} 
私有XSSF工作簿convertToExcel(列表应用程序){
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet(“应用程序数据”);
int row_index=0;
对于(应用程序:应用程序){
XSSFRow row=sheet.createRow(row_index++);
填充行(应用,行);
} 
返回工作簿;
} 
专用空白填充_行(应用程序应用程序,XSSFRow行){
XSSFCell cell=row.createCell(0);
cell.setCellValue(app.getApplicationId());
cell=row.createCell(1);
cell.setCellValue(app.getAppCode());
cell=row.createCell(2);
cell.setCellValue(app.getApplicationSubmittedDate());
cell=row.createCell(3);
cell.setCellValue(app.getComments());
cell=row.createCell(4);
cell.setCellValue(app.getCreatedByUser());
cell=row.createCell(5);
cell.setCellValue(app.getUpdatedByUser());
} 
} 

首先,检查它是否确实没有运行,因为一切看起来都很好。例如,您可以将
logger.info
放在计划任务的某个位置,然后运行应用程序。关于IntelliJ,它没有突出显示您的方法,这完全没问题,因为您自己的代码中没有任何东西运行它,它是从spring框架调用的。

您是否尝试在主类或配置类上添加@EnableScheduling?@VaibS是的,这是在主类alreadyyes中添加的,实际上它没有运行,我添加了一些打印语句,所以它不会被执行。由于没有运行,是否缺少任何内容?首先,如果您可以对
reportGenerator
进行单元测试,那就太好了。我还建议您将
@Scheduled(cron=“0***”)临时更改为
@Scheduled(fixedDelay=5000)
,这样您就不需要等到午夜了。可能它正在运行,并且日志中没有显示异常。