2016-09-28 2 views
1

クライアントがSpring統合HTTP受信ゲートウェイの1つを呼び出す必要があり、APIに与えられた入力は.csvそして、 要求が検証され、正しい即時応答がステータス200 OKで送信されるべきであることが判明したら、エラーの場合、適切なエラーメッセージが送信されます。 非同期処理には、直接チャネルと実行チャネルの組み合わせを使用します。これは、春のブート親バージョン1.2.5を使用すると正常に動作しますが、バージョン1.4.0にアップグレードすると失敗します。ログから見つかった原因がMessageTimeoutExceptionであるため、常に500内部サーバーエラーが発生します。Spring統合 - HTTP要求処理メッセージングゲートウェイでバックグラウンドで非同期処理を使用して送信される即時応答

私たちはJavaベースの設定を使用しており、設定は以下のとおりです。

のpom.xml

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.0.RELEASE</version> 
    </parent> 

      <dependency> 
      <groupId>org.springframework.integration</groupId> 
      <artifactId>spring-integration-http</artifactId> 
      <version>4.3.1.RELEASE</version> 
     </dependency> 

@Configuration 
public class ApplicationIntegrationConfig {  

    @Bean 
    public HttpRequestHandlingMessagingGateway httpMessageGateway(){ 
     HttpRequestHandlingMessagingGateway gateway 
       = new HttpRequestHandlingMessagingGateway(Boolean.TRUE); 
     RequestMapping requestMapping = new RequestMapping(); 
     requestMapping.setMethods(HttpMethod.POST); 
     requestMapping.setPathPatterns("/org/{orgId}/users"); 
     requestMapping.setHeaders("Content-Type=text/csv"); 
     gateway.setRequestMapping(requestMapping); 
     gateway.setRequestChannel(onBoardUserRequestChannel()); 
     Map<String, Expression> customHeaderExpressions = new HashMap<>(); 
     customHeaderExpressions.put("orgId", new SpelExpressionParser(). 
       parseExpression("#pathVariables.orgId")); 
     gateway.setHeaderExpressions(customHeaderExpressions); 
     gateway.setErrorChannel(errorChannel()); 
     gateway.setReplyTimeout(0); 
     return gateway; 
    }  

    @Bean 
    public MessageChannel processUserRequestChannel() { 
     DirectChannel channel =new DirectChannel(); 
     channel.addInterceptor(new AuthenticationInterceptor()); 
     return channel; 
    } 

    @Bean 
    public MessageChannel routeChannel() { 
     return new ExecutorChannel(Executors.newCachedThreadPool()); 
    } 

    @Bean 
    public MessageChannel addUserChannel() { 
     return new ExecutorChannel(Executors.newCachedThreadPool()); 
    } 

    @Bean 
    public MessageChannel removeUserChannel() { 
     return new ExecutorChannel(Executors.newCachedThreadPool()); 
    } 

    @Bean 
    public MessageChannel errorChannel() { 
     return new DirectChannel(); 
    }  
} 

スプリッタ

@MessageEndpoint 
public class PartnerUserOnBoardSplitter {  

    @Splitter(inputChannel= "processUserRequestChannel", outputChannel="routeChannel") 
    public List<UserDTO> split(Message message) throws ApplicationException { 
     List<UserDTO> userList = null; 
     try { 
      userList = validateAndCreateDTO(message); 
     } 
     } catch(Exception ex) { 
      throw new ApplicationException("<Message>"); 
     } 
     return userList; 
    }  
} 

ルータ

@MessageEndpoint 
public class CustomRouter { 

    @Router(inputChannel="routeChannel") 
    public String resolveRoute(UserDTO dto) { 
     return (Operation.ADD.equals(dto.getOperation())) ? "addUserChannel" : "removeUserChannel"; 
    }  
} 

public class ServiceActivator{ 

@ServiceActivator(inputChannel = "addUserChannel") 
public addUser(UserDto dto){ 
//process add 
} 

@ServiceActivator(inputChannel = "removeUserChannel") 
public removeUser(UserDto dto){ 
//process remove 
}  
} 

答えて

0

これは、春の統合4.2増強/改善しました。

以前は、返信が予想されてタイムアウトした場合、ユーザーは間違って200 OKを取得しました。今すぐ彼はタイムアウト例外を持つ500を取得します。

応答を待つようにゲートウェイを設定しているため、この機能が起動しています。

単に返信を期待しないようにゲートウェイを設定...

HttpRequestHandlingMessagingGateway gateway 
      = new HttpRequestHandlingMessagingGateway(false); 

コンテナのスレッドが来ることはありません応答を待機しませんので、あなたはまた、より高速な応答が表示されます(既定のタイムアウトは1秒です) 。

EDIT

あなたは時々、他の回で応答を送信しないようにしたい場合は、trueにexpectReplyを設定し、この設定を追加します。

gateway.setStatusCodeExpression(new SpelExpressionParser().parseExpression("200")); 
gateway.setReplyTimeout(0); 

私はadded some javadocs - それが役に立てば幸い。 XML構成を使用してそれらのために

、それは...返信用

<int-http:inbound-gateway request-channel="receiveChannel" 
         path="/receiveGateway" 
         reply-timeout="0" 
         reply-timeout-status-code-expression="200" 
         supported-methods="POST"/> 
+0

おかげです。しかし、私はどのように返信し、入力が無効であればエラーメッセージを返します。ゲートウェイが応答を期待しないように設定すると、エラーメッセージも送信されません。妥当性チェックが成功し、処理がバックグラウンドで継続すると予想される場合は200 OKと返信します。エラーメッセージを呼び出し元に返します。 – Dpr

+0

編集を参照してください。 –

関連する問題