最初のパラメータ
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);
}
}
のために働いていました