Showing posts with label websphere. Show all posts
Showing posts with label websphere. Show all posts

Sunday, September 23, 2012

Anti-pattern - Using ESB or Integration Broker as Application platform

SOA style of architecture is being adopted gradually by the industry. Organizations have started to view their business capabilities as set of well managed Services that are modular and reusable in different business processes. Business is slowly realizing the value of SOA with respect to Reuse, Business-IT alignment, and Agility.

Product vendors have responded swiftly by introducing SOA enabling products that help expedite the adoption process. There are many ESB & Broker products introduced that are expected to introduce loose coupling by placing intermediary that handles integration concerns between business applications.

Some of the commercial products are:
 1. IBM WebSphere Message Broker, Websphere ESB, WebSphere MQ.
 2. TIBCO Businessworks,
 3. Webmethods Broker, Integration Server,
 4. Oracle Service Bus
5. Sonic ESB, etc

Apart from the commercial products there are open source products and lightweight frameworks that can do the role of ESB's for e.g Mule, Service mix, etc.

Often, I have seen many organizations using the ESB or Integration platforms as Application platforms. This means they use them to build business applications that produces and consumes inter-application messages. This is to me an anti-pattern where Integration platforms are loaded with business logic, complex persistence flows and business decisions. It is a kind of bad-smell in the design of the system and usage. I see following dis-advantages in using this model.

1. The purpose of ESB's is to be an integration middleware, de-coupling producer and consumer applications in many ways.
2. ESB's help induce seperation of concern between Integration-logic and Business-logic. All integration logic is isolated in Integration environment, so that if it changes the change is limited to Integration platform and not applications.
3. ESB's are appropriate for connectivity, translations, enrichment's and  validations in the context message processing. When we start putting applications and business logic inside ESB, then it starts smelling and add more complexity to manage.
4. Most commercial vendors provide message flow framework to write mediators. Sequencing of activities within mediators creates a message processing flow. The flow is created to link the source application to the target application. Adding business logic, entity persistence, rules and decisions, controlling application logic in flows will defeat the purpose of seperation of concerns. Hence it increases rigidity and complexity of applications.
5. ESB's are not made to write applications, where applications need complex persistence mechanism, caching support, data-structure support and other application frameworks.
6. Business applications are best written in good object oriented languages, leveraging application frameworks, containers, management tools etc. For e.g. building order processing engines on Broker or ESB is not good idea as it is defeating ESB's purpose, and not leveraging application frameworks.
7. Applications are best written in OO platforms or frameworks like the Java, dotNet, Ruby on Rails etc.. and deployed on Application Servers like Websphere Application Server, JBoss, Tomcat, etc. The advantages of using OO is huge when written in good abstracted manner, resulting in well defined, modular, maintainable, reusable & component based applications.

I have often seen organizations that use Broker platforms to write applications that processes messages but also host business logic, business entity and persistence of business entities. I find this as  anti-pattern. Probable reasons they take this decision are:

1. Immaturity of decision making,
2. Organizations do not spend efforts in evaluation of architectural and platform decisions.
3. Not enough understanding and appreciation of Object oriented platforms.
4. Ivory tower architects making decisions without practical experience on platforms.
5. Not enough experience on Agile, Object oriented principles and processes.
6. Not enough clarity on usage of architectural styles and their fitment.

I would recommend reading following articles to understand fundamentals of ESB style of architecture.

Mark Richards on ESB
Fundamentals of ESB

I do welcome comments on this topic, please write your thoughts..

Tuesday, February 28, 2012

Jbpm and scheduler components.

Jbpm 4.x has inbuilt timer component that could be used for the standalone deployments. But when it come to production deployments on JEE servers like the websphere, weblogic etc, it is NOT prudent to use it as it uses custom threads that do not share container context. Due to this there could be issues related to transaction management, thread-safety, and database locks.
See issue related:
Un-managed threads
Issues with websphere

So there is need to customize the timer service to suite production deployments. One option is to use most popular open source scheduler library/framework Quartz

Alternatively, one could use websphere server provided scheduler service that is available on the jndi, is persistent, thread-safe, transaction-aware and easy to configure. Only problem is portability.

Quartz 1.8.x is most popular ones used. Recent versions 2.x are compatible with Java 1.5 threading mechanisms. But here is the approach to use quartz 1.8.x with Jbpm 4.x, along with Spring 3.x framework.

Assuming you have done spring integration with jbpm. Where ever you need timer functionality you use the custom state activity in jbpm. This activity will use Spring scheduler factory bean to schedule a quartz job with configured time at run time. The activity then goes to a wait state until the trigger happens or the job is canceled. Here is the program sample for using custom scheduler activity in jbpm.

public class CustomScheduler ExternalActivityBehaviour {

private static final long serialVersionUID = 1L;
private Scheduler scheduler;

public void execute(ActivityExecution execution) {
String executionId = execution.getId();

scheduler.scheduleJob(new TimerJob(executionId,timeInSecs,..);
execution.waitForSignal();
}

public void signal(ActivityExecution execution,String signalName,
Map parameters) {
execution.take(signalName);
}
}


This is simple mechanism to introduce custom timer. The drawback is that this needs improvement to provide better semantics and xml domain elements for ease of use. This could be improved by extending current implementation
of timer service and elements to use quartz framework

There is one important change needed in existing quartz 1.8.x framework to allow it to work with Spring framework and use
websphere provided container threads. The steps are:
1. Apply patch on 1.8.x code as given here
2. Configure Spring to use quartz along with websphere workmanager threads.
3. Use global transaction to create jobs
4. Provide non-XA datasource in scheduler factory configurations.

This should help you work with spring + quartz 1.8.x + Websphere 6.1 smoothly.
Here is the sample configuration:

<bean name="engineScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="schedulerName">
<value>MyScheduler</value>
</property>
<property name="dataSource">
<ref bean="dataBaseSource">
</ref>
<property name="nonTransactionalDataSource">
<ref bean="nonXADatasource">
</ref>
<property name="startupDelay">
<value>20</value>
</property>
<property name="waitForJobsToCompleteOnShutdown">
<value>true</value>
</property>
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
<property name="taskExecutor" ref="scheculerTaskExecutor">
<property name="transactionManager" ref="transactionManager">
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.class">org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer">true</prop>
<prop key="org.quartz.jobStore.class">org.springframework.scheduling.quartz.LocalDataSourceJobStore</prop>
<prop key="org.quartz.jobStore.isClustered">true</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.oracle.OracleDelegate</prop>
</props>
</property>
</property>

It is important to give non-XA datasouce as below:

<bean id="nonXADatasource" class="oracle.jdbc.pool.OracleDataSource" method="close">
<property name="connectionCachingEnabled" value="true"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="user" value="username"/>
<property name="password" value="secret">
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
<property name="acquireIncrement" value="1"/>
<property name="connectionCacheProperties"/>
<props merge="default">
<prop key="MinLimit">3</prop>
<prop key="MaxLimit">20</prop>
</props>
</property>
</bean>