0

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());
        }
        }
Ajinkya
  • 77
  • 4
  • 13
  • Is the method actually being called (eg does the billing job log statement get logged)? If you debug the running service, can you step through the `getKieSession` when the trigger executes and confirm that the decision table gets loaded? Could the method actually be throwing an exception prior to getting to the rules, and the exception not being logged? (Usually this causes the thread itself to become completely dead leading to eventual starvation.) – Roddy of the Frozen Peas Feb 21 '20 at 17:41
  • Yes, the method is getting called. Recently I came across this link [stackoverflow](https://stackoverflow.com/a/42990405/7991294t). The code works perfectly fine when I run the application using 'java -jar'. – Ajinkya Feb 24 '20 at 09:38
  • If it works perfectly fine when running the application, then I don't understand what your problem is...? – Roddy of the Frozen Peas Feb 24 '20 at 18:26
  • when the code is running from eclipse, the cron job runs, kiesession gets created, but data from drools rules are not gettting fired. But if I run it using jar the code works fine (drools rules get fired). This was the main issue. – Ajinkya Feb 25 '20 at 06:44
  • Likely it is an issue with the way your eclipse project is set up, for example where your rule files are stored relative to the classpath. – Roddy of the Frozen Peas Feb 25 '20 at 14:34

0 Answers0