Create Spring Integration Projects using STS

丰胤运
2023-12-01

Engineering
Gunnar Hillert
April 09, 2012

Just days ago, SpringSource Tool Suite™ (STS) 2.9.1 was released. Besides many new features, it provides several features that are especially exciting for Spring Integration users. First of all, the latest STS release adds support for Spring Integration 2.1 and improves the visualization capabilities for Spring Integration flows. STS supports now all recently added Spring Integration adapters such as:

Redis

Gemfire

AMQP

Stored Procedure

Also, all existing Spring Integration adapters have been updated to support new visualization elements. Another wonderful addition to Spring Integration users is that SpringSource Tool Suite 2.9.x now ships with templating support for Spring Integration. Thus, when creating a new project using the Spring Template Project Wizard, you can now select between the following 3 new Spring Integration targeted templates:

Spring Integration Project (Standalone) - Simple

Spring Integration Project (Standalone) - File

Spring Integration Project (War)

The Simple template creates a basic Spring Integration project, which runs as a standalone Java application, using only core Spring Integration components. In order to illustrate File polling capabilities, the File template uses additional components to poll file directories as well as to route those files. Lastly, the War template allows users to easily create basic Spring Integration projects that are targeted to run within servlet containers (e.g. Tomcat) as part of a WAR deployment. For illustration purposes the War template uses the Spring Integration Twitter adapter.
Creating a new Spring Integration Project

In order to start a new project using STS Spring Templates, go in the main menu to File, then NEW and then Spring Template Project. The Template Selection Screen will show up next.

[caption id=“attachment_10681” align=“aligncenter” width=“342” caption=“The Template Selection Screen”][/caption]

This screen provides a list of all available Spring Templates, including the 3 previously mentioned new templates for Spring Integration. If you see that little arrow in front of the template name - that means that the actual template has not been downloaded, yet. Once downloaded, the Templates will be cached on your machine and you won’t need to download the template files again, unless you press the Refresh button and a new version of the template is available remotely.

Let’s select the template Spring Integration Project (Standalone) - Simple and press next. If the template was not downloaded, yet, you will see the following popup:

Once you proceed and the template has been downloaded, you will be presented with the Project Settings Screen. There you have to provide the project name and certain properties that make it easy to deploy your newly created project to a Maven repository.

[caption id=“attachment_10687” align=“aligncenter” width=“420” caption=“Project Settings Screen”][/caption]

In fact, besides creating the Eclipse-specific project files, this Template wizard will also generate a project pom.xml file, which will make this project easily usable outside of STS as well (E.g. for building this project as part of your Continuous Integration process with tools such as Jenkins). Thus, besides the project name, you have to provide a groupId, an artifactId, and a version property. Lastly, you also need to define a top-level (base) package under which your relevant source files will be located.

Once you press “Finish”, the project will be created and it should be listed to your left in the STS Package Explorer window. At this point you now have a fully functional Spring Integration project.
Running the Project

Under your declared base package you will find a Main class that will allow you to execute the Spring Integration application. In STS you can execute it by right clicking the Main class and then selecting “Run As” –> Java Application. The application starts up and you will see the following console output:

Console Output

Besides using STS you can also start the project very simply from the command line using Maven. Just go to the project directory and execute:

$ mvn clean package exec:java

This command will clear the target build directory, compile and package the application and execute the application using the Exec Maven Plugin. The application will start up just as it did in STS. Pressing “q” will quit the application.
The Spring Integration Configuration

What this application does, is that it allows your to enter some free form text via the command line, which is then converted to an upper-case representation and printed to the console.

In order to make this happen, Spring Integration is being used. However, if you look at the details of the Main class that bootstraps the Spring application context, you will notice that it has no references to Spring Integration itself. All the main class basically does is to retrieve an instance of the “StringConversionService” interface from the Spring Application Context. This interface, though, is used by a Spring Integration Gateway to capture the entered String and forward it to the request channel.

The project’s application Context XML file is located under META-INF/spring/integration. As you can see, the application consists of a very basic Spring Integration flow. We start with a simple Gateway that uses the previously mentioned StringConversionService interface.

<?xml version="1.0" encoding="UTF-8"?>

<int:gateway id=“gateway”
default-request-timeout=“5000”
default-reply-timeout=“5000”
default-request-channel=“requestChannel”
default-reply-channel=“replyChannel”
service-interface=“org.springintegration.demo.service.StringConversionService”>
<int:method name=“convertToUpperCase”/>
</int:gateway>

<int:service-activator id=“serviceActivator”
input-channel=“requestChannel”
output-channel=“replyChannel”
expression=“payload.toUpperCase()”/>

<int:channel id=“replyChannel”/>
<int:channel id=“requestChannel”/>

There are 2 defined message channels. The first channel will route the Message containing the input string to the service-activator. The service activator itself will use Spring Expression Language (SpEL) to convert the input Message’s payload to uppercase. And via the reply channel the upper-case String is returned to the caller and printed to the command line.

In a real world scenario, the Service Activator would most likely reference another service bean in order to execute some non-trivial business logic, e.g.:


<int:service-activator id=“serviceActivator”
input-channel=“requestChannel”
output-channel=“replyChannel”
ref=“businessService”
method=“myMethodToExecute”/>

Furthermore, please be aware that we are using the Service Activator in this template in order to illustrate the execution of other Java-based business services. If your goal is to purely transform Spring Integration Message Payloads, please also consider using a Transformer, which may be a better fit semantically.

This is a very simple use-case but it provides you with a nice starting point to implement your own Spring Integration components. We expect that these templates will substantially lower the barrier of entry to learn and experience the benefits of using Spring Integration in your own projects. If you have any questions, we would like to hear from you. Please visit the Spring Integration forum at forum.springsource.org for any questions you may have. For more information on Spring Integration in general, please also visit our project website at: http://www.springintegration.org/.
Resources

STS 2.9.1 Release Notes

Spring Integration Project Website

Spring Integration Templates - GitHub Repository

Spring Integration Templates - Issue Tracker

comments powered by Disqus

translate:
翻译:

此屏幕提供所有可用Spring模板的列表,包括前面提到的用于Spring集成的3个新模板。如果你看到模板名称前面的小箭头-这意味着实际的模板还没有下载。下载后,模板将被缓存在您的计算机上,您不需要再次下载模板文件,除非您按下刷新按钮,并且可以远程使用新版本的模板。
让我们选择模板Spring集成项目(独立的)-简单,然后按next。如果尚未下载模板,您将看到以下弹出窗口:
一旦您继续并下载了模板,您将看到“项目设置”屏幕。在这里,您必须提供项目名称和某些属性,以便将新创建的项目轻松部署到Maven存储库中。

 类似资料:

相关阅读

相关文章

相关问答