Custom notifications and Drools

394 views
Skip to first unread message

gmh

unread,
Oct 21, 2014, 2:05:57 PM10/21/14
to drools...@googlegroups.com
Hi,
One of the requirements for our project (rule-based)  is for users to create their own notification for rule execution.
A good use case will be if item a is moved to location b send an email/sms/twitter to manager. 
Ideally the user should be able to select from a drop down list but for now we would like to provide them with email, twitter, texting services as notifications.
I noticed NotificationWorkItemHandler might be something that I can leverage. How do I implement it? It is a service after all. Can we inject a Spring bean into a knowlodge session?
I am currently using kie-drools-wb  (for authoring) and kie-server-service as a deployment container.
Any ideas?
Gordon

Matteo Mortari

unread,
Oct 21, 2014, 4:44:08 PM10/21/14
to drools...@googlegroups.com
Hi Gordon,
I'm another user and I have similar use case as yours; at present we implemented only for email, but the setup was done taking into account possible extension (SMS, etc.) as you mentioned.

The way I setup, being everything on a Java EE container: I have a dedicated part of the Java EE application where I use Apache Camel framework, and we defined routes which can be "started" from within the knowledge session.
For example, have a route for general email, have another route for high importance email.
Then I have an EJB which is basically proxy-ing methods of Camel's ProducerTemplate, and you can one of these methods be called from within the rule, to send it an email content (the body) with recipients and subject (headers) and to a specific route (the generic or the high importance route).

To obtain a reference to this EJB instance from within the session I believe there are two ways:
  1. set it as a global
  2. look it up via JNDI (as a Drools function returning the lookup)
Although I defined the EJB as @Singleton, so I could use the former way, in practice I actually use the latter, this would be preferred in case I will rework it being a @Stateless, like are already some Controllers/"DAO"/etc.

The great thing about Camel framework is that you have Components for virtually anything, and for email (SMTP in this case), twitter, etc. they are on the main trunk.
I personally used so far SMTP Component for sending out emails from Drools, and Twitter for reading tweets into Drools as events.

The thing I like the most, is that Camel "routes" actually provide mechanism to define control flows, throttling, SLAs, etc. in a very neat, clean and maintainable way via its DSL.
I want to stress this point as I found particularly useful to add a "throttler" for outgoing email.
Once a rule loop caused the original setup to send out a bazillion emails, the solution was to implement a throttler so it would allow a max number of emails per minute. Then we trap exception also as desired in the camel route DSL definition. Obviously I could foresee this potential problem in the first place, I just like to point out the solution was pretty easy to implement thanks to the Camel DSL.

I hope this gives some ideas, and possibly something alternative to Spring? =)

Just a drafted snipped below
Hope this helps
Ciao,
MM

function CamelBootstrapInterface getCamel() {
       
return (CamelBootstrapInterface) javax.naming.InitialContext.doLookup("java:global/my-javaee-app/CamelBootstrap");
}

rule
"Alert means email"
no-loop
when
 $a
: Alert()
then
 
Map<String, Object> headers = new HashMap<String, Object>();
 headers
.put("To", "nob...@acme.com");
 headers
.put("From", new javax.mail.internet.InternetAddress("dro...@acme.com", "Drools at Acme Inc."));
 headers
.put("Subject", "ALERT");
 
 
StringBuilder sb = new StringBuilder();
 sb
.append("<html><body><h1>ALERT !!!</h1>");
 sb
.append($a);
 sb
.append("</body></html>");
 
try {
     getCamel
().sendRouteBodyHeaders("direct:smtp", sb.toString(), headers);
 
} catch (javax.naming.NameNotFoundException e) {
     
// uhm...
 
}
end

gmh

unread,
Oct 23, 2014, 10:34:50 AM10/23/14
to drools...@googlegroups.com
Matteo,
Thanks for your input and detailed explanation. I 'd really appeicated.
I have another thought. I am looking at work item handler in jBPM domain specific processes. It looks like there are some work items that are already provided by Drools, like email, ftp etc,.
According to some documentation I have read you can use work items in guided decision table. The reason I am looking at this approach is because we do not want the users to write java code but rather use decision table.
Does anyone have any experience with this? Is this feasible?
Gordon
Reply all
Reply to author
Forward
0 new messages