Monday, March 29, 2010

Executing Asynchronous Actions in a Web Application

Web applications need to provide instant response to end users, however not all requests can be served instantly due to a variety of constraints on infrastructure the application must live with. In an enterprise web applications there could a variety of long running tasks initiated by user request. To handle such task, the request could be queued for later processing and user is presented with a message to check status at a later time.  For some requests a part of necessary processing could be done instantly and remaining could be done at a later time.

Queuing a request requires adding extra infrastructure to web applications. Request could be queued in a single instance of share data structure across the application. Application threads running in background can read the queue data structure and act on queue messages. Rather than writing this framework/infrastructure enterprise class open source libraries could be utilized.  ActiveMQ provides a robust queuing solution and Apache Camel provides processing queued actions. Both libraries can be used in embedded or standalone fashion.

Architecture:

ServletContextListener initialize the ActiveMQ Broker and CamelContext, queues and routes are setup.

Have web action/controller write to ActiveMQ queue.

Write classes/camel routes to read from queue .