Asynchronous and message-based programming is different from traditional programming because you cannot know when something is going to occur. A method call that adds two numbers is going to return a result. A method call will never decide to maybe return a number or execute some other instructions before returning the number. This might be obvious, but that is how asynchronous and message-based programming works.
In the beginning of this chapter, we showed you the simplest kind of asynchronous programming by using the interface technique. The interface technique is very simple, but the Java libraries created the Observer and Observable interfaces and classes for that specific purpose. The Java classes are acceptable and will solve many problems so long as the problems do not involve threading or generate events across JVM processes.
For a more scalable approach, the threading package exposes a number of Publish and Subscribe classes. These classes use threading and make it possible to concurrently process specific jobs. A good example of using these classes is when you can break down the problem to solve into smaller, similar problems.
The messenger package is a faade of a very complex solution to asynchronous programming. The messenger package makes using JMS not that complicated once the environment is properly set up. Using messaging is very useful when you need to balance the workload. Typically, the problem in this context is not that you can't decompose the problem, but rather that there are too many problem instances. For example, consider a database application that has too many concurrent users and whose load needs to be distributed across multiple machines. Using the workload-balancing best practice, you can easily scale the application.
The Services package is useful for providing a plug-in environment used to execute services. There is no support for threads, which the developer must provide on his own. It is important to consider the Services package; as we will discuss later in the book, administrators in Open Source have been successful because they can configure and tweak the application so that it works optimally.
Overall, you should take away from this chapter that asynchronous programming is sometimes a good way to solve the scalability problem.
On the CD The sources to the concepts presented in this chapter are located under the directory [CDROM]/jseng/src/com.devspace. jseng .asynchronous . For the section Configuring a Messenger Client the sample configuration file is located at [CDROM]/jseng/src/classes/Messenger.xml