In my project, I was trying to convert a Flux object to a different one and to save as a list of Flux. createPostTagsReactor() method uses convertToPost for some business logic.
@Test
public void creatPostTagsReactor(){
Map<String, Tag> nameMapTag = tagRepository.findAll().collectList().block()
.parallelStream()
.collect(Collectors.toMap(t-> t.getTagName(), t-> t));
Flux<PostTags> postTagsFlux = postRepository.findAll()
.flatMap(p-> convertToPostTags(p, nameMapTag));
postTagsRepository.saveAll(postTagsFlux).subscribe();
}
Flux<PostTags> convertToPostTags(Post post, Map<String, Tag> nameMapTag){
List<PostTags> postTags = new ArrayList<>();
return TopicsModelingUtils
.extractTagsReactive(post.getTags())
.map(t-> PostTags.builder().postId(post.getId()).tagId(nameMapTag.get(t).getId()).build());
}
Method extractTagsReactive is borrowed from an Util class and the method body is:
public static Flux<String> extractTagsReactive(String tags){
tags = tags.replaceAll("<","");
tags = tags.replaceAll(">",",");
List<String> tagList = new LinkedList<>(StringUtils.commaDelimitedListToSet(tags));
tagList.remove(tagList.size()-1);
return Flux.fromIterable(tagList);
}
Though the test is passed, but no data is saved. I tried debugging and found convertToPostTags is called once for the first Post object, then nothing happens.
I also tried to move the logic in a service class and used @Transactional annotation, but no success.