我想做的是在tomcat启动时在Spring中初始化KieSession。并且当规则被更改并重新部署为 kie-workbench 中的 jar 时,KieSession 将动态更改。
我下载了kie-drools-wb-6.3.0.Final-tomcat7.war并部署在linux机器上。可以使用以下代码进行测试:
public class TestScanMaven {
public static void main(String[] args) {
KieServices kieServices = KieServices.Factory.get();
try {
ReleaseId releaseId = kieServices.newReleaseId("com.test","epay-risk2", "LATEST");
KieContainer kieContainer = kieServices.newKieContainer(releaseId);
KieScanner kieScanner = kieServices.newKieScanner(kieContainer);
kieScanner.start(20000L);
Scanner scanner = new Scanner(System.in);
while (true) {
runRule(kieContainer);
System.out.println("Press enter in order to run the test again....");
scanner.nextLine();
}
} catch (Exception e) {
System.out
.println("Exception thrown while constructing InputStream");
System.out.println(e.getMessage());
}
}
private static void runRule(KieContainer kieContainer) {
KieSession kSession = kieContainer.newKieSession("defaultKieSession");
// go !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
kSession.insert(message);
kSession.fireAllRules();
}
}
这段代码工作得很好。但是当我在spring中更改为使用以下代码时,它会抛出异常
@PostConstruct
public void init(){
KieServices kieServices = KieServices.Factory.get();
ReleaseId releaseId = kieServices.newReleaseId("com.test","epay-risk2", "LATEST");
KieContainer kieContainer = kieServices.newKieContainer(releaseId);
kSession = kieContainer.newKieSession("defaultKieSession");
kSession.setGlobal("riskRecordService", riskRecordService);
KieScanner kieScanner = kieServices.newKieScanner(kieContainer);
kieScanner.start(20000L);
}
抛出的异常:
Caused by: java.lang.NoSuchMethodError: com.google.inject.Binder.bindListener(Lcom/google/inject/matcher/Matcher;[Lcom/google/inject/spi/ProvisionListener;)V
at org.eclipse.sisu.plexus.PlexusBindingModule.configure(PlexusBindingModule.java:75)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.spi.Elements.getElements(Elements.java:101)
at com.google.inject.spi.Elements.getElements(Elements.java:92)
at org.eclipse.sisu.wire.WireModule.configure(WireModule.java:75)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.spi.Elements.getElements(Elements.java:101)
at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:133)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:103)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at org.codehaus.plexus.DefaultPlexusContainer.addPlexusInjector(DefaultPlexusContainer.java:477)
at org.codehaus.plexus.DefaultPlexusContainer.<init>(DefaultPlexusContainer.java:203)
at org.codehaus.plexus.DefaultPlexusContainer.<init>(DefaultPlexusContainer.java:167)
at org.kie.scanner.embedder.MavenEmbedderUtils.buildPlexusContainer(MavenEmbedderUtils.java:166)
at org.kie.scanner.embedder.MavenEmbedderUtils.buildPlexusContainer(MavenEmbedderUtils.java:140)
at org.kie.scanner.embedder.PlexusComponentProvider.<init>(PlexusComponentProvider.java:37)
at org.kie.scanner.embedder.MavenEmbedderUtils.buildComponentProvider(MavenEmbedderUtils.java:56)
at org.kie.scanner.embedder.MavenEmbedder.<init>(MavenEmbedder.java:75)
at org.kie.scanner.embedder.MavenEmbedder.<init>(MavenEmbedder.java:69)
at org.kie.scanner.embedder.MavenProjectLoader.parseMavenPom(MavenProjectLoader.java:55)
at org.kie.scanner.embedder.MavenProjectLoader.parseMavenPom(MavenProjectLoader.java:49)
at org.kie.scanner.MavenPomModelGenerator.parse(MavenPomModelGenerator.java:36)
at org.drools.compiler.kproject.xml.PomModel$Parser.parse(PomModel.java:89)
at org.drools.compiler.kie.builder.impl.AbstractKieModule.getPomModel(AbstractKieModule.java:395)
at org.drools.compiler.kie.builder.impl.AbstractKieModule.getJarDependencies(AbstractKieModule.java:126)
at org.kie.scanner.MavenClassLoaderResolver.getClassLoader(MavenClassLoaderResolver.java:64)
at org.drools.compiler.kie.builder.impl.KieModuleKieProject.<init>(KieModuleKieProject.java:68)
at org.drools.compiler.kie.builder.impl.KieModuleKieProject.<init>(KieModuleKieProject.java:56)
at org.drools.compiler.kie.builder.impl.KieBuilderImpl.buildKieModule(KieBuilderImpl.java:221)
at org.kie.scanner.KieRepositoryScannerImpl.build(KieRepositoryScannerImpl.java:220)
at org.kie.scanner.KieRepositoryScannerImpl.buildArtifact(KieRepositoryScannerImpl.java:170)
at org.kie.scanner.KieRepositoryScannerImpl.loadArtifact(KieRepositoryScannerImpl.java:126)
at org.kie.scanner.KieRepositoryScannerImpl.loadArtifact(KieRepositoryScannerImpl.java:121)
at org.drools.compiler.kie.builder.impl.KieRepositoryImpl.loadKieModuleFromMavenRepo(KieRepositoryImpl.java:129)
at org.drools.compiler.kie.builder.impl.KieRepositoryImpl.getKieModule(KieRepositoryImpl.java:115)
at org.drools.compiler.kie.builder.impl.KieRepositoryImpl.getKieModule(KieRepositoryImpl.java:92)
at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:115)
at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:111)
at com.test.risk.service.risk.epay.rule.impl.RiskKieServiceImpl.init(RiskKieServiceImpl.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:344)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:295)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130)
我的 Maven 家中的设置是:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>guvnor-m2-repo</id>
<username>drools_tomcat</username>
<password>XXXXXX</password>
<configuration>
<wagonProvider>httpclient</wagonProvider>
<httpConfiguration>
<all>
<usePreemptive>true</usePreemptive>
</all>
</httpConfiguration>
</configuration>
</server>
</servers>
<profiles>
<profile>
<id>guvnor-m2-repo</id>
<repositories>
<repository>
<id>guvnor-m2-repo</id>
<name>BRMS Repository</name>
<url>http://xxx.xxx.xxx.xxx:8082/guvnor/maven2/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>guvnor-m2-repo</activeProfile>
</activeProfiles>
</settings>