![](/img/trans.png)
[英]What is difference between PollableChannel and DirectChannel?
[英]Spring http outbound gateway wants to use PollableChannel instead of DirectChannel
我在應用程序中發現一個配置問題,該問題應嘗試向第三方服務發送請求,並應等待響應。 請求和響應的內容類型為json。
我發現的問題是在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。 有人可以幫我嗎? 我嘗試了許多示例,但沒有找到任何能正常工作的示例。
在此先感謝您的幫助。
您需要顯示com.example.Repository
接口。
您正在調用的方法似乎沒有參數。 您需要發送一些東西才能收到回復。
請參閱有關沒有參數的網關方法的文檔 。
在網關接口上調用不帶任何參數的方法時,默認行為是從PollableChannel接收消息。
但是,有時您可能希望觸發無參數方法,以便實際上可以與不需要用戶提供參數的其他下游組件進行交互,例如,觸發無參數SQL調用或存儲過程。
為了實現發送和接收語義,您必須提供有效負載。 為了生成有效載荷,...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.