Tuesday, January 22, 2008

Drools and Spring Integration

I've posted a donated drools-spring module to the dev mailing list for feedback on Drools integration with Spring. I've had no feedback yet, so I thought I'd open it up to a wider audience. I'm also hoping this work can be replicated into a drools-guice module, so we can start to have out of the box integration for the various containers out there, all working in a similar manner, possibly with standardised annotations.

You can also read about the JBoss MicroContainer integration work for Drools here:
http://www.jboss.org/index.html?module=bb&op=viewtopic&t=117607

Here is a copy of the message sent to the dev mailing list:
You can declare the transaction beans as follows:
<bean id="droolsTransactionManager"
class="org.drools.spring.core.DroolsTransactionManager">
<property name="workingMemory" ref="workingMemory"/>
</bean>

<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionManager" ref="droolsTransactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="newStatefullSession*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

The last one is only a proxy for the transaction, to declare the pointcuts.
I think the classes for aspects in Ales implementation can be
implemented this way for spring, if not it will be needed to look at :
http://static.springframework.org/spring/docs/2.5.x/reference/aop.html
but I need time for that.

The DroolsTransactionManager is for standalone use.

It was added rule base configuration support for the bean factory of
Geoffrey as well to set the type.

Here are information about getting Resources like URL, input stream, file...
http://static.springframework.org/spring/docs/2.5.x/reference/resources.html

attachment http://cache.gmane.org//gmane/comp/java/drools/devel/2388-001.bin

original post:
http://article.gmane.org/gmane.comp.java.drools.devel/2388

10 comments:

  1. I don't think that DroolsTransactionManager is a good name.

    A TransactionManager is Spring is a local or JTA implementation that can begin, commit and rollback a transaction.
    It's usually combined a with EntityManagerFactory(JPA,Hibernate), JdbcTemplate (JDBC) or anything else that can be rolled back.

    ReplyDelete
  2. Could you please clean up some of the language from the original post? I'm both a Drools and Spring user and would appreciate it if I could understand what it is the original author was trying to get across.

    Secondly, the link that you point to (the .bin file), what exactly should I do with that? Where can I find your DroolsTransactionManager implementation.

    Keep up the great work.

    ReplyDelete
  3. Sorry the blog entry was created by the anonymous donator. I know nothing about Spring, so I was just putting it out there, as is. the .bin is a zip file. If you go to the linked mailing list archive posting at gmane you'll see that more clearly.

    If someone wants to let me know what needs to be corrected, leave notes here (anonymous is fine) and I'll update.

    ReplyDelete
  4. From Paul Browne on the dev mailing list:

    I'll bite, although most likely I'm missing something obvious here

    Code sample itself is clear enough (and builds cleanly with maven), and I *think* I can see the intention from the various Spring aspects and config files (although I'm not sure of the purpose of the aspects. .... yet!)

    Looking at the example (DroolsTest.java) what I see is the Drools classes being loaded pretty much as normal (no Spring)

    What I expected to see in the sample was:
    1) Test gets a handle to the Spring config file
    2) Spring auto-configuring the beans based on the contents of this file..
    3) A call (via a bean that we've obtained from the Spring Context) which causes the Rule engine to fire
    4) A 2nd config file showing how to configure Spring transactions / proxy etc

    What can I do to help? Given that this isn't in SVN yet , what's the best way to manage the code?

    Paul

    ReplyDelete
  5. Thank you for the clarification :)

    ReplyDelete
  6. Nice stuff.

    As far as Spring integration I use a simple way to expose the RuleAgent to an app:

    -- In applicationContext.xml:

    <bean id="ruleAgent" class="org.drools.agent.RuleAgent" factory-method="newRuleAgent" lazy-init="true">
    <constructor-arg>
    <props>
    <prop key="newInstance">true</prop>
    <prop key="url">YOUR_PACKAGE_URLS_HERE</prop>
    <prop key="localCacheDir">YOUR_LOCAL_CACHE_DIR_HERE</prop>
    <prop key="poll">30</prop>
    <prop key="name">YOUR_NAME_HERE</prop>
    </props>
    </constructor-arg>
    </bean>

    <bean id="ruleBase" factory-bean="ruleAgent" factory-method="getRuleBase" scope="prototype" lazy-init="true"/>



    -- Then in your code you can simply place:

    @Autowired
    @Qualifier("ruleBase")
    private RuleBase ruleBase;

    and go from there

    ReplyDelete
  7. So what's the latest on Drools-Spring integration? I am a Drools newbie and planning to use it in our project that's already using Spring. "Spring Modules" supports integration but only via JSR-94 API which I suppose doesn't allow using all the features of Drools. Besides the project has been deprecated by the Spring team.
    In hope of some direction...

    ReplyDelete
  8. Haven't had a chance to finish it off. I'll try and do it for the 5.1 release end of this month, beginning of next month.

    ReplyDelete
  9. hi All(specially Mark),
    this is a good idea that we trying something to integrate Drool and Spring together that the need of the hour i m an average geek in drools as well as in spring and was using both together as specified by tsurdilovic (tough name can't pronounce) in previous post i would like to know more about the same and would be more intersted if i can contribute something..........

    Mark,
    u said that u'll try to finish it in 5.1 release i m already wid 5.1.M1 can u just gimme some samples or test cases link (if any).


    thanks and regards,
    KiranP
    phadatarekiran@gmail.com

    ReplyDelete
  10. Kiran,

    See http://bit.ly/aliqXh for details of the release. Note section 2.1.1

    ReplyDelete