Thursday, March 7, 2013

Spring Integration – Application from scratch, Part 1

Before we start

In this tutorial you will learn what is Spring Integration, how to use it and what kind of problems does it help to solve. We will build a sample application from the scratch and demonstrate some of the core components of Spring Integration. If you’re new to Spring check out another tutorial on Spring written by me – Shall we do some Spring together? Also note that you don’t need any special tooling, however you can get the best experience for building Spring Integration applications either with IntelliJ IDEA or Spring Tool Suite (you can get some fancy-looking diagrams with STS). You can either follow this tutorial step-by-step and create application from the scratch yourself, or you can go ahead and get the code from github:
 
DOWNLOAD SOURCES HERE: https://github.com/vrto/spring-integration-invoices

Whichever way you prefer, it’s time to get started!

Application for processing invoices – functional description

Imagine that you’re working for some company that periodically receives a large amount of invoices from various contractors. We are about to build a system that will be able to receive invoices, filter out relevant ones, create payments (either local or foreign) and send them to some banking service. Even though the system will be rather naive and certainly not enterprise-ready, we will try to build it with good scalability, flexibility and decoupled design in the mind.

Before you go on, you must realize one thing: Spring Integration is (not only, but mostly) about messaging. Spring Integration is basically an embedded enterprise service bus that lets you seamlessly connect your business logic to the messaging channels. Messages can be handled both programmatically (via Spring Integration API) or automatically (by framework itself – higher level of decoupling). Message is the thing that travels across channels. Message has headers and payload – which will be in our case the actual relevant content (domain classes). Let’s take a look at the following picture which is a summary of the system and walk over important pieces:

Invoices Integration Schema

On the picture you can see an integration diagram that illustrates our messaging structure and core components of the system – they are marked with red numbers. Let’s walk over those (we will get back to each component in more detail later):

  1. Invoices Gateway – this is the place where we will put new invoices so they can enter the messaging layer
  2. Splitter – the system is designed to accept a collection of invoices, but we will need to process each invoice individually. More specifically, message with payload of Collection type will be split to the multiple messages, where each message will have individual invoice as a payload.
  3. Filter – Our system is designed to automatically process only those invoices that issue less than $10,000
  4. Router – Some invoices use IBAN account numbers and we have two different accounts – one for the local transactions and one for the foreign transactions. The job of a router component is to send a message carrying invoice to the correct channel – either for local invoices, or for the foreign invoices.
  5. Transformers – While we accept Invoices in to the system, our banking APIs work with other types – Payments. Job of the transformer component is to take some message and transform it to another message according to provided logic. We want to transform the payload of original message (invoice) to the new payload – payment.
  6. Banking Service Activator – After we have processed invoices and generated some actual payments we’re ready to talk to the external banking system. We have exposed service of such systems and when message carrying payment enters the correct (banking) channel, we want to activate some logic – passing the payment to the bank and let the bank do further processing.

Creating the project

By now you should have a high level overview of what the system does and how is it structured. Before we start coding you will need an actual Maven project and set up the structure and required dependencies. If you’re familiar with Maven then see pom.xml file below, else if you want to save some time you’re welcome to use a project template I’ve created for you: download the Maven project template.

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0'         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'         xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'>    <modelVersion>4.0.0</modelVersion>    <groupId>spring-integration-invoices</groupId>    <artifactId>spring-integration-invoices</artifactId>    <version>1.0-SNAPSHOT</version>    <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>3.2.1.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework.integration</groupId>            <artifactId>spring-integration-core</artifactId>            <version>2.2.1.RELEASE</version>        </dependency>        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.16</version>        </dependency>        <dependency>            <groupId>com.google.guava</groupId>            <artifactId>guava</artifactId>            <version>13.0.1</version>        </dependency>        <dependency>            <groupId>org.testng</groupId>            <artifactId>testng</artifactId>            <version>6.5.2</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                </configuration>            </plugin>        </plugins>    </build></project>

Let’s now walk over the six major components of the system in more details and get hands on the actual code.

1. Invoices Gateway

First, let’s see the code for Invoice – which will be one of the core classes in our system. I will be using package com.vrtoonjava as root package, and invoices and banking as sub-packages:

package com.vrtoonjava.invoices;import com.google.common.base.Objects;import java.math.BigDecimal;public class Invoice {    private final String iban;    private final String address;    private final String account;    private final BigDecimal dollars;    public Invoice(String iban, String address, String account, BigDecimal dollars) {        this.iban = iban;        this.address = address;        this.account = account;        this.dollars = dollars;    }    public boolean isForeign() {        return null != iban && !iban.isEmpty();    }    public String getAddress() {        return address;    }    public String getAccount() {        return account;    }    public BigDecimal getDollars() {        return dollars;    }    public String getIban() {        return iban;    }    @Override    public String toString() {        return Objects.toStringHelper(this)                .add('iban', iban)                .add('address', address)                .add('account', account)                .add('dollars', dollars)                .toString();    }}

