javascript
Spring集成:轻量级集成方法
當(dāng)今的應(yīng)用程序希望能夠訪問(wèn)企業(yè)環(huán)境中的所有業(yè)務(wù),而無(wú)需考慮與絕望的系統(tǒng)無(wú)縫集成的應(yīng)用程序技術(shù)。 可以通過(guò)使用中間件技術(shù)對(duì)各種系統(tǒng)進(jìn)行布線來(lái)實(shí)現(xiàn)這種集成。
集成平臺(tái)使應(yīng)用程序可以相互共享信息的環(huán)境,從而使體系結(jié)構(gòu)具有高度的互操作性。 Spring Integration提供了一個(gè)中介框架,以使用路由和中介構(gòu)建輕量級(jí)集成解決方案,而與協(xié)議無(wú)關(guān)。 Spring Integration是輕量級(jí)的路由和中介框架,不需要笨重的ESB容器即可部署。
Spring Integration使用Message對(duì)象來(lái)通信,路由和傳遞數(shù)據(jù),這沒(méi)什么,但是Java對(duì)象上的包裝器由有效負(fù)載和標(biāo)頭組成。 有效載荷包含的數(shù)據(jù)可以是任何類型,例如文件,字符串,流等,而標(biāo)頭包含有關(guān)消息的通用信息,例如ID,時(shí)間戳等。
消息通過(guò)通道與生產(chǎn)者進(jìn)行通信,該生產(chǎn)者將源與目標(biāo)分離,并將消息發(fā)布到任何協(xié)議,例如JMS,HTTP,Ldap,文件等。生產(chǎn)者將消息發(fā)送到通道,而使用者則從通道接收消息
簡(jiǎn)單集成應(yīng)用
下例顯示了生產(chǎn)者如何將雇員對(duì)象發(fā)送到渠道,發(fā)布者如何從渠道接收消息。
- 下載源代碼
Spring Dependency Maven配置
為了開(kāi)始簡(jiǎn)單的集成示例,我們只需要將核心spring集成和spring上下文依賴項(xiàng)添加到maven pom.xml中。 此外,我們還需要Junit和spring測(cè)試才能進(jìn)行單元測(cè)試
<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配置中配置通道和網(wǎng)關(guān)以發(fā)送和接收消息
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>在這里,我們創(chuàng)建了請(qǐng)求網(wǎng)關(guān)以將消息發(fā)送到通道,并創(chuàng)建出站適配器以從sendRequest通道接收消息。 Spring Integration的網(wǎng)關(guān)是發(fā)送消息的入口點(diǎn),該消息使用Java接口指定。 Spring Integration在運(yùn)行時(shí)自動(dòng)定義代理實(shí)現(xiàn)
請(qǐng)求和接收
下面我們創(chuàng)建SentRequest和ReceiveResponse類,以發(fā)送和接收消息,如下所示
SentRequest.java
package org.springsample.integration; import org.springframework.integration.annotation.Gateway; public interface SentRequest {@Gateway(requestChannel="sendRequest")public void process(Employee emp);}@Gateway批注將指示發(fā)送消息的入口點(diǎn)
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將指示它將通過(guò)適配器從通道接收消息。
以下是雇員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} }測(cè)試中
我們可以使用spring測(cè)試框架進(jìn)行測(cè)試,如下所示
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,并且應(yīng)該在類路徑中
在運(yùn)行SpringIntegTest時(shí),它將顯示控制臺(tái)消息,如下所示
收到消息
名字:John / n電話:12345678 / n地址:Sunny Street Mac RG1
- 下載源代碼
摘要
Spring Integration是開(kāi)放源代碼的簡(jiǎn)單集成,可增強(qiáng)松散耦合并使應(yīng)用程序集成變得容易和簡(jiǎn)單。 它將以可配置的方式在通道和網(wǎng)關(guān)之間集成,路由和中介消息。 本文有助于了解Spring Integration,并將幫助您開(kāi)發(fā)一個(gè)簡(jiǎn)單的集成應(yīng)用程序。
資源資源
- http://static.springsource.org/spring-integration/reference/html/overview.html
翻譯自: https://www.javacodegeeks.com/2013/09/spring-integration-a-lightweight-integration-approach.html
總結(jié)
以上是生活随笔為你收集整理的Spring集成:轻量级集成方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iPhone 15有望推动苹果超过三星
- 下一篇: Servlet和JSP中的文件上传示例