{{announcement.body}}
{{announcement.title}}

File-Based Integration: Spring Integration vs Ballerina

DZone 's Guide to

File-Based Integration: Spring Integration vs Ballerina

In this article, explore file-based integration and look at Spring integration vs Ballerina.

· Integration Zone ·
Free Resource

Multi-colored files

Introduction

File-based integration is one of the most popular and widely used approaches in data integration. Many software systems work with files — i.e., they either take in files as input, give their results as files, or do both — and working with multiple disparate systems requires connecting those systems together. Newer software systems also provide API-based access to information, and API-based integration is one of the approaches in connecting such systems. Although APIs provide advantages over traditional file-based systems, such as complex data structures and fine-grained security, many legacy software systems, and even modern ones, often use file-based approaches to export and import data. 

Integration requires ensuring that two separate systems understand each other. And, although file structures and data types will likely be different in different software systems, we need an integration mechanism to map, transform, filter, and cleanse data in order for software systems to understand each other. Some of the most popular file formats used in general scenarios are CSV, EDI, JSON, and XML.

Figure 1: A file integration scenario

As shown in Figure 1, the source application exports its information as files to be saved in the file system. The integration logic will connect to this file system, using protocols such as FTP/SFTP, and read in unprocessed files. Here, the file content will be analyzed, and after the required processing is done, it will output its result as files to the target application file system. The target application will be importing these newly created files. 

You might also be interested in:  Best of DZone: It’s Always Spring Time in the Integration Zone

Technologies for Integration

As mentioned earlier, the integration logic needed for our file processing scenario may contain operations such as filtering, enriching, and routing. You will note that these types of operations are applicable to other types of integration scenarios as well. So, if we are to implement similar integrations in multiple occasions, we will find ourselves implementing a lot of similar logic to do so. This leads to developers creating their own library, utility operations, or a framework to extract this common logic and re-use them. The most often re-usable integration logic becomes a set of patterns. 

A well-known set of integration patterns have been formalized in the book “Enterprise Integration Patterns” by Gregor Hohpe and Bobby Woolf. This has become the de-facto standard in enterprise integration, and many programming frameworks have been implemented based on this. 

In this article, we will use the following technologies to implement a file integration scenario and evaluate their features. 

  • Spring Integration: This follows the enterprise integration patterns (EIPs) discussed in Hope and Woolf’s book and implements an integration solution on top of the Spring Framework, a feature-rich solution that has the full support of the Spring Framework.

  • Ballerina: Ballerina is a programming language for network-distributed applications. It contains first-class support for concepts such as services, network endpoints, and a network communications friendly type system. 

Use Case: File Split FTP

Here, we will be re-using a Spring Integration sample for a file integration scenario, which can be found here. Spring Integration has an excellent collection of examples to start your own integration implementation. 

Steps

  • Start reading all text files at “/tmp/in” directory.

  • Read in each file and split its contents to individual messages, which represents a line in the file, and file marker events, which identifies the start and the end of processing all the file data.

  • Route the messages so that data lines are sent to one channel, and the markers are sent to another channel.

  • The line data is sent to an integration flow through a channel, which checks the line header value to find out the file name and write the content to that file.

  • The marker data is sent to another integration flow through a channel, where the marker end message is filtered out, and a publish/subscribe endpoint is used to register multiple subscribers to write each file out to its own dedicated FTP location. Also, another subscriber is used to send an email to signal processing is done. 

Figure 2 : File processing integration flow visualization

