我之前在博客中写过一种编写独立的Spring Integration应用程序的方法。
Spring Boot使创建此独立应用程序变得更加简单。
简单的流程是轮询USGS服务,以提供有关世界各地地震活动的信息并记录该信息。 使用Spring Integration描述的流程如下:
<int:inbound-channel-adapter channel="quakeinfotrigger.channel" expression="''">
<int:poller fixed-delay="60000"></int:poller>
</int:inbound-channel-adapter>
<int:channel id="quakeinfo.channel">
<int:queue capacity="10"/>
</int:channel>
<int:channel id="quakeinfotrigger.channel"></int:channel>
<int-http:outbound-gateway id="quakerHttpGateway"
request-channel="quakeinfotrigger.channel"
url="http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour"
http-method="GET"
expected-response-type="java.lang.String"
charset="UTF-8"
reply-timeout="5000"
reply-channel="quakeinfo.channel">
</int-http:outbound-gateway>
<int:logging-channel-adapter id="messageLogger" log-full-message="true" channel="quakeinfo.channel" level="ERROR">
<int:poller fixed-delay="5000" ></int:poller>
</int:logging-channel-adapter>
在预引导过程中,编写主程序以启动此流程的方式应遵循以下原则:
package standalone;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/httpgateway.xml");
applicationContext.registerShutdownHook();
}
}
但是,使用Spring-boot,恕我直言,配置更简单:
package standalone;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:httpgateway.xml")
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
并通过此更改以及spring-boot-maven-plugin插件,可以通过以下方式启动应用程序:
mvn spring-boot:run
我有一个非常小手在通过促进变化的插件来启动应用程序,而无需手动首先运行编译步骤解决这个启动脚本。
甚至更好的是,spring-boot-maven-plugin提供了将整个应用程序打包到可执行jar中的工具,该jar在打包阶段会被触发,如遮阳插件所示:
mvn package
可执行的jar运行如下:
java -jar target/si-standalone-sample-1.0-SNAPSHOT.jar
- 可在此github位置获得具有此更改的更新项目– https://github.com/bijukunjummen/si-standalone-sample