javascript
spring rmi_Spring集成–使用RMI通道适配器
spring rmi
1.引言
本文介紹了如何使用Spring Integration RMI通道適配器通過RMI發送和接收消息。 它由以下部分組成:
- 實施服務:第一部分著重于創建和公開服務。
- 實現客戶端:顯示如何使用MessagingTemplate類調用服務。
- 抽象SI邏輯:最后,我添加了另一部分,解釋了如何實現抽象所有Spring Integration代碼的相同客戶端,而使客戶端專注于其業務邏輯。
您可以在github上獲取源代碼。
2.實施服務
第一部分非常簡單。 該服務是通過注釋定義的,因此它將通過組件掃描自動檢測。 它注入了一個存儲庫,該存儲庫從嵌入式數據庫獲取數據,這將在同一部分中顯示:
@Service("defaultEmployeeService") public class EmployeeServiceImpl implements EmployeeService {@Autowiredprivate EmployeeRepository employeeRepository;@Overridepublic Employee retrieveEmployee(int id) {return employeeRepository.getEmployee(id);} }存儲庫如下:
@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;}} }以下配置通過RMI公開服務:
服務器配置文件
<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>讓我們關注帶有'int'名稱空間的行:
網關的功能是將消息傳遞系統的管道與應用程序的其余部分分開。 這樣,它就被業務邏輯隱藏了。 網關是雙向的,因此您具有:
- 入站網關:將消息帶入應用程序并等待響應。
- 出站網關:調用外部系統,并將響應發送回應用程序。
在此示例中,我們使用RMI入站網關。 它將通過RMI接收一條消息并將其發送到requestEmployee通道,該通道也在此處定義。
最后, 服務激活器允許您將spring bean連接到消息通道。 在這里,它連接到requestEmployee通道。 消息將到達通道,服務激活器將調用retrieveEmployee方法。 考慮到如果bean只有一個公共方法或帶有@ServiceActivator注釋的方法,則不需要'method'屬性。
然后,響應將發送到回復通道。 由于我們沒有定義此通道,因此它將創建一個臨時回復通道。
3,實施客戶
我們將要實現的客戶端將調用服務以檢索員工。 為此,它將使用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對象轉換為Message并將其發送到本地通道。 如下所示,有一個出站網關連接到本地通道。 該出站網關將通過RMI發送請求消息。
<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邏輯
在上一節中,您可能已經注意到,訪問服務的客戶端類具有特定于Spring Integration的邏輯及其業務代碼:
- 它使用MessagingTemplate,它是一個SI類。
- 它了解本地通道,該本地通道特定于消息傳遞系統
在本節中,我將實現與抽象消息傳遞邏輯相同的示例,因此客戶端將只關心其業務邏輯。
首先,讓我們看一下新客戶端:
@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());} }現在我們可以看到客戶端僅實現其業務邏輯,而不使用消息通道或消息傳遞模板。 它將僅調用服務接口。 所有消息傳遞定義都在配置文件中。
<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"/>客戶端網關配置文件
我們在這里所做的是添加一個網關,該網關將攔截對服務接口EmployeeService的調用。 Spring Integration將使用GatewayProxyFactoryBean類在服務接口周圍創建代理。 該代理將使用消息傳遞模板將調用發送到請求通道并等待響應。
5,結論
我們已經看到了如何使用Spring Integration通過RMI訪問服務。 我們還看到,我們不僅可以使用MessagingTemplate顯式發送消息,還可以使用GatewayProxyFactoryBean透明地發送消息。
翻譯自: https://www.javacodegeeks.com/2014/02/spring-integration-using-rmi-channel-adapters.html
spring rmi
總結
以上是生活随笔為你收集整理的spring rmi_Spring集成–使用RMI通道适配器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果导入安卓通讯录在哪里(苹果导入安卓通
- 下一篇: gradle idea java ssm