Spring批处理作业是线程安全的吗?

时间:2019-05-17 15:38:41

标签: spring-batch

我需要并行执行批处理春季作业的单个步骤。在并行化步骤之前,将运行Tasklet,这些任务会将一些结果放入作业的参数中。 Tasklet产生的结果对于执行Partitioner和要并行化的步骤的项目是必要的。 我真的无法解决一个疑问。由于我可以使用相同的初始参数让同一份作业同时运行多次,因此小任务和步骤项是否安全线程安全?

2 个答案:

答案 0 :(得分:0)

您不想运行同一作业的多个实例。最好在同一步骤和/或作业中运行多个任务或流程。您可能需要查找作业分区,或进行远程处理以进行并发处理。

如果它必须是孤立的作业,那么您可能会写出并发作业以说一个消息队列作为结束(写程序)步骤,然后让另一个作业侦听该队列中的消息。

https://docs.spring.io/spring-batch/2.1.x/cases/parallel.html

答案 1 :(得分:0)

否,tasklet和面向块的步骤组件不是线程安全的。如果在同时运行的多个作业实例/执行之间共享它们,则需要使它们成为线程安全的。

您可以使用public OAuth2AccessToken createAccessToken(Authentication authentication) { UserPrincipal userPrincipal = (UserPrincipal)authentication.getPrincipal(); Set<GrantedAuthority> authorities = new HashSet(userPrincipal.getAuthorities()); Map<String, String> requestParameters = new HashMap<>(); String clientId = "jwtClientIdPassword"; boolean approved = true; Set<String> resourceIds = new HashSet<>(); Set<String> responseTypes = new HashSet<>(); responseTypes.add("code"); Map<String, Serializable> extensionProperties = new HashMap<>(); List<String> scopes = new ArrayList<>(); scopes.add("read"); scopes.add("write"); OAuth2Request oAuth2Request = new OAuth2Request(requestParameters, clientId, authorities, approved, new HashSet<>(scopes), resourceIds, null, responseTypes, extensionProperties); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userPrincipal, null, authorities); OAuth2Authentication auth = new OAuth2Authentication(oAuth2Request, authenticationToken); AuthorizationServerTokenServices tokenService = configuration.getEndpointsConfigurer().getTokenServices(); OAuth2AccessToken token = tokenService.createAccessToken(auth); return token; } d个步骤和JobScope d个读者/作家来实现。您还可以使用StepScope和(upcomingSynchronizedItemStreamReader使读者和作家线程安全。 Spring Batch提供的所有项目读写器在Javadoc中都提到了它们的线程安全性。

相关问题