Drools-6 (kie) automatic scanning (from spring) of modules and sessions from the involved kie workbench artifacts

I am trying to create a web application (spring -mvc) with kie (drools 6) integrated by injection. I used the workplace to create a workflow, completed and deployed. I added a link to this artifact in my pom.xml project and added a local kie-workbench repository according to this blog post and it works fine (pulling an artifact depending on my maven / spring project). What I'm trying to do is inject kiesession into one of my services as a dependency with the following snippet -

@Service public class TniServiceImpl implements TniService { @Inject @KSession("tniSession") private KieSession tniSession; ... } 

In my root .xml context, I added the kie namespace as well as the xsd link. I added org.kie.spring.KModuleBeanFactoryPostProcessor, as well as roll documentation. I am trying to do a CDI injection job for scanning and KSession injection (it already works for my other components in the same project using @Inject). Until now, I always get a “No qualification bean of type [org.kie.api.runtime.KieSession] found for the“ error ”dependency. It looks like spring cannot scan for available modules and sessions in it. I need help with the following -

  • Is CDI injection really supported with spring? Should I automatically configure kmodules and kession as indicated here ?
  • I missed something here, what should this scan and injection operation do?

My next environment is

  • spring 3.2.6-RELEASE (including webmvc and other components)
  • kie-api-6.0.1.FINAL
  • kie- spring -6.0.1.FINAL
  • kie-internal-6.0.1.FINAL

I already went through the links, but no luck (basically they are not trying to do what I am) -

  • Download Drools / KIE Workbench artifacts directly from the repository
  • Why does loading Drools 6 KIE JAR into code not work?

I would appreciate it if someone could direct me to what might be missing here, or if there is no option but to explicitly define all the module files / ksessions in the spring configuration file.

+8
spring drools jbpm kie
source share
3 answers

I had the same problem and found a solution here: http://drools.46999.n3.nabble.com/Spring-4-0-amp-Drools-6-0-1-Integration-issue-td4028052.html

Basically you will need to enter ApplicationContext instead of kieSession and get the xml bean manually.

TniServiceImpl.java

 @Service public class TniServiceImpl implements TniService { @Inject ApplicationContext context; KieSession kieSession; @PostConstruct public void postConstruct(){ kieSession = (KieSession) context.getBean("ksession1"); } ... } 

root context.xml

  <kie:kmodule id="kmodule1"> <kie:kbase name="kbase1"> <kie:ksession name="ksession1" /> </kie:kbase> </kie:kmodule> <bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" /> 

Hope this helps.

UPDATE:

Another way to achieve this is to keep the xml identical, and instead of trying to enter KieSession, enter KieBase. Then, using the KieBase instance, create a new KieSessions.

 @Service public class TniServiceImpl implements TniService { @Autowired private KieBase kbase; /* inside some method */ @RequestMapping(method=RequestMethod.GET) public @ResponseBody Data getData() { KieSession ksession = kbase.newKieSession(); ... } } 
+4
source share

The above answer does not work with spring mvc. I found that this is a bug in existing salivary, and they correct it in the next version. I am stuck at this point since I am using DROOLS in batch mode, but I want it to be used in the REST service hosted by websphere. The above solution works great in a batch program.

0
source share

This is what I work with the latest Spring MVC (Spring Boot)

 @SpringBootApplication public class DroolDemoApplication { public static void main(String[] args) { SpringApplication.run(DroolDemoApplication.class, args); } @Bean public KieContainer kieContainer() { return KieServices.Factory.get().getKieClasspathContainer(); } @Bean public KieSession kieSession() throws IOException { return kieContainer().newKieSession("DroolDemoSession"); } } 

and below - kmodule.xml

 <kbase name="DroolDemoKbase" packages="rules"> <ksession name="DroolDemoSession" /> </kbase> 

Finally, everything you do in your controller,

 @Autowired private KieSession kieSession; kieSession.fireAllRules(); 

hope this helps those who still have problems.

0
source share

All Articles