Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么是<;int:spring中JavaDSL中的网关xml标记-集成?_Java_Spring Integration_Gateway_Spring Integration Dsl - Fatal编程技术网

什么是<;int:spring中JavaDSL中的网关xml标记-集成?

什么是<;int:spring中JavaDSL中的网关xml标记-集成?,java,spring-integration,gateway,spring-integration-dsl,Java,Spring Integration,Gateway,Spring Integration Dsl,我想使用java DSL重写以下内容: 目前,我坚持以下配置部分: <int:gateway id="userGateway" default-request-timeout="5000" default-reply-timeout="5000" service-interface="org.springframework.integration.samples.enricher.service.UserService">

我想使用java DSL重写以下内容:

目前,我坚持以下配置部分:

<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>
发票人:

   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));
@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 {

    private static final Log LOGGER = LogFactory.getLog(SystemService.class);

    /** Default Constructor. */
    public SystemService() {
        super();
    }

    public User findUser(User user) {

        LOGGER.info(String.format("Calling method 'findUser' with parameter %s", user));

        final User fullUser = new User(user.getUsername(),
                                       "secret",
                                       user.getUsername() + "@springintegration.org");
        return fullUser;
    }

    public User findUserByUsername(String username) {

        LOGGER.info(String.format("Calling method 'findUserByUsername' with parameter: %s", username));

        return new User(username, "secret", username + "@springintegration.org");

    }

}
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}
用户服务

@Bean
public IntegrationFlow findUserEnricherChannelFlow() {
    return IntegrationFlows.from(UserService.class)
            .handle(SystemService.class, "findUser") // I have no idea how to map all methods
            .get();
}
public interface UserService {

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

    /**
     * Retrieves a user based on the provided user. User object is routed to the
     * "findUserByUsernameEnricherChannel" channel.
     */
    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.
     */
    Map<String, Object> findUserWithUsernameInMap(Map<String, Object> userdata);

}
public class SystemService {

    private static final Log LOGGER = LogFactory.getLog(SystemService.class);

    /** Default Constructor. */
    public SystemService() {
        super();
    }

    public User findUser(User user) {

        LOGGER.info(String.format("Calling method 'findUser' with parameter %s", user));

        final User fullUser = new User(user.getUsername(),
                                       "secret",
                                       user.getUsername() + "@springintegration.org");
        return fullUser;
    }

    public User findUserByUsername(String username) {

        LOGGER.info(String.format("Calling method 'findUserByUsername' with parameter: %s", username));

        return new User(username, "secret", username + "@springintegration.org");

    }

}
但在这种情况下,应用程序不能从以下跟踪开始:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-08-29 16:04:54.293 ERROR 7948 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'findUserEnricherChannelFlow' defined in class path resource [enricher/Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.integration.dsl.IntegrationFlow]: Factory method 'findUserEnricherChannelFlow' threw exception; nested exception is java.lang.IllegalStateException: Target object of type [class java.lang.Class] has no eligible methods for handling Messages.
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at enricher.MyApplication.main(MyApplication.java:13) ~[classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.integration.dsl.IntegrationFlow]: Factory method 'findUserEnricherChannelFlow' threw exception; nested exception is java.lang.IllegalStateException: Target object of type [class java.lang.Class] has no eligible methods for handling Messages.
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.IllegalStateException: Target object of type [class java.lang.Class] has no eligible methods for handling Messages.
    at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.integration.handler.support.MessagingMethodInvokerHelper.findHandlerMethodsForTarget(MessagingMethodInvokerHelper.java:898) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.integration.handler.support.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:293) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.integration.handler.support.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:223) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.integration.handler.support.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:227) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.integration.handler.MethodInvokingMessageProcessor.<init>(MethodInvokingMessageProcessor.java:54) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.integration.handler.ServiceActivatingHandler.<init>(ServiceActivatingHandler.java:46) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:996) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:979) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at enricher.Config.findUserEnricherChannelFlow(Config.java:36) ~[classes/:na]
    at enricher.Config$$EnhancerBySpringCGLIB$$f36636fe.CGLIB$findUserEnricherChannelFlow$0(<generated>) ~[classes/:na]
    at enricher.Config$$EnhancerBySpringCGLIB$$f36636fe$$FastClassBySpringCGLIB$$ada0b78a.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at enricher.Config$$EnhancerBySpringCGLIB$$f36636fe.findUserEnricherChannelFlow(<generated>) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ... 18 common frames omitted
主要方法:

   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));
