Bonjour à tous
J'ai récemment eu un souci avec les RuntimeException dans un projet Java EE. Le fait est que si un EJB en référence un autre (via @EJB) et que ce dernier lance une RuntimeException, toutes les injections @EJB sautent !
Voici l'exemple utilisé :
Code : Sélectionner tout - Visualiser dans une fenêtre à part public class ProcessorBean { public String process() { throw new RuntimeException("error"); } } /* This is a sub-bean that throws a simple RuntimeException */
Code : Sélectionner tout - Visualiser dans une fenêtre à part public class OtherBean { public String hello() { return "processed by OtherBean"; } } /* Another sub-bean to test the catch-call (optional) */Afin de palier au problème, nous avons pensé à la solution suivante : utiliser les annotations pour faire du pré-processing avant la compilation. L'idée est d'encapsuler chacune des méthodes des classes annotées par un gros try/catch et de relancer une RemoteException (qui n'est pas une RuntimeException).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class TestBean { @EJB private ProcessorLocal processor; @EJB private OtherLocal other; public String process() { String result = null; try { result = processor.process(); //will throws a RuntimeException } catch(Exception e) { result = other.hello(); //CRASH !!!! --> afther that processor.process() trhows a RuntimeException, all injections @EJB are down ! There will be a ejb.some_unmapped_exception written in the console. } return result; } }
J'aimerais si possible avoir quelques retour la-dessus. Est-ce que quelqu'un a déjà eu ce problème ? Comment l'a-t-il contourné ? Est-ce que notre idée semble correcte ?
Merci d'avance !
@++
Partager