Channel adapters are used to connect an application to the messaging system so that it can send and receive messages. Again, the definition written in the book “Enterprise Integration Pattern”, by Gregor Hohpe and Bobby Woolf is as given below:
The adapter acts as a messaging client to the messaging system and invokes applications functions via an application-supplied interface. This way, any application can connect to the messaging system and be integrated with other applications as long as it has a proper Channel Adapter.
Pictorially channel adapter can be represented as shown in Figure 22-31 below.
Figure 22-31. Function of Channel Adapter
Spring Integration Support
Spring Integration provides a number of adapters which shields application components from integration details. External events produce various kinds of messages which feds into the Spring Integration. The adapters according to the message type kicks into action and deals with the messages without caring neither about the message origin nor about destination. Once the message is processed it can trigger another external event to which the processed messages are sent.
Figure 22-32. Spring Integration adapters
The Spring Integration includes support for the following adapters:
- FTP, FTPS and Secured File Transfer Protocol (SFTP)
- User Datagram Protocol (UDP)
- HTTP (Representational State Transfer [REST])
- Web services (SOAP)
- Mail (POP3 or IMAP for receiving, SMTP for sending)
- Java Message Service (JMS)
- Java Management Extensions (JMX)
- Remote Method Invocation (RMI)
- Really Simple Syndication (RSS) feeds
- Extensible Messaging and Presence Protocol (XMPP)
The following sections are aimed at explaining some of the important adapters in some detail. Since these are very extensive topic, I would like to give you information and details which allow you a stepping-stone in actually considering using these adapters in your application. Some adapters, I will only be giving information and you can find more details on these from other books available in market.
Each of the following sub-sections in which Spring’s Integration capability will be covered in detail follows a standard sub-sections as shown below:
- Brief Introduction
- Maven Dependency
- Spring Integration Namespace
- Adapter Configuration
It is one of the simplest approaches to enterprise integration. Spring Integration provides both inbound and outbound channel adapters to support this integration style.
Listing 22-31 below shows maven dependency required for integrating file system using Spring Integration.
Listing 22-31. Maven dependency required
Listing 22-32 below shows configuration of main Spring Integration namespace and file related namespace in the Spring configuration file (file-integration-context.xml).
Listing 22-32. Spring Integration Namespace declaration
<?xml version="1.0" encoding="UTF-8"?>
Listing 22-33 below shows configuration of a basic file adapter in the Spring configuration file (file-integration-context.xml). In this sample, the file inbound channel adapter polls the configured directory every 5 seconds and passes it onto the “inputFileChannel” and then the file output channel adapter reads the files in the channel to the output directory configured.
Listing 22-33. Basic file adpater configuration
<file:inbound-channel-adapter id="inputFiles" channel="inputFileChannel"
<int:poller fixed-rate="5000" max-messages-per-poll="50"/>
<file:outbound-channel-adapter id="outputFiles" channel="inputFileChannel"
There are various atributes in “inbound-channel-adapter” as shown in Table 22-3 using which can be used to change the behavior of this adapter according to your application requirement. I would not be able to explain these attributes in detail (excessively much and out of scope of this book) but think it would be of great help to know that these attributes can be used if need arises in your application.
Table 22-3. The attributes in “inbound-channel-adapter” element
|comparator||Specify a Comparator to be used when ordering Files. If none is provided, the order will be determined by the java.io.File implementation of Comparable.|
|filter||Specify a FileListFilter to be used. By default, an AcceptOnceFileListFilter is used, which ensures files are picked up only once from the directory. You can also apply multiple filters by referencing a CompositeFileListFilter.|
|filter-pattern||Only files matching this ant style path will be picked up by this adapter.|
|filename-regex||Only files matching this regular expression will be picked up by this adapter.|
|scanner||Reference to a custom DirectoryScanner implementation.|
|prevent-duplicates||A boolean flag indicating whether duplicates should be prevented. If a ‘filter’ reference is provided, duplicate prevention will not be enabled by default (the assumption is that the provided filter is sufficient), but setting this to true will enable it. If a ‘filename-pattern’ is provided, duplicate prevention will be enabled by default (preceding the pattern matching), but setting this to false will disable it. If neither ‘filter’ nor ‘filename-pattern’ is provided, duplicate prevention is enabled by default, but setting this to false will disable it. For more detail on the actual duplicate prevention, see the Javadoc for AcceptOnceFileListFilter.|
Spring Integration also provides an outbound gateway file adapter, which will send the file as a payload to the reply channel as soon as written.
Note: In the above Table 22-1, only some attributes have been explained which the adapter to customize it further can use.
Page Visitors: 10803