You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by deckerego <de...@gmail.com> on 2010/11/15 04:34:24 UTC

Any interest in a Drools-based dynamic router?

I have a number of services I'm weaving together and routes are becoming a
bit cumbersome to maintain. I'd like to build a decision-table based dynamic
router, likely built on Drools so we can have more concise rules based on
tuple patterns.

Would this be of any interest to others out there? I'll likely start
fleshing things out this week and would welcome any feedback, either
proposed design or nifty features.
-- 
View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3265110.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Any interest in a Drools-based dynamic router?

Posted by deckerego <de...@gmail.com>.
To follow up on this subject, I do have this working now with the
5.2.0-SNAPSHOT of drools-camel provided by JBoss. Here's the trick:


My route is specified as:

    
    
    
        routingSlip
    


Where ksession1 is a stateless knowledge session. The "insertMessage" action
for a stateless session will take the org.apache.camel.Message object,
insert it directly into working memory and execute the given rule sets.


The DRL itself is:
rule "User_Read"
when
    $message : Message(headers["PURPOSE"] == "USER", headers["OPERATION"] ==
"READ");
then
    $message.setHeader("routingSlip", "mock:queue:user_read");
end

Note I'm setting the routing slip header directly on the message and not
updating working memory. The header will then be interpreted by the Camel
route as a routing slip and the message delivered to the endpoint(s)
specified.


As a unit test this could be expressed by:
readUsers.expectedMessageCount(1);
Map headers = new HashMap();
headers.put("PURPOSE", "USER");
headers.put("OPERATION", "READ");
template.sendBodyAndHeaders("direct:drools", "Getting a user!", headers);
readUsers.assertIsSatisfied();

All in all, this seems to be a pretty clean solution for content based
routing with Drools.
-- 
View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3291931.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Any interest in a Drools-based dynamic router?

Posted by deckerego <de...@gmail.com>.
Attached:  http://camel.465427.n5.nabble.com/file/n3274023/drools_cbr.tar.bz2
drools_cbr.tar.bz2 

Attached is a working version of what I'm thinking. I sent this to Mark
Proctor over at the Drools project on Thursday as well. The implementation
attached isn't ideal, but it ended up being the cleanest one I could
discern.

The type conversion is pretty straightforward; it was more of enforcing
convention rather than adding any functionality. The type converters, their
unit tests and Camel's META-INF registries are in the tarball.

When it came to actual routing, the dynamicRoute EIP was ill fitted for this
use case, but the recipientList pattern worked well. I resorted to using the
Bean Language to specify the recipientList pattern, although I don't quite
like the aesthetics of it. The Drools-based route ended up being defined as: 

&lt;route>
    &lt;from uri="cbrBroker:queue:router"/>
    &lt;recipientList>
        &lt;method ref="fetchRouter"/>
    &lt;/recipientList>
&lt;/route>
...
&lt;bean id="fetchRouter" class="org.drools.camel.cbr.router.FetchRouter">
    &lt;property name="droolsEndpoint" value="drools:node1/ksession1"/>
&lt;/bean>


It looks ugly to have the router construction outside of the route
definition itself... but I'm not sure how else to do it without endpoint
injection or creating a whole new route pattern.

Let me know what you think... I've been spinning the issue around in my head
for the past couple of days but haven't been able to think of a superior
solution.


Hadrian Zbarcea wrote:
> 
> Can you please post a unit test/example that doesn't necessarily work, but
> shows what you intend to do?
> 
> When the camel-drools component was initially developed there was a
> discussion about where it should be developed. Since both projects as AL2
> licensed, it didn't make much of a difference and the drools community
> preferred to host it there based on the assumption that it will be better
> maintained. I don't think there should be another camel-drools component
> hosted at the ASF, instead we should improve the existing one. The drools
> community is, in my experience very open to that.
> 
> You can create a jira with apache camel (and assign it to me) if you
> prefer and I can follow up and get it integrated in the drools code base.
> 
-- 
View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3274023.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Any interest in a Drools-based dynamic router?

Posted by Hadrian Zbarcea <hz...@gmail.com>.
Hi,

Can you please post a unit test/example that doesn't necessarily work, but shows what you intend to do?

