2017-09-13 135 views
0

現在我想在一個印跡中使用Drools,它在LocalCluster中正常工作,但是當我將它放到生產羣集時,它有錯誤。 印跡是:如何在風暴拓撲結構中使用drools

public class DealLostBolt extends BaseRichBolt { 

     private static final long serialVersionUID = 1L; 

     private static final Logger LOGGER = LoggerFactory.getLogger("DEAL_LOST_BOLT"); 

     private OutputCollector collector; 

     private KieSession kieSession; 

     private FactHandle factHandle; 

     @Override 
     public void execute(Tuple input) { 
     // 獲取數據 
     String sentence = (String) input.getValue(0); 
     LOGGER.info("DealLostBolt獲取到的數據:" + sentence); 

     // 數據轉換 
     PutDataPoint dataPoint = Json.fromJson(PutDataPoint.class, sentence); 

     KieServices ks = KieServices.Factory.get(); 
     KieContainer kieContainer = ks.getKieClasspathContainer(); 
     kieSession = kieContainer.newKieSession("all-rule"); 
     kieSession.getAgenda().getAgendaGroup("deal-lost").setFocus(); 

     factHandle = kieSession.insert(dataPoint); 
     kieSession.fireAllRules(); 
     kieSession.delete(factHandle); 

     collector.emit(new Values(sentence)); 
     } 

     @Override 
     public void declareOutputFields(OutputFieldsDeclarer declarer) { 
     declarer.declare(new Fields("value")); 

     } 

     @Override 
     public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { 
     this.collector = collector; 
     } 

    } 

我用官方文件創建kiesession。 的誤差修改爲:

java.lang.RuntimeException: java.lang.NullPointerException 
    at org.apache.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:495) ~[storm-core-1.1.1.jar:1.1.1] 
    at org.apache.storm.utils.DisruptorQueue.consumeBatchWhenAvailable(DisruptorQueue.java:460) ~[storm-core-1.1.1.jar:1.1.1] 
    at org.apache.storm.disruptor$consume_batch_when_available.invoke(disruptor.clj:73) ~[storm-core-1.1.1.jar:1.1.1] 
    at org.apache.storm.daemon.executor$fn__5030$fn__5043$fn__5096.invoke(executor.clj:848) ~[storm-core-1.1.1.jar:1.1.1] 
    at org.apache.storm.util$async_loop$fn__557.invoke(util.clj:484) [storm-core-1.1.1.jar:1.1.1] 
    at clojure.lang.AFn.run(AFn.java:22) [clojure-1.7.0.jar:?] 
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121] 
    Caused by: java.lang.NullPointerException 
    at org.kie.internal.io.ResourceFactory.newByteArrayResource(ResourceFactory.java:66) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.AbstractKieModule.getResource(AbstractKieModule.java:299) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.AbstractKieModule.addResourceToCompiler(AbstractKieModule.java:264) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.AbstractKieModule.addResourceToCompiler(AbstractKieModule.java:259) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.AbstractKieProject.buildKnowledgePackages(AbstractKieProject.java:228) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.AbstractKieModule.createKieBase(AbstractKieModule.java:206) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.createKieBase(KieContainerImpl.java:584) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:552) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:680) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:648) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at cn.ennwifi.storm.bolt.DealLostBolt.execute(DealLostBolt.java:52) ~[se-storm-0.0.1-SNAPSHOT-jar-with-dependencies.jar:?] 
    at org.apache.storm.daemon.executor$fn__5030$tuple_action_fn__5032.invoke(executor.clj:729) ~[storm-core-1.1.1.jar:1.1.1] 
    at org.apache.storm.daemon.executor$mk_task_receiver$fn__4951.invoke(executor.clj:461) ~[storm-core-1.1.1.jar:1.1.1] 
    at org.apache.storm.disruptor$clojure_handler$reify__4465.onEvent(disruptor.clj:40) ~[storm-core-1.1.1.jar:1.1.1] 
    at org.apache.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:482) ~[storm-core-1.1.1.jar:1.1.1] 
    ... 6 more 

Perhapse東西不初始化。 但是我在印跡執行時創建一個新的基礎服務。 有人可以幫我嗎?

謝謝!

回答

0

我有一個類似的問題,使用Drools和JMH作爲陰影罐。 Drools使用ServiceRegistry的方法。這意味着Drools庫(drools-compiler,kie-ci,drools-decisiontables等)包含一個相同的命名屬性文件,它指出了它提供的接口的實現。

陰影的jar插件通常將(傳遞)依賴關係變爲一個jar。對於多次存在的文件,這通常意味着如果沒有特別指定,則選擇其中一個文件。對於ServiceRegistry屬性,我們需要組合所有文件。通常這是通過ServicesResourceTransformer完成的。此變壓器處理META-INF/services/中的文件,但Drools的相關文件爲META-INF/kie.conf。我與江鈴控股問題可能與AppendingTransformer解決:

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 
    <resource>META-INF/kie.conf</resource> 
</transformer> 

我不是風暴的專家,但Starter表明,它也使用陰影插件。我假設你從IDE運行你的本地集羣 - 它不使用陰影的jar。

+0

謝謝,我用maven-dependency-plugin解決了這個問題。但是我的方法非常複雜,我應該把依賴jar放到風暴裏。我試試你的方法,它也解決了我的問題。 – zhangbo

+0

然後請接受它作爲解決方案 – Dennis