Imagine that we’re getting invoices from an another system (be it database, web-service or something else), but we don’t want to couple this part to the integration layer. We will use Gateway component for that purpose. Gateway introduces a contract that decouples client code from the integration layer (Spring Integration dependencies in our case). Let’s see the code for InvoiceCollectorGateway:

package com.vrtoonjava.invoices;import java.util.Collection;/** * Defines a contract that decouples client from the Spring Integration framework. */public interface InvoiceCollectorGateway {    void collectInvoices(Collection<Invoice> invoices);}

Now, to actually use the Spring Integration we need to create a standard Spring configuration file and use Spring Integration namespace. To get started, here’s invoices-int-schema.xml file. Put it into src/main/resources. Note that we’ve already defined a logging-channel-adapter – which is a special channel where we will send messages from the logger. We’re also using wire-tap – you can think of it as sort of global interceptor that will send logging-related messages to the logger channel.

<?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'       xsi:schemaLocation = 'http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd'>    <!-- intercept and log every message -->    <int:logging-channel-adapter  level='DEBUG' />    <int:wire-tap channel = 'logger' /></beans>

Let’s get back to our gateway now. We’ve defined a gateway interface – that is the dependency that client will use. When client calls collectInvoices method, gateway will send a new message (containing List payload) to the newInvoicesChannel channel. That leaves client decoupled from the messaging facilities, but lets us place the result to the real messaging channel. To configure gateway, add following code to the integration schema config:

<int:channel  /><int:gateway      service-interface='com.vrtoonjava.invoices.InvoiceCollectorGateway'>    <int:method name='collectInvoices' request-channel='newInvoicesChannel' /></int:gateway>

2. Invoices Splitter

From the Gateway we’re sending one big message to the system that contains a collection of invoices – in other words – Message has payload of Collection type. As we want to process invoices individually, we will get the result from the newInvoicesChannel and use a splitter component, that will create multiple messages. Each of these new messages will have a payload of Invoice type. We will then place messages to the new channel – singleInvoicesChannel. We will use a default Splitter that Spring Integration provides (by default Spring Integration uses DefaultMessageSplitter that does exactly what we want). This is how we define splitter:

<int:splitter        input-channel='newInvoicesChannel'        output-channel='singleInvoicesChannel' /><int:channel  />

3. Filtering some invoices

A business use case of our system requires us to automatically process only those invoices that that issue us less than $10,000. For this purpose we will introduce a Filter component. We will grab messages from the singleInvoicesChannel, apply our filtering logic on them, and then write matched results to the new filteredInvoicesChannel channel. First, let’s create a standard Java class that will contain filtering logic for single invoice. Note that we use @Component annotation (which makes it a standard Spring bean) and we annotate filtering method with @Filter annotation – that will tell Spring Integration to use this method for filtering logic:

package com.vrtoonjava.invoices;import org.springframework.integration.annotation.Filter;import org.springframework.stereotype.Component;@Componentpublic class InvoiceFilter {    public static final int LOW_ENOUGH_THRESHOLD = 10_000;    @Filter    public boolean accept(Invoice invoice) {        boolean lowEnough =                 invoice.getDollars().intValue() < LOW_ENOUGH_THRESHOLD;        System.out.println('Amount of $' + invoice.getDollars()                + (lowEnough ? ' can' : ' can not')                 + ' be automatically processed by system');        return lowEnough;    }}

Note that this is a standard POJO that we can easily unit test it! As I’ve said before – Spring Integration doesn’t tightly couple us to its messaging facilities. For the sake of brevity, I am not pasting unit tests in this tutorial – but if you’re interested go ahead and download github project and see the tests for yourself.

Let’s specify input/output channels for the messaging layer and hook the filter in. Add the following code to the integration schema config:

<int:filter    input-channel='singleInvoicesChannel'    output-channel='filteredInvoicesChannel'    ref='invoiceFilter' /><int:channel  />

4. Routing invoices

So far, we’ve splitted and filtered out some invoices. Now it’s time to inspect contents of the each invoice more closely and decide, whether it is an invoice issued from the current country (local), or from an another country (foreign). In order to do that we can approach as before and use custom class for routing logic. We will (for the sake of demonstration purposes) take the other approach now – we will put Spring Expression Language (SpEL) to use and handle routing completely declaratively. Remember isForeign method on Invoice class? We can directly invoke it with SpEL in router declaration (by using selector-expression attribute)! Router will take a look on the payload, evaluate whether it’s a foreign or a local invoice and forward it to the corresponding channel:

<int:recipient-list-router input-channel='filteredInvoicesChannel'>    <int:recipient channel = 'foreignTransactions' selector-expression='payload.foreign' />    <int:recipient channel = 'localTransactions' selector-expression='!payload.foreign' /></int:recipient-list-router><int:channel  /><int:channel  />

We will continue developing this application in the second part of this tutorial.
 

Reference: Spring Integration – Application from scratch, Part 1 from our JCG partner Michal Vrtiak at the vrtoonjava blog.


Source : feedproxy[dot]google[dot]com

No comments:

Post a Comment