动态从 Drool 引擎读取 .xlsx 文件,无需重新启动 spring 项目
我正在尝试使用 drool 读取我的 excel 文件 我能够成功地从 s3 读取该文件,但是当我对该文件进行更改并用新文件替换现有文件时出现问题 我的规则引擎仍然读取旧文件值 谁能告诉我可以采取什么措施来解决此问题
Dto
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Getter
@Setter
@ToString
@Component
@RefreshScope
public class DataDTO {
String firstName;
String lastName;
}
Drool 配置
package com.example.demo.Config;
import lombok.extern.slf4j.Slf4j;
import org.drools.decisiontable.DecisionTableProviderImpl;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
@Slf4j
@RefreshScope
public class DroolConfig {
private KieServices kieServices = KieServices.Factory.get();
public KieFileSystem getKieFileSystem() throws IOException {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
Resource resource;
log.info("File read form local resource folder");
resource = ResourceFactory
.newUrlResource("awsurl/123.xlsx");
kieFileSystem.write(resource);
DecisionTableProviderImpl decisionTableProvider
= new DecisionTableProviderImpl();
String leadBox = decisionTableProvider.loadFromResource(resource, null);
log.info("Rules Loaded from LeadBoxFile:{}", leadBox);
return kieFileSystem;
}
@Bean
@RefreshScope
public KieContainer getKieContainer() throws IOException {
getKieRepository();
KieBuilder abc = kieServices.newKieBuilder(getKieFileSystem());
abc.buildAll();
KieModule kieModule = abc.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kContainer;
}
private void getKieRepository() {
final KieRepository kieRepository = kieServices.getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
}
@Bean
public KieSession getKieSession() throws IOException {
return getKieContainer().newKieSession();
}
}
控制器
package com.example.demo;
import com.example.demo.Config.DroolConfig;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@RestController
@RequestMapping("/v2")
public class LoadData {
@Autowired
private KieSession kieSession;
@Autowired
private final KieContainer kieContainer;
@Autowired
DroolConfig droolConfig;
public LoadData(KieSession kieSession, KieContainer kieContainer) {
this.kieSession = kieSession;
this.kieContainer = kieContainer;
}
@PostMapping("/printData")
public String printData(@RequestBody DataDTO dataDTO) throws IOException {
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(dataDTO);
kieSession.fireAllRules();
kieSession.dispose();
return dataDTO.getLastName();
}
@GetMapping("/loadDroolFile")
public String getDroolData() throws IOException {
droolConfig.getKieFileSystem();
kieContainer.getKieSessionConfiguration();
return "Done";
}
}
每当我尝试点击 loadDroolFile 时,我都能够获取刷新的数据,但数据不会被注入,我仍然使用旧值从 drool 引擎获得结果
I am trying to read my excel file using drool I am able to successfully read the file from s3 but the problem comes when I make changes to that file and replace the existing file with new file my rule engine still reads old file values can anyone tell what can be done to resolve this issue
Dto
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Getter
@Setter
@ToString
@Component
@RefreshScope
public class DataDTO {
String firstName;
String lastName;
}
Drool Config
package com.example.demo.Config;
import lombok.extern.slf4j.Slf4j;
import org.drools.decisiontable.DecisionTableProviderImpl;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
@Slf4j
@RefreshScope
public class DroolConfig {
private KieServices kieServices = KieServices.Factory.get();
public KieFileSystem getKieFileSystem() throws IOException {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
Resource resource;
log.info("File read form local resource folder");
resource = ResourceFactory
.newUrlResource("awsurl/123.xlsx");
kieFileSystem.write(resource);
DecisionTableProviderImpl decisionTableProvider
= new DecisionTableProviderImpl();
String leadBox = decisionTableProvider.loadFromResource(resource, null);
log.info("Rules Loaded from LeadBoxFile:{}", leadBox);
return kieFileSystem;
}
@Bean
@RefreshScope
public KieContainer getKieContainer() throws IOException {
getKieRepository();
KieBuilder abc = kieServices.newKieBuilder(getKieFileSystem());
abc.buildAll();
KieModule kieModule = abc.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kContainer;
}
private void getKieRepository() {
final KieRepository kieRepository = kieServices.getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
}
@Bean
public KieSession getKieSession() throws IOException {
return getKieContainer().newKieSession();
}
}
Controller
package com.example.demo;
import com.example.demo.Config.DroolConfig;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@RestController
@RequestMapping("/v2")
public class LoadData {
@Autowired
private KieSession kieSession;
@Autowired
private final KieContainer kieContainer;
@Autowired
DroolConfig droolConfig;
public LoadData(KieSession kieSession, KieContainer kieContainer) {
this.kieSession = kieSession;
this.kieContainer = kieContainer;
}
@PostMapping("/printData")
public String printData(@RequestBody DataDTO dataDTO) throws IOException {
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(dataDTO);
kieSession.fireAllRules();
kieSession.dispose();
return dataDTO.getLastName();
}
@GetMapping("/loadDroolFile")
public String getDroolData() throws IOException {
droolConfig.getKieFileSystem();
kieContainer.getKieSessionConfiguration();
return "Done";
}
}
Whenever I try to hit loadDroolFile I am able to get refreshed data but the data does not get injected I still get the result from drool engine using old values
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论