gpt4 book ai didi

java - 如何使用spring集成java DSL实现enricher?

转载 作者:行者123 更新时间:2023-12-02 09:33:43 54 4
gpt4 key购买 nike

我想重写following xml sample使用java DSL

xml 配置:

    <int:channel id="findUserServiceChannel"/>
<int:channel id="findUserByUsernameServiceChannel"/>

<!-- See also:
https://docs.spring.io/spring-integration/reference/htmlsingle/#gateway-proxy
https://www.enterpriseintegrationpatterns.com/MessagingGateway.html -->
<int:gateway id="userGateway" default-request-timeout="5000"
default-reply-timeout="5000"
service-interface="org.springframework.integration.samples.enricher.service.UserService">
<int:method name="findUser" request-channel="findUserEnricherChannel"/>
<int:method name="findUserByUsername" request-channel="findUserByUsernameEnricherChannel"/>
<int:method name="findUserWithUsernameInMap" request-channel="findUserWithMapEnricherChannel"/>
</int:gateway>

<int:enricher id="findUserEnricher"
input-channel="findUserEnricherChannel"
request-channel="findUserServiceChannel">
<int:property name="email" expression="payload.email"/>
<int:property name="password" expression="payload.password"/>
</int:enricher>

<int:enricher id="findUserByUsernameEnricher"
input-channel="findUserByUsernameEnricherChannel"
request-channel="findUserByUsernameServiceChannel"
request-payload-expression="payload.username">
<int:property name="email" expression="payload.email"/>
<int:property name="password" expression="payload.password"/>
</int:enricher>

<int:enricher id="findUserWithMapEnricher"
input-channel="findUserWithMapEnricherChannel"
request-channel="findUserByUsernameServiceChannel"
request-payload-expression="payload.username">
<int:property name="user" expression="payload"/>
</int:enricher>

<int:service-activator id="findUserServiceActivator"
ref="systemService" method="findUser"
input-channel="findUserServiceChannel"/>

<int:service-activator id="findUserByUsernameServiceActivator"
ref="systemService" method="findUserByUsername"
input-channel="findUserByUsernameServiceChannel"/>

<bean id="systemService"
class="org.springframework.integration.samples.enricher.service.impl.SystemService"/>

现在我有以下内容:

配置:

@Configuration
@EnableIntegration
@IntegrationComponentScan
public class Config {

@Bean
public SystemService systemService() {
return new SystemService();
}

@Bean
public IntegrationFlow findUserEnricherFlow(SystemService systemService) {
return IntegrationFlows.from("findUserEnricherChannel")
.<User>handle((p, h) -> systemService.findUser(p))
.get();
}

@Bean
public IntegrationFlow findUserByUsernameEnricherFlow(SystemService systemService) {
return IntegrationFlows.from("findUserByUsernameEnricherChannel")
.<User>handle((p, h) -> systemService.findUserByUsername(p.getUsername()))
.get();
}

@Bean
public IntegrationFlow findUserWithUsernameInMapFlow(SystemService systemService) {
return IntegrationFlows.from("findUserWithMapEnricherChannel")
.<Map<String, Object>>handle((p, h) -> {
User user = systemService.findUserByUsername((String) p.get("username"));
Map<String, Object> map = new HashMap<>();
map.put("username", user.getUsername());
map.put("email", user.getEmail());
map.put("password", user.getPassword());
return map;
})
.get();
}
}

服务接口(interface):

@MessagingGateway
public interface UserService {

/**
* Retrieves a user based on the provided user. User object is routed to the
* "findUserEnricherChannel" channel.
*/
@Gateway(requestChannel = "findUserEnricherChannel")
User findUser(User user);

/**
* Retrieves a user based on the provided user. User object is routed to the
* "findUserByUsernameEnricherChannel" channel.
*/
@Gateway(requestChannel = "findUserByUsernameEnricherChannel")
User findUserByUsername(User user);

/**
* Retrieves a user based on the provided username that is provided as a Map
* entry using the mapkey 'username'. Map object is routed to the
* "findUserWithMapChannel" channel.
*/
@Gateway(requestChannel = "findUserWithMapEnricherChannel")
Map<String, Object> findUserWithUsernameInMap(Map<String, Object> userdata);

}

