Channel Adapter
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:
- Filesystem
- FTP, FTPS and Secured File Transfer Protocol (SFTP)
- User Datagram Protocol (UDP)
- TCP
- HTTP (Representational State Transfer [REST])
- Web services (SOAP)
- Mail (POP3 or IMAP for receiving, SMTP for sending)
- Java Message Service (JMS)
- JDBC
- 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
File System
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
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-file</artifactId> <version>${spring-integration-version}</version> </dependency> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:file="http://www.springframework.org/schema/integration/file" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-2.1.xsd"> … </beans> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<beans …> <int:channel id="inputFileChannel"/> <file:inbound-channel-adapter id="inputFiles" channel="inputFileChannel" directory="file:/sample/input" prevent-duplicates="true"> <int:poller fixed-rate="5000" max-messages-per-poll="50"/> </file:inbound-channel-adapter> <file:outbound-channel-adapter id="outputFiles" channel="inputFileChannel" directory="file:/sample/output"/> </beans> |
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
Attribute Name | Description |
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: 13000


Tomcy John


Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi,
Thanks for this blog and information sharing!
I have one question from Spring Integration. Just thought of checking with you.
I have SimplewebserviceOutBoundGateway. I need to send a document / attachment to the webservice. How will i achieve that?
This is my current gateway configuration:
Please let me know, if you can throw some light in this area.
Thanks in Advance!
Hi,
Thank you for your information sharing.
I have one doubt,could you please suggest me the correct frameworks to address that issue.
My requirement is I would like to get and process files using spring and camel.
Thanks,
Madhu