Giter Site home page Giter Site logo

garyrussell / gs-integration Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-guides/gs-integration

0.0 1.0 0.0 346 KB

Integrating Data :: Learn how to build an application that uses Spring Integration to fetch data, process it, and write it to a file.

Home Page: https://spring.io/guides/gs/integration/

Shell 46.04% Java 53.96%

gs-integration's Introduction

tags projects
spring-integration

This guide walks you through the process of using Spring Integration to create a simple application that retrieves data from an RSS Feed (Spring Blog), manipulates the data, and then writes it to a file. This guide uses traditional Spring Integration XML configuration; other guides exist showing the use of JavaConfig/DSL with and without JDK 8 Lambda expressions.

What you’ll build

You’ll create a flow using Spring Integration.

Define an integration flow

For this guide’s sample application, you will define a Spring Integration flow that reads blog posts from Spring IO’s RSS feed, transforms them into an easily readable String consisting of the post title and the URL for the post, and appends that String to the end of a file /tmp/si/SpringBlog.

To define an integration flow, you simply create a Spring XML configuration with a handful of elements from Spring Integration’s XML namespaces. Specifically, for the desired integration flow, you work with elements from these Spring Integration namespaces: core, feed, and file.

The following XML configuration file defines the integration flow:

src/main/resources/blog/integration.xml

link:complete/src/main/resources/blog/integration.xml[role=include]

As you can see, three integration elements are in play here:

  • <feed:inbound-channel-adapter>. An inbound adapter that retrieves the posts, one per poll. As configured here, it polls every 5 seconds. The posts are placed into a channel named "news" (corresponding with the adapter’s ID).

  • <int:transformer>. Transforms entries (com.rometools.rome.feed.synd.SyndEntry) in the "news" channel, extracting the entry’s title (payload.title) and link (payload.link) and concatenating them into a readable String (adding a newline). The String is then sent to the output channel named "file".

  • <file:outbound-channel-adapter>. An outbound channel adapter that writes content from its channel (here named "file") to a file. Specifically, as configured here, it will append anything in the "file" channel to a file at /tmp/si/SpringBlog.

This simple flow is illustrated like this:

A flow that reads RSS feed entries

Make the application executable

Although it is common to configure a Spring Integration flow within a larger application, perhaps even a web application, there’s no reason that it can’t be defined in a simpler standalone application. That’s what you do next, creating a main class that kicks off the integration flow and also declares a handful of beans to support the integration flow. You also build the application into a standalone executable JAR file. We use Spring Boot’s SpringApplication to create the application context.

src/main/java/blog/Application.java

link:complete/src/main/java/blog/Application.java[role=include]

Run the application

Now you can run the application from the jar:

java -jar build/libs/{project_id}-0.1.0.jar

... app starts up ...

Once the application starts up, it connects to the RSS feed and starts fetching blog posts. The application processes those posts through the integration flow you defined, ultimately appending the post information to a file at /tmp/si/SpringBlog.

After the application has been running for awhile, you should be able to view the file at /tmp/si/SpringBlog to see the data from a handful of posts. On a UNIX-based operating system, you can also choose to tail the file to see the results as they are written:

tail -f /tmp/si/SpringBlog

You should see something like this (the actual news will differ):

Spring Integration Java DSL 1.0 GA Released @ https://spring.io/blog/2014/11/24/spring-integration-java-dsl-1-0-ga-released
This Week in Spring - November 25th, 2014 @ https://spring.io/blog/2014/11/25/this-week-in-spring-november-25th-2014
Spring Integration Java DSL: Line by line tutorial @ https://spring.io/blog/2014/11/25/spring-integration-java-dsl-line-by-line-tutorial
Spring for Apache Hadoop 2.1.0.M2 Released @ https://spring.io/blog/2014/11/14/spring-for-apache-hadoop-2-1-0-m2-released

Summary

Congratulations! You have developed a simple application that uses Spring Integration to fetch blog posts from spring.io, process them, and write them to a file.

gs-integration's People

Contributors

andirdju avatar btalbott avatar cbeams avatar garyrussell avatar gregturn avatar habuma avatar royclarkson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.