目标服务:

public class SystemService {

public User findUser(User user) {
...
}

public User findUserByUsername(String username) {
...
}

}

主要方法:

public static void main(String[] args) {
ConfigurableApplicationContext ctx = new SpringApplication(MyApplication.class).run(args);
UserService userService = ctx.getBean(UserService.class);
User user = new User("some_name", null, null);
System.out.println("Main:" + userService.findUser(user));
System.out.println("Main:" + userService.findUserByUsername(user));
Map<String, Object> map = new HashMap<>();
map.put("username", "vasya");
System.out.println("Main:" + userService.findUserWithUsernameInMap(map));
}

输出:

2019-08-30 14:09:29.956  INFO 12392 --- [           main] enricher.MyApplication                   : Started MyApplication in 2.614 seconds (JVM running for 3.826)
2019-08-30 14:09:29.966 INFO 12392 --- [ main] enricher.SystemService : Calling method 'findUser' with parameter User{username='some_name', password='null', email='null'}
Main:User{username='some_name', password='secret', email='some_name@springintegration.org'}
2019-08-30 14:09:29.967 INFO 12392 --- [ main] enricher.SystemService : Calling method 'findUserByUsername' with parameter: some_name
Main:User{username='some_name', password='secret', email='some_name@springintegration.org'}
2019-08-30 14:09:29.967 INFO 12392 --- [ main] enricher.SystemService : Calling method 'findUserByUsername' with parameter: vasya
Main:{password=secret, email=vasya@springintegration.org, username=vasya}

如您所见,一切正常,但我在配置内部进行了转换。我不确定我是否必须这样做,因为 xml 配置没有这样的转换,并且一切都使用内部魔法以某种方式工作。这是正确的方法还是我应该使用一些内部 DSL 魔法进行转换?

附注

我想Config类可以以某种方式简化。我的意思是findUserByUsernameEnricherFlow findUserWithUsernameInMapFlow方法

更新

我意识到我并不真正理解 XML 配置是如何工作的:

让我们考虑一下方法 Userservice#findUserWithUsernameInMap方法

它有以下界面:

Map<String, Object> findUserWithUsernameInMap(Map<String, Object> userdata);

它最终会调用 findUserByUsername方法SystemService :

public User findUserByUsername(String username) 

因为客户端代码使用 Userservice里面有2个转换:

  1. 在 TO 路上(在 SystemService#findUserByUsername 调用之前),因为 Userservice#findUserWithUsernameInMap接受Map<String, Object>但是SystemService#findUserByUsername接受字符串

  2. 返回途中(SystemService#findUserByUsername 调用之后),因为 SystemService#findUserByUsername返回 User 但 Userservice#findUserWithUsernameInMap返回Map<String, Object>

这些转换到底是在 xml 配置中声明的?

我建议使用 request-payload-expression 来进行 TO 转换。看起来它可以使用与对象相同的方式与 Map 一起使用。但是BACK变换根本就不清楚。确定配置有

<int:property name="user"    expression="payload"/>

但是我不知道这是什么意思。

最佳答案

Java DSL 相当于 <int:enricher>.enrich() 。所以,你的findUserEnricherFlow应该是这样的:

@Bean
public IntegrationFlow findUserEnricherFlow(SystemService systemService) {
return IntegrationFlows.from("findUserEnricherChannel")
.enrich((enricher) -> enricher
.requestChannel("findUserServiceChannel")
.propertyExpression("email", "payload.email")
.propertyExpression("password", "payload.password"))

.get();
}

您仍然可以简单地只指出一种网关方法和一种丰富器的问题...

关于java - 如何使用spring集成java DSL实现enricher?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57727592/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com