Thursday, June 28, 2007

Accessing databases in servicemix-drools

ServiceMix provides a Service Engine for Drools, the famous Rules Engine.

People often want to retrieve data from the rules and such data is usually stored in a database. Previously there was no easy way to configure a DataSource and inject it in the rules definitions. This is a small enhancement that I've just written and that will be included in next major release.

So let's say you write your database access code in a simple helper object:
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class DbHelper {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);

public String getSurname(String name) {
String surname = (String) this.jdbcTemplate
"select surname from t_actor where name = ?",
new Object[]{name}, String.class);

Now, let's use it in our rule definition:
import org.apache.servicemix.drools.model.Exchange;
global org.apache.servicemix.drools.model.JbiHelper jbi;
global DbHelper helper;

rule Init
$me : Exchange()
String t = $me.getIn().getContent().getTextContent();
+ helper.getSurname(t)
+ "</surname>");

And the xbean.xml would look like:
<drools:endpoint  service="test:service"
globals="#globals" />
<util:map id="globals">
<entry key="helper" value-ref="helper" />
<bean id="helper" class="DbHelper">
<property name="dataSource">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:myDataBase" />

This will make available the helper bean to the rules while initializing it with a DataSource looked up from JNDI.

Happy hacking !

Friday, June 22, 2007

Loose coupling in JBI

Loose coupling is a feature that can be easily achieved in JBI but which is sometimes not well understood by newbies in the JBI world.

In JBI, services are exposed inside the bus by JBI components when you deploy a given service onto it. These components can be Service Engines (contain business logic) or Binding Components (handling a specific protocol). The distinction is the key for loose coupling: the service itself it decoupled from the protocols used to access it. This is true for all services accessed from inside the JBI bus, be it internal to the bus or external to it.

Let's take an example. You need to write a BPEL process and expose it over SOAP/HTTP, and this process will consume several other services. These services can be inside the JBI bus, accessible via SOAP/HTTP or plain JMS. The important point is that the BPEL process has no knowledge of the protocol and location of these services. How does it work ?

The key is the WSDL. A WSDL can be split into two parts: the abstract and the concrete model. The abstract model contains the interface (Port) definition along with the messages and xml structures definition. The concrete part contains the binding (tying the service to a particular protocol) and service definition (location and binding for a given service). When JBI components talk together, they only use the abstract WSDL definition which does not contain any protocol or location information. The NMR will select a JBI endpoint based on some informations (name of the interface, name of the service) and will send the JBI exchange to the selected endpoint. If the endpoint has been activated by a Service Engine, it means that the service is hosted inside the JBI bus, and not protocol transformation will be performed. However, if the endpoint is activated from a Binding Component, the endpoint will act as a proxy for an external service provider: the binding component will send a request to the service in the protocol it handles to the known location, but the consumer (the BPEL process) has no knowledge of the exact protocol and location: the only things it needs is the contract (the abstract WSDL).

Take a look at ServiceMix and it's User's Guide.

Saturday, June 02, 2007

Father for the fifth time

Welcome to this world, Alexandre! My new son was born on May the 30th, weights 3.8 kg and his height is 52 cm.
Posted by Picasa