When the camel-drools component was initially developed there was a discussion about where it should be developed. Since both projects as AL2 licensed, it didn't make much of a difference and the drools community preferred to host it there based on the assumption that it will be better maintained. I don't think there should be another camel-drools component hosted at the ASF, instead we should improve the existing one. The drools community is, in my experience very open to that.

You can create a jira with apache camel (and assign it to me) if you prefer and I can follow up and get it integrated in the drools code base.

Cheers,
Hadrian


On Nov 17, 2010, at 4:15 AM, deckerego wrote:

> 
> Just to clarify, here's an ideal route scenario:
> 
> &lt;route>
>    &lt;from uri="broker:queue:router"/>
>    &lt;dynamicRouter>
>        &lt;policy ref="droolsPolicy">
>            &lt;to uri="drools:node1/ksession1"/>
>        &lt;/policy>
>    &lt;/dynamicRouter>
> &lt;/route>
> 
> 
> This would in turn execute a DRL like (using a paraphrased rule): 
> 
> rule "fetchUser"
>    dialect "mvel"
>    when
>        m : Exchange( in.body instanceof FetchUser )
>    then
>        insert( new Route("broker:queue:fetchUser") );
> end
> 
> 
> The current Drools component and Spring integration does a great job of
> bootstrapping a node, but then I would also need to perform transformations
> from [ Camel Exchange -> Inserting Facts ] and [ Execution Results -> Camel
> Routes ]. Those aren't impossible tasks by any stretch of the imagination...
> I think a Camel TypeConverter could possibly do part of the job.
> DroolsProducer is coded correctly since it performs a
> getBody(Command.class), so type conversion from an Exchange to a Command
> would be doable. However, dynamic routers are required to return URI
> String's and that would likely require manual conversion.
> 
> 
> deckerego wrote:
>> 
>> I've built a prototype based on the drools-server example and thought I'd
>> post my results here. No questions at this point per se, but I thought I'd
>> at least send out an update for those interested.
> 
>> 
>> It looks the Drools Camel component produced by JBoss isn't particularly
>> well suited for routing, although it does simplify configuration a bit.
>> Ultimately things boil down to:
>> 
> 
>> Command batchCommand = CommandFactory.newBatchExecution(commands,
>> "ksession1");
>> ExecutionResults results = template.requestBody("direct:rules",
>> batchCommand, ExecutionResultImpl.class);
>> 
> 
>> 
> 
> -- 
> View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3268523.html
> Sent from the Camel Development mailing list archive at Nabble.com.


Re: Any interest in a Drools-based dynamic router?

Posted by deckerego <de...@gmail.com>.
Just to clarify, here's an ideal route scenario:

&lt;route>
    &lt;from uri="broker:queue:router"/>
    &lt;dynamicRouter>
        &lt;policy ref="droolsPolicy">
            &lt;to uri="drools:node1/ksession1"/>
        &lt;/policy>
    &lt;/dynamicRouter>
&lt;/route>


This would in turn execute a DRL like (using a paraphrased rule): 

rule "fetchUser"
    dialect "mvel"
    when
        m : Exchange( in.body instanceof FetchUser )
    then
        insert( new Route("broker:queue:fetchUser") );
end


The current Drools component and Spring integration does a great job of
bootstrapping a node, but then I would also need to perform transformations
from [ Camel Exchange -> Inserting Facts ] and [ Execution Results -> Camel
Routes ]. Those aren't impossible tasks by any stretch of the imagination...
I think a Camel TypeConverter could possibly do part of the job.
DroolsProducer is coded correctly since it performs a
getBody(Command.class), so type conversion from an Exchange to a Command
would be doable. However, dynamic routers are required to return URI
String's and that would likely require manual conversion.


deckerego wrote:
> 
> I've built a prototype based on the drools-server example and thought I'd
> post my results here. No questions at this point per se, but I thought I'd
> at least send out an update for those interested.

> 
> It looks the Drools Camel component produced by JBoss isn't particularly
> well suited for routing, although it does simplify configuration a bit.
> Ultimately things boil down to:
> 

> Command batchCommand = CommandFactory.newBatchExecution(commands,
> "ksession1");
> ExecutionResults results = template.requestBody("direct:rules",
> batchCommand, ExecutionResultImpl.class);
> 

> 

-- 
View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3268523.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Any interest in a Drools-based dynamic router?

