2017-08-02 6 views
1

)Spring Webアプリケーションがあります。 APIはJerseyで実装されていますが、WebサーバーはJettyです。Spring:Webアプリケーションスレッドからリクエスト(セッション)スコープ付きBeanにアクセスする(ThreadPoolから)子スレッド(

親スレッドによって初期化された並列ラムダ式とマルチキャストされたApache Camelルートから要求スコープBeanにアクセスできるようにしたい。

子スレッドが(InheritableThreadLocal変数を介して)親スレッドから要求コンテキストを継承できるようにすることは可能です。問題は、それらのプロパティがであり子スレッドに渡されていないことですが、スレッドプール(別のラムダとラクダ)からが再利用されているためです。

リクエストバインド情報をパラメータで渡すことはできません。変更する必要のあるメソッドがプロジェクト内に多すぎます。

答えて

0

最初のパラメータ

SecurityContext context = SecurityContextHolder.getContext(); 
RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); 

を取得し、

SecurityContextHolder.setContext(context); 
RequestContextHolder.setRequestAttributes(attributes, true); 

私はの入力としてファイルを読んで私のアプリケーションで同じ問題に直面していたようなあなたのスレッド内でそれらを設定することによって、それを行うことができますそれを行ごとに解析し、データベースにレコードを挿入する休息要求。

ファイルに5個以上のlacレコードが含まれていて、処理に時間がかかりすぎました。だから私はパラレルストリームに行くことにしました。コードの下

は私

public void saveRecordsFromFile(MultipartFile file) { 

    // Getting security and request params 
    SecurityContext context = SecurityContextHolder.getContext(); 
    RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); 

    try (BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream()))) { 

     // Reading the file line by line and making rule 
     br.lines().parallel().forEach(line -> { 

      // Setting security and request params for current thread 
      SecurityContextHolder.setContext(context); 
      RequestContextHolder.setRequestAttributes(attributes, true); 

      saveRecord(line); 

     }); 
    } catch (Exception ex) { 
     throw new SystemException("Error while input file", ex); 
    } 
} 
のために働いていました
関連する問題