【问题标题】:Spring Integration Gateway Inner Bean DefinitionSpring集成网关内部bean定义
【发布时间】:2014-08-15 21:58:20
【问题描述】:

我正在升级中:

  • 春季 3.1.0.RELEASE => 4.0.6.RELEASE
  • Spring 集成 2.1.0.RELEASE => 4.0.3.RELEASE

我注意到网关的 XML 配置作为内部 bean 存在一些不兼容性。

在升级之前,这样的事情很好:

<bean id="eventPublisher" class="com.blorp.EventPublisher">
    <constructor-arg>
           <int:gateway default-request-channel="eventPublishingChannel"
                         default-reply-timeout="10000"
                         service-interface="com.blorp.messaging.Sender">
                <int:method name="send" />
            </int:gateway>
    </constructor-arg>
</bean>


<int:channel id="eventPublishingChannel" />

执行上述升级后,在运行时实际发送事件时,由于尚未设置请求通道而引发异常:

Caused by: java.lang.IllegalStateException: send is not supported, because no request channel has been configured
    at org.springframework.util.Assert.state(Assert.java:385) ~[spring-core-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.integration.gateway.MessagingGatewaySupport.send(MessagingGatewaySupport.java:197) ~[spring-integration-core-4.0.3.RELEASE.jar:na]
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:344) ~[spring-integration-core-4.0.3.RELEASE.jar:na]
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:304) ~[spring-integration-core-4.0.3.RELEASE.jar:na]
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:295) ~[spring-integration-core-4.0.3.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at com.sun.proxy.$Proxy84.send(Unknown Source) ~[na:na]
    at com.blorp.DefaultEventPublisher.publish(DefaultEventPublisher.java:29) ~[blorp-0.54.0-SNAPSHOT.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_45]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_45]
...

当我使用调试器单步调试时,MessageGatewaySupport.requestChannel 属性确实是 null,即使配置指定了它。

解决方法是不要将网关配置为内部 bean。下面的 sn-p 运行良好:

<int:gateway id="sender" default-request-channel="eventPublishingChannel"
                         default-reply-timeout="10000"
                         service-interface="com.blorp.messaging.Sender">
    <int:method name="send" />
</int:gateway>

<bean id="eventPublisher" class="com.blorp.EventPublisher">
    <constructor-arg ref="sender" />
</bean>

我很好奇为什么升级后前者不起作用。创建代理有问题吗?它是一个错误吗?我还没有找到这方面的问题。

【问题讨论】:

    标签: spring configuration spring-integration


    【解决方案1】:

    确实如此,因为在这里您尝试将&lt;gateway&gt; 配置为嵌套元素。

    自 Spring Integration 3.0 起,只能在 &lt;chain&gt; 定义中使用。

    根源就在这里(GatewayParser):

    boolean isNested = parserContext.isNested();
    ...
    gatewayAttributes.put("defaultRequestChannel", element.getAttribute(isNested ? "request-channel" : "default-request-channel"));
    

    对于任何其他情况,您确实必须将&lt;gateway&gt; 配置为根组件并使用其id 注入其他bean。

    只是简单的迁移成本:其他情况可能没那么简单:https://github.com/spring-projects/spring-integration/wiki

    从另一面,我发现你 nested 解决方案对于集成来说并没有像 Gateway 模式定义那样有用。

    我确信有一天您会决定使用来自另一个服务的相同网关接口。

    在这种情况下不要试图处理框架:它只是一个我们应该遵循的 API,因为我们必须提供一个具体类型的对象作为某些 Java 核心类的现有方法的参数。

    【讨论】:

    • 我已经看到了迁移提示链接,但在此处或搜索中没有找到任何关于此的内容,感谢您指出 GatewayParser。我希望不会找到其他运行时惊喜 =)
    猜你喜欢
    • 2015-05-30
    • 2023-04-04
    • 1970-01-01
    • 2021-01-08
    • 2018-01-05
    • 2012-05-21
    • 2018-06-15
    • 2014-07-24
    • 2019-03-01
    相关资源
    最近更新 更多