javascript
spring rmi_Spring集成–使用RMI通道适配器
spring rmi
1.引言
本文介紹了如何使用Spring Integration RMI通道適配器通過(guò)RMI發(fā)送和接收消息。 它由以下部分組成:
- 實(shí)施服務(wù):第一部分著重于創(chuàng)建和公開(kāi)服務(wù)。
- 實(shí)現(xiàn)客戶端:顯示如何使用MessagingTemplate類調(diào)用服務(wù)。
- 抽象SI邏輯:最后,我添加了另一部分,解釋了如何實(shí)現(xiàn)抽象所有Spring Integration代碼的相同客戶端,而使客戶端專注于其業(yè)務(wù)邏輯。
您可以在github上獲取源代碼。
2.實(shí)施服務(wù)
第一部分非常簡(jiǎn)單。 該服務(wù)是通過(guò)注釋定義的,因此它將通過(guò)組件掃描自動(dòng)檢測(cè)。 它注入了一個(gè)存儲(chǔ)庫(kù),該存儲(chǔ)庫(kù)從嵌入式數(shù)據(jù)庫(kù)獲取數(shù)據(jù),這將在同一部分中顯示:
@Service("defaultEmployeeService") public class EmployeeServiceImpl implements EmployeeService {@Autowiredprivate EmployeeRepository employeeRepository;@Overridepublic Employee retrieveEmployee(int id) {return employeeRepository.getEmployee(id);} }存儲(chǔ)庫(kù)如下:
@Repository public class EmployeeRepositoryImpl implements EmployeeRepository {private JdbcTemplate template;private RowMapper<Employee> rowMapper = new EmployeeRowMapper();private static final String SEARCH = "select * from employees where id = ?";private static final String COLUMN_ID = "id";private static final String COLUMN_NAME = "name";@Autowiredpublic EmployeeRepositoryImpl(DataSource dataSource) {this.template = new JdbcTemplate(dataSource);}public Employee getEmployee(int id) {return template.queryForObject(SEARCH, rowMapper, id);}private class EmployeeRowMapper implements RowMapper<Employee> {public Employee mapRow(ResultSet rs, int i) throws SQLException {Employee employee = new Employee();employee.setId(rs.getInt(COLUMN_ID));employee.setName(rs.getString(COLUMN_NAME));return employee;}} }以下配置通過(guò)RMI公開(kāi)服務(wù):
服務(wù)器配置文件
<context:component-scan base-package="xpadro.spring.integration"/><int-rmi:inbound-gateway request-channel="requestEmployee"/><int:channel id="requestEmployee"/><int:service-activator method="retrieveEmployee" input-channel="requestEmployee" ref="defaultEmployeeService"/><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /> </bean><!-- in-memory database --> <jdbc:embedded-database id="dataSource"><jdbc:script location="classpath:db/schemas/schema.sql" /><jdbc:script location="classpath:db/schemas/data.sql" /> </jdbc:embedded-database>讓我們關(guān)注帶有'int'名稱空間的行:
網(wǎng)關(guān)的功能是將消息傳遞系統(tǒng)的管道與應(yīng)用程序的其余部分分開(kāi)。 這樣,它就被業(yè)務(wù)邏輯隱藏了。 網(wǎng)關(guān)是雙向的,因此您具有:
- 入站網(wǎng)關(guān):將消息帶入應(yīng)用程序并等待響應(yīng)。
- 出站網(wǎng)關(guān):調(diào)用外部系統(tǒng),并將響應(yīng)發(fā)送回應(yīng)用程序。
在此示例中,我們使用RMI入站網(wǎng)關(guān)。 它將通過(guò)RMI接收一條消息并將其發(fā)送到requestEmployee通道,該通道也在此處定義。
最后, 服務(wù)激活器允許您將spring bean連接到消息通道。 在這里,它連接到requestEmployee通道。 消息將到達(dá)通道,服務(wù)激活器將調(diào)用retrieveEmployee方法。 考慮到如果bean只有一個(gè)公共方法或帶有@ServiceActivator注釋的方法,則不需要'method'屬性。
然后,響應(yīng)將發(fā)送到回復(fù)通道。 由于我們沒(méi)有定義此通道,因此它將創(chuàng)建一個(gè)臨時(shí)回復(fù)通道。
3,實(shí)施客戶
我們將要實(shí)現(xiàn)的客戶端將調(diào)用服務(wù)以檢索員工。 為此,它將使用MessagingTemplate類:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:xpadro/spring/integration/test/config/client-config.xml"}) public class TestRmiClient {@AutowiredMessageChannel localChannel;@AutowiredMessagingTemplate template;@Testpublic void retrieveExistingEmployee() {Employee employee = (Employee) template.convertSendAndReceive(localChannel, 2);Assert.assertNotNull(employee);Assert.assertEquals(2, employee.getId());Assert.assertEquals("Bruce Springsteen", employee.getName());} }客戶端使用messagingTemplate將Integer對(duì)象轉(zhuǎn)換為Message并將其發(fā)送到本地通道。 如下所示,有一個(gè)出站網(wǎng)關(guān)連接到本地通道。 該出站網(wǎng)關(guān)將通過(guò)RMI發(fā)送請(qǐng)求消息。
<int-rmi:outbound-gateway request-channel="localChannel" remote-channel="requestEmployee" host="localhost"/><int:channel id="localChannel"/><bean class="org.springframework.integration.core.MessagingTemplate" />4,抽象SI邏輯
在上一節(jié)中,您可能已經(jīng)注意到,訪問(wèn)服務(wù)的客戶端類具有特定于Spring Integration的邏輯及其業(yè)務(wù)代碼:
- 它使用MessagingTemplate,它是一個(gè)SI類。
- 它了解本地通道,該本地通道特定于消息傳遞系統(tǒng)
在本節(jié)中,我將實(shí)現(xiàn)與抽象消息傳遞邏輯相同的示例,因此客戶端將只關(guān)心其業(yè)務(wù)邏輯。
首先,讓我們看一下新客戶端:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:xpadro/spring/integration/test/config/client-gateway-config.xml"}) public class TestRmiGatewayClient {@Autowiredprivate EmployeeService service;@Testpublic void retrieveExistingEmployee() {Employee employee = service.retrieveEmployee(2);Assert.assertNotNull(employee);Assert.assertEquals(2, employee.getId());Assert.assertEquals("Bruce Springsteen", employee.getName());} }現(xiàn)在我們可以看到客戶端僅實(shí)現(xiàn)其業(yè)務(wù)邏輯,而不使用消息通道或消息傳遞模板。 它將僅調(diào)用服務(wù)接口。 所有消息傳遞定義都在配置文件中。
<int-rmi:outbound-gateway request-channel="localChannel" remote-channel="requestEmployee" host="localhost"/><int:channel id="localChannel"/><int:gateway default-request-channel="localChannel" service-interface="xpadro.spring.integration.service.EmployeeService"/>客戶端網(wǎng)關(guān)配置文件
我們?cè)谶@里所做的是添加一個(gè)網(wǎng)關(guān),該網(wǎng)關(guān)將攔截對(duì)服務(wù)接口EmployeeService的調(diào)用。 Spring Integration將使用GatewayProxyFactoryBean類在服務(wù)接口周圍創(chuàng)建代理。 該代理將使用消息傳遞模板將調(diào)用發(fā)送到請(qǐng)求通道并等待響應(yīng)。
5,結(jié)論
我們已經(jīng)看到了如何使用Spring Integration通過(guò)RMI訪問(wèn)服務(wù)。 我們還看到,我們不僅可以使用MessagingTemplate顯式發(fā)送消息,還可以使用GatewayProxyFactoryBean透明地發(fā)送消息。
翻譯自: https://www.javacodegeeks.com/2014/02/spring-integration-using-rmi-channel-adapters.html
spring rmi
總結(jié)
以上是生活随笔為你收集整理的spring rmi_Spring集成–使用RMI通道适配器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 苹果导入安卓通讯录在哪里(苹果导入安卓通
- 下一篇: 莱芜楼盘备案价(莱芜楼盘备案)