javascript
Spring集成:轻量级集成方法
當今的應用程序希望能夠訪問企業環境中的所有業務,而無需考慮與絕望的系統無縫集成的應用程序技術。 可以通過使用中間件技術對各種系統進行布線來實現這種集成。
集成平臺使應用程序可以相互共享信息的環境,從而使體系結構具有高度的互操作性。 Spring Integration提供了一個中介框架,以使用路由和中介構建輕量級集成解決方案,而與協議無關。 Spring Integration是輕量級的路由和中介框架,不需要笨重的ESB容器即可部署。
Spring Integration使用Message對象來通信,路由和傳遞數據,這沒什么,但是Java對象上的包裝器由有效負載和標頭組成。 有效載荷包含的數據可以是任何類型,例如文件,字符串,流等,而標頭包含有關消息的通用信息,例如ID,時間戳等。
消息通過通道與生產者進行通信,該生產者將源與目標分離,并將消息發布到任何協議,例如JMS,HTTP,Ldap,文件等。生產者將消息發送到通道,而使用者則從通道接收消息
簡單集成應用
下例顯示了生產者如何將雇員對象發送到渠道,發布者如何從渠道接收消息。
- 下載源代碼
Spring Dependency Maven配置
為了開始簡單的集成示例,我們只需要將核心spring集成和spring上下文依賴項添加到maven pom.xml中。 此外,我們還需要Junit和spring測試才能進行單元測試
<properties><spring.framework.version>3.2.3.RELEASE</spring.framework.version><spring.integration.version>2.2.4.RELEASE</spring.integration.version><junit.version>4.11</junit.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.framework.version}</version></dependency><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-core</artifactId><version>${spring.integration.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.framework.version}</version><scope>test</scope></dependency></dependencies> 彈簧配置
我們必須在Spring配置中配置通道和網關以發送和接收消息
SpringIntegTest-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd"><!--sendRequestreceiveRequest--><annotation-config/><context:component-scan base-package="org.springsample.integration"/><gateway id="request" service-interface="org.springsample.integration.SentRequest"/><channel id="sendRequest"/><outbound-channel-adapter channel="sendRequest" ref="receiveResponse" method="processMessage" /> </beans:beans>在這里,我們創建了請求網關以將消息發送到通道,并創建出站適配器以從sendRequest通道接收消息。 Spring Integration的網關是發送消息的入口點,該消息使用Java接口指定。 Spring Integration在運行時自動定義代理實現
請求和接收
下面我們創建SentRequest和ReceiveResponse類,以發送和接收消息,如下所示
SentRequest.java
package org.springsample.integration; import org.springframework.integration.annotation.Gateway; public interface SentRequest {@Gateway(requestChannel="sendRequest")public void process(Employee emp);}@Gateway批注將指示發送消息的入口點
package org.springsample.integration; import org.springframework.integration.Message; import org.springframework.integration.annotation.MessageEndpoint; @MessageEndpoint public class ReceiveResponse { public void processMessage(Message<Employee> message) {Employee employee = message.getPayload();System.out.println("Message Received \n Name :"+employee.getName()+"/n Phone : "+employee.getPhone()+"/n Address :"+employee.getAddress());} }@MessageEndpoint將指示它將通過適配器從通道接收消息。
以下是雇員POJO,但并非不行
package org.springsample.integration; public class Employee {private String name;private String title;private String address;private String phone;public Employee(String name, String phone, String address) {this.name=name;this.phone=phone; this.address=address; //……..Getter amd Setter} }測試中
我們可以使用spring測試框架進行測試,如下所示
package org.springbyexample.integration; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class SpringIntegTest {@Autowiredprivate SentRequest request = null;@Testpublic void testIntegration() {Employee emp = new Employee("John", "12345678", "Sunny Street Mac RG1");request.process(emp);} } 確保在org / springbyexample / integration中保留spring配置文件名稱SpringIntegTest-context,并且應該在類路徑中
在運行SpringIntegTest時,它將顯示控制臺消息,如下所示
收到消息
名字:John / n電話:12345678 / n地址:Sunny Street Mac RG1
- 下載源代碼
摘要
Spring Integration是開放源代碼的簡單集成,可增強松散耦合并使應用程序集成變得容易和簡單。 它將以可配置的方式在通道和網關之間集成,路由和中介消息。 本文有助于了解Spring Integration,并將幫助您開發一個簡單的集成應用程序。
資源資源
- http://static.springsource.org/spring-integration/reference/html/overview.html
翻譯自: https://www.javacodegeeks.com/2013/09/spring-integration-a-lightweight-integration-approach.html
總結
以上是生活随笔為你收集整理的Spring集成:轻量级集成方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone 15有望推动苹果超过三星
- 下一篇: 特斯拉汽车 8 月国内销量 84159