(source: https://github.com/spring-projects/spring-integration-samples/tree/master/applications/file-split-ftp)

Implementation

The solution’s design and its code segments are discussed in relation to each technology reviewed.

Spring Integration

Spring Integration implements most of the concepts defined in EIPs. This is built on top of the Spring Framework on adding additional abstractions in fulfilling regularly used integration scenarios. Spring Integration follows a pipe and filter pattern, where each processing element in a flow is connected by a virtual pipe. These are known as message endpoints and message channels, respectively. This ensures there is loose coupling between individual message endpoints. The message endpoints implement operations such as routing, filtering, splitting, and aggregations.

There are many types of channels, where the main ones are direct channels and publish/subscribe channels. In direct channels, two message endpoints are directly connected, and in pub/sub-channels, one message endpoint can send messages to a channel, which can be received by many messaging endpoints. 

Figure 3: Pipes/filters pattern

Spring Integration provides an XML configuration-based approach and also a Java DSL-based approach on a fluent builder pattern. In our implementation, we will be using the Java DSL-based approach. 

We will look at some of the main code snippets used with Spring Integration in implementing this scenario. An integration scenario is represented using an “IntegrationFlow” interface in Spring Integration. One of the ways of creating this would be to use a builder pattern starting with the “IntegrationFlows” class. Here, you can use this to create an input channel adapter in order to get messages into the system and connect them to a message channel. Each message that is created and put through the message channels are represented using the “Message” interface. This will contain a domain-specific “payload” value and a set of headers. For example, a file inbound adapter will have “File” objects as payload values. 

Refer below to view how the files from the source directory is read with a certain pattern. The text content in the files are split to separate lines, and also, with the file line messages, it will also be emitting file marker messages that show the start and the end of processing. 

Java
x
14
 
1
@Bean
2
public IntegrationFlow fromFile() {
3
   return IntegrationFlows.from(
4
           Files.inboundAdapter(new File("/tmp/in"))
5
                   .preventDuplicates(false)
6
                   .patternFilter("*.txt"), e -> e.poller(Pollers.fixedDelay(5000)
7
                   .errorChannel("tfrErrors.input"))
8
                   .id("fileInboundChannelAdapter"))
9
             .handle(Files.splitter(true, true))
10
             .<Object, Class<?>>route(Object::getClass, m -> m
11
                   .channelMapping(FileSplitter.FileMarker.class, "markers.input")
12
                   .channelMapping(String.class, "lines.input"))
13
             .get();
14
}

Listing 1: Spring Integration — Reading file data

In line 03 of Listing 1, we signal to the framework that we are reading files from the directory “/tmp/in”, and also we are configuring it to filter only files that have the “.txt” extension. Then, the actual file reading is done in a polling fashion with a five-second polling interval. Thereafter, the “Files.splitter” creates a message endpoint, which splits the messages into string lines, and emit messages with string payloads, and also create messages with “FileMarker” objects as payloads. From there, another “route” endpoint is used to perform some routing based on the message type we are sending to it. If it receives a “String” value, it sends this to the channel named “lines.input”, or else, if it is a marker object of type “FileMarker”, it sends it to the “markers.input” channel. 

For each bean representing a message endpoint, you can reach its implicit input channel by simply appending “.input” to the bean name. This is how the “markers” and “lines” message endpoints’ input channels are named “markers.input” and “lines.input”, respectively. 

In the next step, let’s create the FileWritingMessageHandler, which is basically an output channel adapter, to write messages to files. 

Java
xxxxxxxxxx
1
 
1
@Bean
2
public FileWritingMessageHandler fileOut() {
3
   return Files.outboundAdapter("'/tmp/out'")
4
           .appendNewLine(true)
5
           .fileNameExpression("payload.substring(1, 4) + '.txt'")
6
           .get();
7
}

Listing 2: Spring Integration — Defining the output file channel adapter

Here, we simply define a file channel adapter, which outputs data lines by deriving the file name by extracting the first few characters in the data line. The following shows the actual message endpoint used to write out the data lines. 

Java
xxxxxxxxxx
1
 
1
@Bean
2
public IntegrationFlow lines(FileWritingMessageHandler fileOut) {
3
   return f -> f.handle(fileOut);
4
}

Listing 3: Spring Integration —  The integration flow to write out data lines to files

In the following integration flow, we wait for the file mark end message, and flush the files that were written data to. Then, each of the files are picked up, and they are sent to their own FTP location. And, finally, an email is sent to notify the processing has been done. 

Java
xxxxxxxxxx
1
46
 
1
@Bean
2
public IntegrationFlow markers() {
3
   return f -> f.<FileSplitter.FileMarker>filter(m -> 
4
          m.getMark().equals(FileSplitter.FileMarker.Mark.END),
5
          e -> e.id("markerFilter"))
6
          .publishSubscribeChannel(s -> s
7
 
          
8
                  // first trigger file flushes
9
              .subscribe(sf -> sf.transform("'/tmp/out/.*\\.txt'", 
10
                      e -> e.id("toTriggerPattern"))
11
                  .trigger("fileOut", e -> e.id("flusher")))
12
 
          
13
                   // send the first file
14
              .subscribe(sf -> sf.<FileSplitter.FileMarker, 
15
                      File>transform(p -> new File("/tmp/out/002.txt"))
16
                  .enrichHeaders(h -> h.header(
17
                      FileHeaders.FILENAME, "002.txt", true))
18
                  .handle(Ftp.outboundAdapter(ftp1())
19
                  .remoteDirectory("foo"), e -> e.id("ftp002")))
20
 
          
21
                   // send the second file
22
               .subscribe(sf -> sf.<FileSplitter.FileMarker, 
23
                    File>transform(p -> new File("/tmp/out/006.txt"))
24
                   .enrichHeaders(h -> h.header(FileHeaders.FILENAME, 
25
                       "006.txt", true))
26
                   .handle(Ftp.outboundAdapter(ftp2()).remoteDirectory("foo"), 
27
                       e -> e.id("ftp006")))
28
 
          
29
                   // send the third file
30
               .subscribe(sf -> sf.<FileSplitter.FileMarker, File>transform(
31
                       p -> new File("/tmp/out/009.txt"))
32
                   .enrichHeaders(h -> h.header(FileHeaders.FILENAME, 
33
                       "009.txt", true))
34
                   .handle(Ftp.outboundAdapter(ftp3()).remoteDirectory("foo"), 
35
                       e -> e.id("ftp009")))
36
 
          
37
                   // send an email
38
               .subscribe(sf -> sf.transform(
39
                       FileSplitter.FileMarker::getFilePath)
40
                   .enrichHeaders(Mail.headers()
41
                   .subject("File successfully split and transferred")
42
                   .from("foo@bar")
43
                   .toFunction(m -> new String[] { "bar@baz" }))
44
                   .enrichHeaders(h -> h.header(EMAIL_SUCCESS_SUFFIX, ".success"))
45
                   .channel("toMail.input")));
46
}

Listing 4: Spring Integration — Flush files, write to FTP, and send email 

In the above integration flow, the message source is the channel that retrieves file marker messages. We simply filter out the messages to only consider marker end messages and process the output files from there. At this point, a pub/sub message channel is created to take in the marker message and send it out to multiple target channels, which will have their own integration flows. These subscribe flows are used to send a file to an FTP location. One of the flows sends out an email to signal completion. The flow that sends the email flow and its initialization of the mail output adapter is shown below.

Java
xxxxxxxxxx
1
12
 
1
@Autowired
2
private MailProperties mailProperties;
3
 
          
4
@Bean
5
public IntegrationFlow toMail() {
6
   return f -> f
7
           .handle(Mail.outboundAdapter(this.mailProperties.getHost())
8
                           .port(this.mailProperties.getPort())
9
                           .credentials(this.mailProperties.getUsername(), 
10
                                        this.mailProperties.getPassword()),
11
                   e -> e.id("mailOut").advice(afterMailAdvice()));
12
}

Listing 5: Spring Integration — Email sending integration flow

Listing 5 shows the configuration of the mail output adapter, and how it can be used to send a mail. 

Solution Analysis

The scenario implementation from Spring Integration is done using the Java DSL, but this can also be done using XML-based configuration. Developers who prefer a code-first approach will be more inclined to select the Java DSL-based approach. This provides a good balance between rolling up your own integration code, and a full graphical-only based integration solution. Spring Integration provides a rich set of functionality to implement most of the EIPs; the Spring Framework also provides additional features, such as Spring Data and Spring Security.

Also, whenever required, we can drill in and create custom integration logic, with a custom message handler (filter/message endpoint), and plug it into an integration flow construct. This gives the flexibility of having custom logic, while retaining the uniformity of following a single framework. 

Ballerina

Ballerina follows a pure code-first approach for integration. The programming language maps the requirements of modern application developments and addresses key aspects such as being network-aware and having resilient communication features. These aspects are built into the language, as language features alongside the standard library, and this makes Ballerina an out-of-the-box solution for performing integration in an intuitive manner. 

Let’s see how we can implement our sample use case using Ballerina. The source code for the below implementation can be found here

Ballerina has a general services concept that is used to represent any incoming messages to a system. Here, we are defining a file listener and a service that binds to this listener, which can be used to monitor a local file system folder. 

C
xxxxxxxxxx
1
14
 
1
listener file:Listener filein = new ({
2
   path: "/tmp/in"
3
});
4
 
          
5
service file_reader on filein {
6
   resource function onCreate(file:FileEvent fe) returns error? {
7
       if (fe.name.endsWith(".txt")) {
8
           io:println("File: ", fe.name);
9
           check processFileData(<@untainted> fe);
10
           check ftpFiles();
11
           check sendDoneEmail();
12
       }
13
   }
14
}

Listing 6: Ballerina Integration — Defining a file listener service

The above setup takes care of listening for file changes in a directory and passing the file information to be processed by the “processFileData”function. Afterwards, the resultant files are FTPed using the “ftpFiles” function, and, finally, a status email is sent using the “sendDoneEmail” function. 

Let’s take a look at how the “processFileData” function is implemented. 

C
xxxxxxxxxx
1
16
 
1
function processFileData(file:FileEvent fe) returns error? {
2
   io:ReadableByteChannel bch = check io:openReadableFile("/tmp/in/" + fe.name);
3
   io:ReadableCharacterChannel cch = new(bch, "UTF-8");
4
   io:ReadableTextRecordChannel rch = new(cch, rs = "\n", fs = "*");
5
   while (rch.hasNext()) {
6
       string[] line = check rch.getNext();
7
       check writeFileLine("/tmp/out/" + line[0], line);
8
   }
9
}
10
 
          
11
function writeFileLine(string path, string[] line) returns error? {
12
   io:WritableByteChannel bch = check io:openWritableFile(path);
13
   io:WritableDataChannel dch = new(bch);
14
   string linex = "".'join(...line);
15
   check dch.writeString(linex, "UTF-8");
16
}

Listing 7: Ballerina Integration —  Input file processing

Here, file data is read from the source represented by the earlier “FileEvent” instance. This file data is split into a record format, where, in each record, the first field is used as the output file name. Each line is sent to its respective file, where this output file location is used as a buffer area, where the resultant data will be written later to a remote FTP location. 

In the next section, we will see how the FTP client endpoint is set up, followed by the logic used in writing our buffered data out to the final targets.

C
xxxxxxxxxx
1
24
 
1
ftp:ClientEndpointConfig ftpConfig = {
2
   protocol: ftp:FTP,
3
   host: config:getAsString("ftp_host"),
4
   port: 20,
5
   secureSocket: {
6
       basicAuth: {
7
           username: config:getAsString("ftp_user"),
8
           password: config:getAsString("ftp_pass")
9
       }
10
   }
11
};
12
 
          
13
ftp:Client ftpClient = new(ftpConfig);
14
 
          
15
function ftpFiles() returns error? {
16
   check ftpFile("/tmp/out/1.txt", "/data/1.txt");
17
   check ftpFile("/tmp/out/2.txt", "/data/2.txt");
18
   check ftpFile("/tmp/out/3.txt", "/data/3.txt");
19
}
20
 
          
21
function ftpFile(string src, string tgt) returns error? {
22
   io:ReadableByteChannel bch = check io:openReadableFile(src);
23
   check ftpClient.append(tgt, bch);
24
}

Listing 08: Ballerina Integration — FTP configuration

Here, we have defined our FTP target as a Ballerina network client. For this, the configuration is first given using an instance of “ftp:ClientEndpointConfig”, which contains the target FTP server-specific information. The Ballerina config API has been used to externalize the properties that are used for this. They can be passed in many ways, including an external configuration file or using environment variables. The “ftpFile” function implements the actual logic to write the source file contents to a target FTP location, where the buffered file content is simply appended to the target location. 

The final step is to send a notification mail stating that a set of files were processed by our integration solution. 

C
xxxxxxxxxx
1
31
 
1
gmail:GmailConfiguration gmailConfig = {
2
   clientConfig: {
3
       auth: {
4
           scheme: http:OAUTH2,
5
           config: {
6
               grantType: http:DIRECT_TOKEN,
7
               config: {
8
                   accessToken: config:getAsString("gmail_at"),
9
                   refreshConfig: {
10
                       refreshUrl: gmail:REFRESH_URL,
11
                       refreshToken: config:getAsString("gmail_rt"),
12
                       clientId: config:getAsString("gmail_clientid"),
13
                       clientSecret: config:getAsString("gmail_clientsecret")
14
                   }
15
               }
16
           }
17
       }
18
   }
19
};
20
 
          
21
gmail:Client gmailClient = new(gmailConfig);
22
 
          
23
function sendDoneEmail() returns error? {
24
   gmail:MessageRequest messageRequest = {};
25
   messageRequest.recipient = "admin@acme.com";
26
   messageRequest.sender = "foo@acme.com";
27
   messageRequest.subject = "File Integration Executed";
28
   messageRequest.messageBody = "File Integration Done with FTP transfers";
29
   messageRequest.contentType = gmail:TEXT_PLAIN;
30
   check gmailClient->sendMessage("foo", messageRequest);
31
}

Listing 9: Ballerina Integration - Email notification

Here, we are using the Gmail connector to send out emails for the notification. The user has to provide the credentials to authenticate herself for sending mail. This is done by creating a Gmail network client with an authentication configuration. Afterward, using the “sendDoneEmail”, an email message is generated and sent out. 

A notable design decision in Ballerina is the use of a sequence diagram model in organizing the code. Its networking constructs and operations are represented as actors and messages passed between them. Also, the concurrency model fits naturally into this model as an intuitive way of visualizing concurrent executions. Code written in Ballerina can be automatically visualized as a sequence diagram without any manual definitions. 

Figures 4 and 5 show the graphical versions of the code in Listings 6 and 9, respectively. 

Figure 4: Ballerina Integration —  Defining a file listener service as a graphical view

Figure 5: Ballerina Integration — An email notification as a graphical view

Solution Analysis

Ballerina’s solution follows a more procedural pattern, compared with Spring Integration’s more declarative mode with its Java DSL. However, Ballerina’s services concept and its network endpoints make the file and other network operations feel more natural, and getting up to speed with its functionality feels more intuitive. Although Ballerina does not directly implement EIPs in the way Spring Integration does, its language features mean that this is not necessarily required to be productive in implementing integration logic. 

The graphical view with the sequence diagram provides the best of both worlds in graphical modeling and code-first programming for integration. It is not meant to be a graphical-first mode for defining your logic, but rather an approach to self-document your code, and have a high-level view of the project that is developed. Ballerina’s principle is that integration and application code has become one — there is no distinction anymore. Any developer will now perform integrations with his or her application code, and integrating with other systems is becoming the actual business logic.

Also, Ballerina’s approach being more closer to the business logic gives developers the flexibility to make tweaks when required. This is not directly possible with a standard framework and curtails our freedom to some extent. 

Conclusion

Both approaches have their own merits; however, if you already have a Spring Framework-based application, you will be compelled to use Spring Integration owing to the large ecosystem and in-built functionality, with strict conformance to EIPs.

Ballerina provides a more novel approach by converging business logic and integration. The language tells us that there is no such thing as writing separate integration code: it has already become a natural part of a developer’s day-to-day coding. Ballerina succeeds in helping to define those operations more intuitively. 

Further Reading

Integration Testing in Spring Boot

Data Integration With Ballerina

Topics:
spring integration ,integration ,microservices ,ballerina

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}