6262import org .eclipse .microprofile .config .inject .ConfigProperty ;
6363import org .jboss .logging .Logger ;
6464import org .neo4j .driver .Driver ;
65+ import org .neo4j .driver .reactive .RxSession ;
6566import org .reactivestreams .FlowAdapters ;
6667import org .reactivestreams .Processor ;
6768
@@ -86,6 +87,10 @@ public class ImportService {
8687
8788 @ Inject protected SourceService sourceService ;
8889
90+ static Uni <Void > sessionFinalizer (RxSession session ) {
91+ return Uni .createFrom ().publisher (session .close ());
92+ }
93+
8994 public Uni <String > runImport (UUID domainId ) {
9095
9196 if (pipelineMap .containsKey (domainId )) {
@@ -155,7 +160,6 @@ private Cancellable startImportTask(UUID domainId, Domain domain, Graph graph) {
155160 private MultiFlatten <JsonNode , Long > createImportTask (
156161 UUID domainId , Domain domain , String cypher , Processor <List <String >, JsonNode > publisher ) {
157162 var counter = new AtomicLong (1L );
158- var session = driver .asyncSession ();
159163 return Multi .createFrom ()
160164 .publisher (publisher )
161165 .emitOn (Infrastructure .getDefaultWorkerPool ())
@@ -165,19 +169,36 @@ private MultiFlatten<JsonNode, Long> createImportTask(
165169 var entry =
166170 new ObjectMapper ()
167171 .convertValue (node , new TypeReference <HashMap <String , Object >>() {});
168- var s = driver .asyncSession ();
169172 return Uni .createFrom ()
170- .completionStage (
171- s .runAsync (cypher , Map .of ("model" , entry , "domainId" , domainId .toString ()))
172- .exceptionally (
173- e -> {
174- log .errorf (
175- " Failed item import in file: %s on lines: %s msg: %s" ,
176- domain .getFile (), node .get ("lines" ), e .getMessage (), e );
177- return null ;
178- })
179- .thenCompose (x1 -> session .closeAsync ())
180- .thenApply (v -> counter .getAndIncrement ()));
173+ .emitter (
174+ emitter ->
175+ Multi .createFrom ()
176+ .resource (
177+ driver ::rxSession ,
178+ session ->
179+ session .writeTransaction (
180+ tx ->
181+ tx .run (
182+ cypher ,
183+ Map .of (
184+ "model" ,
185+ entry ,
186+ "domainId" ,
187+ domainId .toString ()))
188+ .records ()))
189+ .withFinalizer (ImportService ::sessionFinalizer )
190+ .map (v -> counter .getAndIncrement ())
191+ .onFailure ()
192+ .invoke (
193+ (throwable ) ->
194+ log .errorf (
195+ " Failed item import in file: %s on lines: %s msg: %s" ,
196+ domain .getFile (),
197+ node .get ("lines" ),
198+ throwable .getMessage (),
199+ throwable ))
200+ .subscribe ()
201+ .with (emitter ::complete ));
181202 });
182203 }
183204
0 commit comments