Spring boot 无法从spring启动应用程序中的计划作业执行Drools规则

Spring boot 无法从spring启动应用程序中的计划作业执行Drools规则,spring-boot,drools,Spring Boot,Drools,我是一个流口水的初学者。我有一个用例,需要从spring boot应用程序调度一个作业(cron)。在这个作业中,我需要触发存储在.xlsx文件中的规则。问题在于,当cron作业运行时,不会触发规则。但是,当我通过单元测试类运行同一个cron作业时,规则会成功执行。我试图调试该问题,但未能检测到该问题。 请在下面找到我的drools配置和cron作业 private void getKieRepository() { final KieRepository kieReposito

我是一个流口水的初学者。我有一个用例,需要从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());
        }
        }
@Autowired
DroolsConfigurationForBilling配置;
@已计划(cron=“${bill.cron.job}”)
public void calculateDailyBilling()引发ParseException异常{
log.info(“#计费作业开始于”);
String uploadDir=fileStorageProperties.getUploadDir();
LocalDate current=LocalDate.now();
字符串start=current.withDayOfMonth(1).format(模式的DateTimeFormatter.of(“yyyy-MM-dd”);
字符串end=current.withDayOfMonth(current.lengthOfMonth()).format(模式的DateTimeFormatter.of(“yyyy-MM-dd”);
SimpleDataFormat格式化程序=新的SimpleDataFormat(“yyyy-MM-dd”);
Date startDate1=格式化程序.parse(开始);
Date endDate1=格式化程序.parse(结束);
列表研究=billStudyRepository.getBillingStudyByDate(开始日期1,结束日期1);
BillBS=null;
Double hospitalRate=null,userRate=null;
计算结果bcr=null;
试一试{
研究(s:研究){
bs=billStudyRepository.findByStudyId(s.getId());
如果(bs==null){
bs=新的BillStudy();
}
bs.设置研究;
住院率=计算住院账单;
bcr=计算的八进制数;
}
log.info(“计费实用程序成功运行”);
}捕获(例外e){
log.error(“#计算研究定额账单时出错”,e);
}
私人双计算机医院账单(研究s){
bh=null;
字符串模态=modalityMapping.get(s.getModality().getName());
字符串hospitalName=s.getHospital().getName();
HospitalBillingDto=新HospitalBillingDto();
dto.setHospitalName(hospitalName);
dto.设置模态(模态);
setDoctorName(s.getSubmittedDoctor().getFirstName());
dto.setStudyDescription(s.getStudyDescription());
KieSession KieSession=config.getKieSession();
插入(dto);
int fireAllRules=kieSession.fireAllRules();
System.out.println(“Hospital”+dto.getHospitalName()+“”+dto.getmodel()+“”+dto.getHospitalRate());
// }
if(模态==null){
返回null;
}
bh=s.getHospital().getBillHospital();
如果(bh==null){
日志错误(“医院账单未定义,无法计算账单”);
返回null;
}
返回新的Double(dto.getHospitalRate());
}
}

该方法是否实际被调用(例如计费作业日志语句是否被记录)?如果调试正在运行的服务,是否可以在触发器执行时单步执行
getKieSession
,并确认已加载决策表?该方法是否真的会在进入规则之前抛出异常,而该异常不会被记录?(通常这会导致线程本身完全死亡,最终导致饥饿。)是的,正在调用该方法。最近我发现了这个链接。当我使用“java-jar”运行应用程序时,代码运行得非常好。如果在运行应用程序时代码运行得非常好,那么我不明白您的问题是什么…?当代码从eclipse运行时,cron作业运行,kiesession被创建,但来自drools规则的数据不会被触发。但是如果我使用jar运行它,代码就可以正常工作(drools规则会被触发)。这是主要问题,可能是eclipse项目的设置方式问题,例如,规则文件相对于类路径的存储位置。