如何使用drools和Spring整合详解编程语言

如何使用drools和Spring整合详解编程语言


一、安装插件

grails install-plugin drools-gorm


二、引入相关的依赖jar包

加入drools-spring-5.2.0.Final.jar,drools-grid-impl-5.2.0.Final.jar


三、程序源码实例


1.conf/spring/resources.xml配置文件

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans”

       xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

       xsi:schemaLocation=”

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”>

    <import resource=”drools.xml” />

</beans>


2.drools.xml配置文件

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans”

       xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

       xmlns:drools=”http://drools.org/schema/drools-spring”

       xsi:schemaLocation=”

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd”>

    <drools:kbase id=”kBaseHelloWorld”>

        <drools:resources>

            <!–不是<drools:resource type=”DRL” source=”classpath:com/jsptpd/rjy/zyj/service/Login.drl”/> –>

            <drools:resource type=”DRL” source=”classpath:drools/HelloWorld.drl”/>

        </drools:resources>

    </drools:kbase>

    <!–不能用这个,这样是单例,并发有问题–>

    <!–<drools:ksession id=”kSessionHelloWorld” type=”stateful” kbase=”kBaseHelloWorld”/>–>

    <bean id=”droolsSpringDemo” class=”com.drools.demo.spring.DroolsSpringDemo”>

        <property name=”kBaseHelloWorld” ref=”kBaseHelloWorld” />

    </bean>

</beans>


3.DroolsSpringDemo java代码实例

package com.drools.demo.spring

import com.drools.demo.main.Message

import org.drools.runtime.StatefulKnowledgeSession

class DroolsSpringDemo {

    def kBaseHelloWorld

    def helloWorld(){

        new Thread(new Runnable() {

            void run() {

                while (true){

                    Thread.sleep(100)

                    run()

                }

            }

        }) .start()

        new Thread(new Runnable() {

            void run() {

                while (true){

                    Thread.sleep(76)

                    run()

                }

            }

        }) .start()

        new Thread(new Runnable() {

            void run() {

                while (true){

                    Thread.sleep(43)

                    run()

                }

            }

        }) .start()

        while (true);

    }

    private run() {

        StatefulKnowledgeSession kstateful = kBaseHelloWorld.newStatefulKnowledgeSession()

        Message message = new Message();

        message.setMessage(“Hello World”);

        message.setStatus(Message.HELLO);

        kstateful.insert(message);

        kstateful.fireAllRules();

        kstateful.dispose();

    }

}


4.测试实例

class DemoDroolsSpringTest extends GroovyTestCase{

    def droolsSpringDemo

    public void testSomething() {

        droolsSpringDemo.helloWorld()

    }

}

如何使用drools和Spring整合详解编程语言

转载请注明来源网站:blog.ytso.com谢谢!

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14629.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论