首页 > 解决方案 > Drools 规则未从 Spring Boot 应用程序中的计划作业执行

问题描述

我处于 Drools 的初学者水平。我有一个用例,我需要从 Spring Boot 应用程序中安排一个作业(cron)。在这项工作中,我需要触发存储在 .xlsx 文件中的规则。问题是 cron 作业运行时没有触发规则。但是当我通过单元测试类运行相同的 cron 作业时,规则会成功执行。我试图调试问题,但无法检测到问题。请在下面找到我的 drools 配置和 cron 作业。

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();
    }

下面是我注释为@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());
        }
        }

标签: spring-bootdrools

解决方案


推荐阅读