当前位置: 首页 > 知识库问答 >
问题:

Spring http出站网关希望使用PollableChannel而不是DirectChannel

荀正谊
2023-03-14

我发现的问题是在MessagingGatewaySupport类中,我发现对应答通道的期望应该是PollableChannel,但我需要使用DirectChannel而不是Pollable:

protected Object receive() {
this.initializeIfNecessary();
MessageChannel replyChannel = getReplyChannel();
Assert.state(replyChannel != null && (replyChannel instanceof PollableChannel),
        "receive is not supported, because no pollable reply channel has been configured");
return this.messagingTemplate.receiveAndConvert(replyChannel, null);
}

我真的认为我的配置有一个错误,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-http="http://www.springframework.org/schema/integration/http" xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-4.3.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.3.xsd">

<jee:jndi-lookup id="repositoryUrl" jndi-name="repositoryUrl"/>
<jee:jndi-lookup id="wsMaxTotalConnections" jndi-name="wsMaxTotalConnections" default-value="50"/>
<jee:jndi-lookup id="wsMaxPerRoute" jndi-name="wsDefaultMaxPerRoute" default-value="50"/>
<jee:jndi-lookup id="wsConnectionTimeout" jndi-name="wsDefaultConnectionTimeout" default-value="10000"/>
<jee:jndi-lookup id="wsReadTimeout" jndi-name="wsDefaultReadTimeout" default-value="5000"/>

<int:gateway id="requestGateway" service-interface="com.example.Repository" default-request-channel="requestChannel" 
    default-reply-channel="responseChannel"/>

<int:channel id="requestChannel"/>
<int:channel id="responseChannel"/>

<int-http:outbound-gateway request-channel="requestChannel" url="#{repositoryUrl}" http-method="POST" expected-response-type="java.lang.String"
    request-factory="httpComponentsClientHttpRequestFactory" message-converters="jsonMessageConverter"/>

<bean id="jsonMessageConverter" class="com.example.JsonMessageConverter">
    <constructor-arg>
        <bean class="com.example.DataType"/>
    </constructor-arg>
</bean>

<bean id="httpComponentsClientHttpRequestFactory" class="com.example.ApplicationHttpComponentsClientHttpRequestFactory">
    <property name="maxTotalConnections" ref="wsMaxTotalConnections"/>
    <property name="defaultMaxPerRoute" ref="wsMaxPerRoute"/>
    <property name="connectionTimeout" ref="wsConnectionTimeout"/>
    <property name="readTimeout" ref="wsReadTimeout"/>
</bean>

JsonMessageConverter类:

public class JsonMessageConverter implements HttpMessageConverter<Object> {
private static final Logger logger = LoggerFactory.getLogger(JsonMessageConverter.class);

private Object clazz;

private List<MediaType> supportedMediaTypes = Collections.emptyList();

public Object getClazz()
{
    return clazz;
}

public void setClazz(Object clazz)
{
    this.clazz = clazz;
}

public JsonMessageConverter(Object clazz)
{
    this.clazz = clazz;
}

@Override
public boolean canRead(Class<?> clazz, MediaType mediaType)
{
    return this.clazz.getClass().equals(clazz);
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType)
{
    return false;
}

@Override
public List<MediaType> getSupportedMediaTypes()
{
    return supportedMediaTypes;
}

@Override
public Object read(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
{
    Object response = new Object();

    logger.trace("Received message in :{} ", inputMessage.getBody().toString());

    try
    {
        Gson gson = new Gson();
        String inputStreamString = inputMessage.getBody().toString();

        response = gson.fromJson(inputStreamString, this.clazz.getClass());
    }
    catch (Exception e)
    {
        throw new HttpMessageConversionException("Failed to convert response to: " + clazz, e);
    }

    logger.trace("Received message out :{} ", response);

    return response;

}

@Override
public void write(Object t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
{
    Gson gson = new Gson();

    logger.trace("Sent message in :{} ", t);

    String json = gson.toJson(t);

    outputMessage.getBody().write(json.getBytes());

    logger.trace("Sent message out :{} ", json);
}
java.lang.IllegalStateException: receive is not supported, because no pollable reply channel has been configured
at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.integration.gateway.MessagingGatewaySupport.receive(MessagingGatewaySupport.java:391)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:468)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:429)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:420)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy28.getSoccerSeasons(Unknown Source)
at com.example.ServiceImpl.getData(ServiceImpl.java:26)

Spring Integration的版本是4.3.6。有人能帮帮我吗?我试了很多例子,但我没有找到任何一个有效的。

提前感谢你的帮助。

共有1个答案

司空俊雅
2023-03-14

您需要显示com.example.repository接口。

您正在调用的方法看起来没有参数。您需要发送一些东西才能收到回复。

请参阅有关没有参数的网关方法的文档。

 类似资料:
  • 问题陈述: Spring amqp outbound gateway从不同的线程生成应答(如jms outbound gateway,具有不同的队列,使用相关键关联请求/响应)。 无法将消息与此示例关联。 Spring集成 配置 服务 错误: 组织。springframework。整合。处理程序。ReplyRequiredException:处理程序“outboundGtwyId”未生成回复,其“

  • 我可以看到请求消息被加入和出列,响应消息被加入和出列。但我有个例外, 我应该如何检索响应?

  • 我正在尝试将spring集成配置为向队列发送消息,然后接收消息,即非常简单的事情: 我认为解耦所必需的是在流程的两端都有一个消息网关。因此,我的第一次尝试(有效)如下所示: 其中MessageReceiverHandler()是扩展AbstractMessageHandler的bean。 所以上面我们有一个用于出站消息的消息网关。我假设我们也应该有一个用于入站消息的网关,允许我们将传入消息处理与应

  • 我是Spring integration SFTP的新手。现在,我想从多个目录下载文件。那么SFTP出站网关似乎是我的首选,但我只找到了使用XML config的示例。如何使用Java config来实现这一点呢?我的配置类: 但当我启动应用程序时,什么也没发生,哪里出了问题? ---更新---我的测试类

  • 我一直使用SI出站网关调用使用NTLM(Microsoft Dynamics backend)保护的RESTendpoint。它非常简单,只需配置HttpComponentsMessageSender凭据以使用Apache HttpClient的NTCredentials,并且它可以透明地工作。

  • 我需要关于spring集成jdbc出站网关的帮助。我目前正在使用spring集成示例jdbc。我添加代码来使用jdbc出站网关更新数据,下面是spring集成上下文。xml: 这是我的个人服务。爪哇: Main.java: 为什么我总是得到这个错误消息,每当我试图运行它? 那个人。java POJO: 请帮帮我,我不知道是哪种java。对象,因为我在代码中的任何地方都不使用它。谢谢