I am at beginner level for Drools. I have a use case where I need to schedule a job (cron) from the spring boot application. And within this job I need to fire rules which are stored in the .xlsx file. The issue is that the rules are not getting fired when the cron job runs. But the rules executes successfully when I run the same cron job via unit test class. I tried to debug the issue but couldn't detect the issue. Please find below my drools configuration and the cron job.
private void getKieRepository() {
final KieRepository kieRepository = kieServices.getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
}
public KieSession getKieSession() {
getKieRepository();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(ResourceFactory.newClassPathResource("rules/hospitalRules.xlsx"));
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
KieModule kieModule = kb.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kContainer.newKieSession();
}
Below is my method annotated as @Schedule:
@Autowired
DroolsConfigurationForBilling config;
@Scheduled(cron = "${bill.cron.job}")
public void calculateDailyBilling() throws ParseException {
log.info("# Billing job started at ");
String uploadDir = fileStorageProperties.getUploadDir();
LocalDate current = LocalDate.now();
String start = current.withDayOfMonth(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String end = current.withDayOfMonth(current.lengthOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate1 = formatter.parse(start);
Date endDate1 = formatter.parse(end);
List<Study> studies = billStudyRepository.getBillingStudyByDate(startDate1, endDate1);
BillStudy bs = null;
Double hospitalRate = null, userRate = null;
BillComputationResult bcr = null;
try {
for (Study s : studies) {
bs = billStudyRepository.findByStudyId(s.getId());
if (bs == null) {
bs = new BillStudy();
}
bs.setStudy(s);
hospitalRate = computeHospitalBill(s);
bcr = computeDoctorBill(s);
}
log.info("# Billing utility run successfully");
} catch (Exception e) {
log.error("# Error while calculating flat bill for studies.", e);
}
private Double computeHospitalBill(Study s) {
BillHospital bh = null;
String modality = modalityMapping.get(s.getModality().getName());
String hospitalName = s.getHospital().getName();
HospitalBillingDto dto = new HospitalBillingDto();
dto.setHospitalName(hospitalName);
dto.setModality(modality);
dto.setDoctorName(s.getSubmittedDoctor().getFirstName());
dto.setStudyDescription(s.getStudyDescription());
KieSession kieSession = config.getKieSession();
kieSession.insert(dto);
int fireAllRules = kieSession.fireAllRules();
System.out.println("Hospital " + dto.getHospitalName() + " " + dto.getModality() + " " + dto.getHospitalRate());
// }
if (modality == null) {
return null;
}
bh = s.getHospital().getBillHospital();
if (bh == null) {
log.error("Hospital bill not defined, could not compute bill");
return null;
}
return new Double(dto.getHospitalRate());
}
}