An EJB initialization template that depends on another EJB application

How to initialize an EJB when it depends on another application that is in another cluster that is not already running?

How can I do it?

  • @PostConstruct . I may be able to loop until the dependent EJB is available, but I am afraid that it will delay or block the process of loading the server.
  • @Schedule : it is possible to plan the initialization process to avoid blocking on the server, and then service requests only if init is completed, otherwise print an error.

How do you think I should continue? Would you recommend me a template?

+5
source share
2 answers

All of the above suggestions are valid, I think. I think this is a matter of circumstance around your problem.

Depending on whether you can start A yourself, you can use a third element, such as Gas, to ensure that A does not appear (and does not work or gets stuck) if B is not ready.

If, on the other hand, A starts automatically and you cannot change it, it depends on whether you can control when the initialization process occurs. If you plan to initialize the entire dependency chain, and it should work, but if you don’t know or cannot control when B enters the network and A starts no matter what, then there is no choice but to interrogate until B will be up.

Personally, I saw that the survey is not so bad as long as the expected one is available, it starts quickly or quickly recovers in the event of a failure.

On the other hand, can you control how clusters begin their configuration? You could avoid this problem by first creating the first application cluster.

+2
source

Even if you manage to resolve the EJB initialization, you will create dependencies. Architecturally speaking, the dependence on initialization cancels the reason why it was decided to separate application No. 1 (gateway) and application No. 2 (service host).

An alternative suggestion is to keep them independent as they are at present, and instead rely on exception handling. If the service in application No. 2 is unavailable, you can throw a custom exception, for example, "The service is unavailable, try again later" or, depending on the need, combine requests that will be executed when the service is available again.

It also protects you from application crash No. 2 after starting, for ex if it does not work or does not respond due to some internal errors.

+1
source

Source: https://habr.com/ru/post/1208789/


All Articles