@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 {

    private static final Log LOGGER = LogFactory.getLog(SystemService.class);

    /** Default Constructor. */
    public SystemService() {
        super();
    }

    public User findUser(User user) {

        LOGGER.info(String.format("Calling method 'findUser' with parameter %s", user));

        final User fullUser = new User(user.getUsername(),
                                       "secret",
                                       user.getUsername() + "@springintegration.org");
        return fullUser;
    }

    public User findUserByUsername(String username) {

        LOGGER.info(String.format("Calling method 'findUserByUsername' with parameter: %s", username));

        return new User(username, "secret", username + "@springintegration.org");

    }

}
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魔术来进行转换?

文档是针对旧版本的DSL for Spring Integration 4.3的;自5.0版以来,DSL被集成到主项目和文档中。该版本中不存在网关功能

请参见Wiki页面顶部的横幅

从5.0版开始,该项目已被Spring集成核心吸收。有关实际文件,请参阅其参考手册。此项目仅处于维护、错误修复状态。

IntegrationFlow可以从提供GatewayProxyFactoryBean组件的服务接口开始,如下例所示:

公共接口控制总线网关{
无效发送(字符串命令);
}
...
@豆子
公共集成流量控制总线流量(){
返回IntegrationFlows.from(ControlBusGateway.class)
.controlBus()
.get();
}
该示例应用程序的工作原理相同

公共接口请求网关{
字符串回显(字符串请求);
}
..来自(RequestGateway.class)
...
编辑

DSL网关目前不支持具有不同通道的多方法网关。你可以使用一些技巧


@SpringBoot应用程序
@IntegrationComponentScan//查找消息传递网关
公共类SO57709118应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(So57709118Application.class,args);
}
@豆子
公共集成流(){
返回IntegrationFlows.from(“网关通道”)
//.路线(……)
.log()
.nullChannel();
}
@豆子
@DependsOn(“流”)
公共应用程序运行程序(大门){
返回参数->{
闸门方法1(“bar”);
闸门方法2(“bar”);
};
}
}
@MessagingGateway(defaultRequestChannel=“gatewayChannel”,
defaultHeaders=@GatewayHeader(name=“method”,expression=“#gatewayMethod.name”))
接口门{
无效方法1(字符串foo);
无效方法2(字符串foo);
}
或者,您可以在每个方法上设置requestChannel,而不是所有方法的公共流

@MessagingGateway(defaultHeaders=@GatewayHeader(name=“method”,expression=“#gatewayMethod.name”))
接口门{
@网关(requestChannel=“foo”)
无效方法1(字符串foo);
@网关(requestChannel=“bar”)
无效方法2(字符串foo);
}

文档是针对Spring Integration 4.3的旧版本DSL的;自5.0版以来,DSL被集成到主项目和文档中。该版本中不存在网关功能

请参见Wiki页面顶部的横幅

从5.0版开始,该项目已被Spring集成核心吸收。有关实际文件,请参阅其参考手册。此项目仅处于维护、错误修复状态。

IntegrationFlow可以从提供GatewayProxyFactoryBean组件的服务接口开始,如下例所示:

公共接口控制总线网关{
无效发送(字符串命令);
}
...
@豆子
公共集成流量控制总线流量(){
返回IntegrationFlows.from(ControlBusGateway.class)
.controlBus()
.get();
}
该示例应用程序的工作原理相同

公共接口请求网关{
字符串回显(字符串请求);
}
..来自(RequestGateway.class)
...
编辑

DSL网关目前不支持具有不同通道的多方法网关。你可以使用一些技巧


@SpringBoot应用程序
@IntegrationComponentScan//查找消息传递网关
公共类SO57709118应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(So57709118Application.class,args);
}
@豆子
公共集成流(){
返回IntegrationFlows.from(“网关通道”)
//.路线(……)
.log()
.nullChannel();
}
@豆子
@DependsOn(“流”)
公共应用程序运行程序(大门){
返回参数->{
闸门方法1(“bar”);
闸门方法2(“bar”);
};
}
}
@MessagingGateway(defaultRequestChannel=“gatewayChannel”,
defaultHeaders=@GatewayHeader(name=“method”,expression=“#gatewayMethod.name”))
接口门{
无效方法1(字符串foo);
无效方法2(字符串foo);
}
或者,您可以在每个方法上设置requestChannel,而不是所有方法的公共流

@MessagingGateway(defaultHeaders=@GatewayHeader(name=“method”,expression=“#gatewayMethod.name”))
接口门{
@网关(requestChannel=“foo”)
无效方法1(字符串foo);
@网关(requestChannel=“bar”)
无效方法2(字符串foo);
}

但是如何设置接口方法和一些实际方法调用之间的映射呢?在您的示例中,ControlBusGateway只有一个方法,但UserService有三种不同的方法。关于以下UserService:您可以使用多个方法,只要它们都指向同一个流。当每种方法转到不同的信道时,没有直接的DSL等价物;当使用DSL时,我们