-
I am having difficult determining how to do this in the "reactive" way. I understand I need to be running the task on the Vert.x event loop so it has to be annotated with @nonblocking or return Uni, but what does the call to the Hibernate Panache repo or the PanacheEntity item look like?
I'm certain that isn't right, but I've also tried every combination of I'm sure there's a simple pattern that I'm missing - enlighten me. I just want to read some things from the database, do a bunch of work on them, and the write them back. It's a fairly straightforward set of tasks. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 3 replies
-
/cc @DavideD, @Sanne, @gavinking, @mkouba |
Beta Was this translation helpful? Give feedback.
-
I'll let the hibernate guys to answer the "reactive way" question but I believe that something like this should work just fine: @Scheduled(every = "10s")
Uni<Void> findItems() {
return MyItem.<MyItem>listAll().onItem().invoke( myItems -> {
System.out.println(myItem.size());
}).chain(ignored -> Uni.createFrom().voidItem());
} What version of quarkus do you use? |
Beta Was this translation helpful? Give feedback.
-
@mkouba answer is correct, but you can simplify it a bit:
@MWatter, Your original solution doesn't work because you are not chaining the unis. This means that when findAll returns the void, listAll might not been executed yet. The session gets closed when the method returns and so when the query actually runs you have the session closed error. |
Beta Was this translation helpful? Give feedback.
-
I'm closing this issue but feel free to ask more questions or reopen it if something is not clear. |
Beta Was this translation helpful? Give feedback.
-
I would rewirte it as something like this: @Scheduled(every = "10s")
Uni<Void> findItems() {
return MyItem.<MyItem>listAll()
.invoke( this::printSize )
.replaceWithVoid();
}
private void printSize(List<MyItem> list) {
System.out.println( list.size() );
} But it is a question of taste. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the suggestions. I'll give them a shot this morning. |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, as I've seen in a few other situations, the above suggestions all result in
At this point, this is probably outside the scope of Quarkus and now well within the Hibernate area, so I'll start looking for answers over there... or perhaps just switch to imperative. Thank you for the help. |
Beta Was this translation helpful? Give feedback.
-
I doubt this is something related to Hibernate Reactive. If you can push a test project somewhere, we can have a look. |
Beta Was this translation helpful? Give feedback.
-
I've adapted one of the quickstarts to test that it actually works. You can find the project here: https://github.com/DavideD/quarkus-quickstarts/tree/scheduler-latest/hibernate-reactive-panache-quickstart Running |
Beta Was this translation helpful? Give feedback.
I've adapted one of the quickstarts to test that it actually works. You can find the project here: https://github.com/DavideD/quarkus-quickstarts/tree/scheduler-latest/hibernate-reactive-panache-quickstart
Running
mvn quarkus:dev
will start the project and print the list size on the terminal.The bean with the scheduler is here: https://github.com/DavideD/quarkus-quickstarts/blob/scheduler-latest/hibernate-reactive-panache-quickstart/src/main/java/org/acme/hibernate/orm/panache/scheduler/CounterBean.java