Detach
The detach technique aims to introduce loose coupling between components. Loose coupling will change real-time transactions to near real-time. A similar concept has been traditionally accomplished by applications using messaging bridges between applications. Messaging makes applications loosely coupled by communicating asynchronously. Messaging applications transmit data through a message channel, a virtual pipe that connects a sender to a receiver. A messaging scenario with a single producer and multiple consumers is depicted in Figure 5.

Figure 5. Messaging representation
Even though we don't plan to use a full-fledged messaging infrastructure, we can design components loosely to share data or events each other, in accordance with the principles of a loosely coupled messaging system. In the place of the virtual pipe, we can utilize a shared data or event sink. When we do that, we need to guard against data corruption, since multiple threads might be accessing the shared data structure concurrently. Java uses the synchronized keyword to indicate that a single thread at a time can be executing in this or any other synchronized method of the object representing the monitor. Each Java monitor has a single nameless anonymous condition variable on which a thread can wait(), or signal one waiting thread with notify(), or signal all waiting threads with notifyAll(). This condition variable corresponds to a lock on the object that must be obtained whenever a thread calls a synchronized method in the object. These sequences of steps are better depicted with the help of the diagram in Figure 6.

Figure 6. Threads sharing resource
Reference Architecture
The above discussion points are realized to implement a reference architecture to solve the problem we discussed in the "Getting Started" section. Distribution is realized by horizontally scaling the hardware, whereas we parallelize and detach the components at the software layer using appropriate design primitives. This leads us to the deployment architecture highlighted in Figure 7, to solve the NFR listed in the "Getting Started" section.

Figure 7. Deployment architecture. Click on thumbnail to view full-sized image.
The reference architecture spans across the HTTP application server farm and JRMP business server blocks, shown with a blue tint in Figure 7. We have stubbed out all database interactions to make the architecture deployment simple. The major components and their structural relationship are shown in Figure 8. This will help the reader to better understand how the classes are arranged at code level; sample code is attached in the References section.

Figure 8. RI component relationship. Click on thumbnail to view full-sized image.
Running the RI
Deploying and executing the RI is a straightforward process. First download and unzip the .zip file containing the RI source from the "References" section to a convenient location. It will create a folder called DistributeDetachAndParalleliseInTomcatSrc. This folder will have a build.xml file, and this file depends on an environment variable called CATALINA_HOME on your system, which points to the folder where you have Tomcat unzipped. The build.xml file needs the Ant build tool. To execute the RI, follow the steps below:
- Open a command prompt,
cd DistributeDetachAndParalleliseInTomcatSrc, and enter ant runrmi (Figure 12).
- Copy the web.war file from the DistributeDetachAndParalleliseInTomcatSrc\dist folder to the CATALINA_HOME\webapps folder, and start Tomcat (Figure 11).
- Open another command prompt,
cd DistributeDetachAndParalleliseInTomcatSrc, and enter ant runclient (Figure 9).
- You can repeat step 3 above to have multiple HTTP clients send requests to Tomcat (Figure 10).
- To rebuild the RI,
cd DistributeDetachAndParalleliseInTomcatSrc, and enter ant.
Observe the console windows shown from Figure 9 through Figure 12 to visualize the RI dynamics. The deployment is horizontally scalable by including multiple Tomcat processes behind a load-balancing router, as shown in the deployment diagram.

Figure 9. Simulated Web Server Client 1. Click on thumbnail to view full-sized image.

Figure 10. Simulated Web Server Client 2. Click on thumbnail to view full-sized image.

Figure 11. HTTP application server in Tomcat. Click on thumbnail to view full-sized image.

Figure 12. Java RMI business server. Click on thumbnail to view full-sized image.
Summary
This article showcases how developers can leverage Java language primitives and harness the runtime and hardware resources. A scalable architecture can be deployed in a single node or in multiple nodes as and when the need arises. But for scaled-out deployments, we need to be careful to co-ordinate and synchronize shared resource access. Not all applications can be designed in a detached fashion, but wherever possible, this design proves to be a strong alternative to synchronous, real-time processing. Understanding the requirements and planning for a scalable architecture is an indispensable step for the success of scalable deployments, and this article shows how we analyzed and designed for the various pain points one by one.
References
Acknowledgments
I would like to express my gratitude to my technical advisor Bob Rudi, with whom I worked on architecting and designing the solution. Special thanks go to Duane Gearhart and Birenjith Sasidharan for their support in optimizing and performance tuning the application.