Spring provides translation of specific technology exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception. These exceptions wrap the original exception so there is no risk of loosing any information about the actual exception which caused this exception.
This allows handling most persistence exceptions, which are not recoverable, only in the appropriate layers, without annoying boilerplate, catch and throw blocks and exception declarations.
Figure 6-2. DataAccessException Hierarchy dipicting first set of classes
Figure 6-3. DataAccessException hierarchy dipicting second set of classes
Java Messaging Service (JMS) opened the gate for sculpting the asynchronous mode of communication in the Java world. It provides a collective way of sending and receiving messages by having a middle man, also called as Message Broker or Message oriented middleware. Now with the capability of an asynchronous framework like JMS being integrated with Spring, it can take all the benefits of Spring.
Some of the advantages of this integration style are:
- Asynchronous nature
Some of the disadvantages associated with this integration style are:
- Complex in nature
- Loss of transaction and security contexts
Figure 6-4. Messaging Integration style
Spring framework provides extensive support for this integration style. The support by Spring for this integration style is as below:
- Spring JMS
- Spring AMQP
- Spring Web Services
- Spring Integration
These will be explained to some extent in the following sections. Minute details of messaging using Spring will be explained in Chapter 16.
Spring provides a JMS integration framework that simplifies the use of the JMS API and shields the user from differences between various JMS API versions available. JMS can be roughly divided into two areas of functionality, namely the production and consumption of messages.
The package org.springframework.jms provides the core functionality for using JMS. It contains JMS template classes that simplify the use of the JMS by handling the creation and release of resources transparently.
The Spring org.springframework.jms.core.JmsTemplate is a convenience class for sending and receiving JMS messages. The JmsTemplate was originally designed to be used with a J2EE container where the container provides the necessary pooling of the JMS resources like connections, consumers and producers. If you want to use JmsTemplate outside of J2EE containers, and because some JMS providers do not offer caching/pooling of JMS resources, a different solution was brought in namely CachingConnectionFactory, which will discussed in the next section in some detail. JmsTemplate object needs to be aware of the connection factory reference through which it will contact the Message Broker for establishing connection and session. Listing 6-1 shows configuring JmsTemplate using ActiveMQ.
Listing 6-1 Configuring the JmsTemplate using XML configuration.
<property name=" connectionFactory" ref="amqConnectionFactory" />
Using JmsTemplate in your Java code you can do common JMS functionalities in an easy and concise manner. Listing 6-3 uses JmsTemplate to send messages and Listing 6-4 uses JmsTemplate to receive messages.
Page Visitors: 2317