Posted by deckerego <de...@gmail.com>.
I've built a prototype based on the drools-server example and thought I'd
post my results here. No questions at this point per se, but I thought I'd
at least send out an update for those interested.


It looks the Drools Camel component produced by JBoss isn't particularly
well suited for routing, although it does simplify configuration a bit.
Ultimately things boil down to:

Command batchCommand = CommandFactory.newBatchExecution(commands,
"ksession1");
ExecutionResults results = template.requestBody("direct:rules",
batchCommand, ExecutionResultImpl.class);


where the route is:

&lt;route>
    &lt;from uri="direct:rules"/>
    &lt;policy ref="droolsPolicy">
        &lt;to uri="drools:node1/ksession1"/>
    &lt;/policy>
&lt;/route>


This somewhat simplifies the command submission process, but doesn't
necessarily help in performing content based routing (assuming I'm not
missing something). You can insert an Exchange as a fact and get a URI back
as an ExecutionResult value, but this would require message transformation
on either end of the Drools component.


I'm still open to contributing whatever I get working back into the
community, no matter if it's just documenting this process or some code
resulting from this effort. Knowing the scope of what the Drools component
will do, I wanted to see what would be the most helpful to submit. I could
either do:

Documentation on how to do routing with the JBoss component
Create a new Drools component that performs routing without the JBoss
component
Create a method of generic transformation that works in conjunction with the
JBoss component


For now I'll walk down the path of the first option, but let me know if
another option would be more helpful.


Thanks!


deckerego wrote:
> 
> I'll prototype out some examples using JBoss' own implementation and see
> if there's anything I can add. Looking at
> http://lucazamador.wordpress.com/2010/07/20/drools-server-configuration-updated/
> it seems they've pretty much got everything figured out tho.
> 
> Claus Ibsen-2 wrote:
>> 
>> JBoss have done some work to integrate Drools with Camel
>> http://blog.athico.com/2010/07/declarative-rest-services-for-drools.html
>> 
>> Maybe take a peak at that as well?
>> 
> 

-- 
View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3267709.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Any interest in a Drools-based dynamic router?

Posted by deckerego <de...@gmail.com>.
Oh man - how could I have missed that?! That seems to fit our use case. If it
can work alongside Guvnor then it should be a good solution. 

I'll prototype out some examples using JBoss' own implementation and see if
there's anything I can add. Looking at
http://lucazamador.wordpress.com/2010/07/20/drools-server-configuration-updated/
it seems they've pretty much got everything figured out tho.

As far as code contributions - it looks like I have the green light to
release my changes under the Apache 2 license. If I can contribute with
tweaks or bug fixes I should be equipped to do so now.

Thanks Claus!


Claus Ibsen-2 wrote:
> 
> JBoss have done some work to integrate Drools with Camel
> http://blog.athico.com/2010/07/declarative-rest-services-for-drools.html
> 
> Maybe take a peak at that as well?
> 
> But yeah we love contributions, but maybe looking at what JBoss have done.
> And if something is missing get engaged with them about it.
> 
> But nevertheless we love contributions at Apache, so a new component
> is also possible. Just that licensing can be an issue where we can host
> the component.
> 

-- 
View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3265800.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Any interest in a Drools-based dynamic router?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

JBoss have done some work to integrate Drools with Camel
http://blog.athico.com/2010/07/declarative-rest-services-for-drools.html

Maybe take a peak at that as well?

But yeah we love contributions, but maybe looking at what JBoss have done.
And if something is missing get engaged with them about it.

But nevertheless we love contributions at Apache, so a new component
is also possible.
Just that licensing can be an issue where we can host the component.



On Mon, Nov 15, 2010 at 4:34 AM, deckerego <de...@gmail.com> wrote:
>
> I have a number of services I'm weaving together and routes are becoming a
> bit cumbersome to maintain. I'd like to build a decision-table based dynamic
> router, likely built on Drools so we can have more concise rules based on
> tuple patterns.
>
> Would this be of any interest to others out there? I'll likely start
> fleshing things out this week and would welcome any feedback, either
> proposed design or nifty features.
> --
> View this message in context: http://camel.465427.n5.nabble.com/Any-interest-in-a-Drools-based-dynamic-router-tp3265110p3265110.html
> Sent from the Camel Development mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/