-
1. Re: Does CDI container takes care of calling event observer method on server stop or crash
manovotn Jan 25, 2019 8:39 AM (in response to gandhirajan)Hello,
if you could attach some code samples of how does the event/observer chain look like in your case, that would make it easier to try and suggest a solution.
E.g. what is in transaction, how do you connect JPA event to a CDI event and ultimately to JMS etc.
But going just from the verbal description...
What you are likely looking for is known as graceful shutdown and you can read more about it here, in WFLY docs.
The docs list supported subsystems and CDI is not amongst them (CDI observers are just in memory, so there can be no recovery on server shutdown/restart).
However, WFLY has some support for transactions, so if you could fit the whole behaviour into a transaction, you should theoretically get graceful shutdown.
-
2. Re: Does CDI container takes care of calling event observer method on server stop or crash
gandhirajan Jan 25, 2019 10:31 AM (in response to manovotn)HI Matej, Thanks a lot for your response. My basic issue is that I m trying to perist data in DB and send JMS message to an other service within one JTA transaction. Pseudo code as follows:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public method_A () {
// code to persist data
// code to send JMS message
}
But the problem is that the second service consumes the message before even the persistence is completed by first service resulting in problem. Hence I tried to work around the problem using CDI events as follows:
@Inject
@Transaction
private Event<Item> itemEvent;
public void handleItem(Item item) {
//code to persist the item
itemEvent.fire(item);
}
@Asynchronous
public void observeAfterTransactionCompletion(@Observes(during = TransactionPhase.AFTER_SUCCESS) @Transaction Item item) {
//code to send JMS message to the second service
}
This approach works fine but it may fail during Jboss crash or shutdown. I Just need to ensure that the observer does not miss out on any events to fire JMS message. Please advice. You can also refer my post here - https://stackoverflow.com/questions/54343688/cdi-event-observer-handling-on-server-crash-and-restart
Thanks in advance
-
3. Re: Does CDI container takes care of calling event observer method on server stop or crash
manovotn Jan 28, 2019 8:59 AM (in response to gandhirajan)So, looking at your public method_A () I think the problem why you got the msg to JMS faster then the persistance is the fact that persistence usually goes through a cash that flushes once in a time.
You could enforce that via EntityManager#flush() method and probably achieve your desired behaviour completely omitting CDI. Have you tried that?
As a side node - in the code with CDI, you are using @Asynchronous annotation which is an EJB annotation, not CDI. This will likely have zero effect on your actual code.
Browse EJB spec for when you can use this annotation and on what methods.
-
4. Re: Does CDI container takes care of calling event observer method on server stop or crash
gandhirajan Jan 29, 2019 12:03 AM (in response to manovotn)Hi Matej, Thanks again for the response. As mentioned in your post, we have already placed "entityManager.flush()" code next to "entityManager.persist(data)". But the problem still exists unfortunately. For now we have ruled out CDI events as they are not persistent in case of failures. Please let me know if there are any other ways to resolve this race condition of message consumption before data persistence.
Thanks in advance.
Regards,
Gandhi
-
5. Re: Does CDI container takes care of calling event observer method on server stop or crash
manovotn Jan 29, 2019 3:58 AM (in response to gandhirajan)To be fair, I am not that familiar with how you could bypass this limitation.
You might want to ask around in WildFly part of these forums - WildFly
Or maybe try WildFly mailing list, whichever you prefer - wildfly-dev@lists.jboss.org
-
6. Re: Does CDI container takes care of calling event observer method on server stop or crash
gandhirajan Jan 30, 2019 4:13 AM (in response to manovotn)Thanks for the response Matej. Will check out in WildFly forum.
Regards,
Gandhi