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透明地发送消息。

参考: Spring Integration –使用 XavierPadró博客博客中的JCG合作伙伴 Xavier Padro 使用RMI通道适配器 。

翻译自: https://www.javacodegeeks.com/2014/02/spring-integration-using-rmi-channel-adapters.html

Spring集成–使用RMI通道适配器相关推荐

  1. spring rmi_Spring集成–使用RMI通道适配器

    spring rmi 1.引言 本文介绍了如何使用Spring Integration RMI通道适配器通过RMI发送和接收消息. 它由以下部分组成: 实施服务:第一部分着重于创建和公开服务. 实现客 ...

  2. Spring集成基础知识

    本文是我们名为" EAI的Spring集成 "的学院课程的一部分. 在本课程中,向您介绍了企业应用程序集成模式以及Spring Integration如何解决它们. 接下来,您将深 ...

  3. 通过Spring集成进行消息处理

    Spring Integration提供了Spring框架的扩展,以支持著名的企业集成模式. 它在基于Spring的应用程序中启用轻量级消息传递,并支持与外部系统的集成. Spring Integra ...

  4. Spring集成和Web服务

    本文是我们名为" Spring Integration for EAI "的学院课程的一部分. 在本课程中,向您介绍了企业应用程序集成模式以及Spring Integration如 ...

  5. Spring集成文件轮询和测试

    我最近实施了一个小项目,在该项目中,我们必须轮询文件夹中的新文件,然后在文件内容上触发服务流. Spring Integration非常适合此要求,因为它带有一个通道适配器 ,该适配器可以扫描文件夹中 ...

  6. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  7. windows api中文文档_Web服务开发:Spring集成Swagger,3步自动生成API文档

    目录: 1,Spring Boot集成Swagger 2,Swagger接口文档页面 3,常见问题和解决方法 在Sping开发REST接口服务时,API文档是不可缺少的一个重要部分.Swagger框架 ...

  8. Spring集成spymemcached

    Spring集成spymemcached Memcached的安装部署我就不介绍了! 首先下载spymemcached,下载地址: jar:https://spymemcached.googlecod ...

  9. Liferay7 BPM门户开发之5: Activiti和Spring集成

    参考文档: https://github.com/jbarrez/spring-boot-with-activiti-example https://github.com/sxyx2008/sprin ...

最新文章

  1. sql按相似度模糊查询实例
  2. 用Python做科学计算
  3. 18亿用户、10万条电源线、4200万月活......创业者的底限究竟在哪里?
  4. 最大矩形—leetcode85
  5. 东风本田crv2020新款混动说明书_能上绿牌,无续航焦虑!2020北京车展混动新车抢先看...
  6. OpenJudge计算概论-找出第k大的数
  7. 流计算引擎数据一致性的本质
  8. php 测试控制器,php – 控制器的Laravel单元测试
  9. vue请求数据完成后执行_生产库删除数据后怎样执行对应的undo sql来恢复数据?...
  10. DataGrid与GridView中删除前提示框与编辑框长度设置的实现(ASP.NET)
  11. angular2+ 中封装调用递归tree
  12. 10 个牛逼的一行代码就能搞定的编程技巧,你会用吗?
  13. http://blog.csdn.net/jiazimo/article/details/17265061
  14. python给图片加半透明水印_python给图片增加透明文字水印
  15. MATLAB与DPS做Mann-Kendall显著性检验
  16. 全国所有县的12.5m分辨率DEM数据制作与分享
  17. 【科普】有趣“小学”数学题,做出一道即可成名(持续补充)
  18. 百度蜘蛛的抓取方式有哪些
  19. Android指纹验证(BiometricPrompt)
  20. crm系统如何处理好客户投诉问题?

热门文章

  1. 百度地图描绘轨迹html,百度地图API 绘制轨迹历史
  2. 学院派 实践派 计算机科学与技术,饶旻现场为boss所在企业“挑错”
  3. asp 执行 exe_EXE程序加密锁下载-EXE程序加密锁电脑版下载v5.0
  4. junit 测试执行顺序_JUnit 5中的测试执行顺序
  5. java开发指南_Java 12新功能完整指南
  6. java gradle构建_在Gradle中为JPMS构建Java 6-8库
  7. java如何避免注释重复_Java 8中的可重复注释
  8. 安卓清理垃圾清理代码_从战中清理代码
  9. 众神进入瓦尔哈拉_一时冲动:“通往瓦尔哈拉之路的冒险”
  10. spark wai_WAI-ARIA对自动完成小部件的支持