| Author |
Application Exception in MDB
|
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
|
|
Hello
Can an MDB throw an application exception ?
Thanks
|
"Know where to find the solution and how to use it - that's the secret of success."
|
 |
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
|
|
|
any help on above post?
|
 |
Krzysztof Koziol
Ranch Hand
Joined: Nov 19, 2006
Posts: 133
|
|
Amirr Rafique wrote:Hello
Can an MDB throw an application exception ?
Thanks
The onMessage() method can throw only RuntimeExceptions therefore bean provider may throw an @ApplicationException which is the java.lang.RuntimeException.
|
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCEA/OCMJEA 5.0
|
 |
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
|
|
I have seen couple of question about MDB throwing ApplicationException and as per my developed understanding MDB should not throw Application Exception as there is no client to receive this exception ....
Guys can any one please give any reference from specs
|
 |
Lee Kian Giap
Ranch Hand
Joined: Jan 23, 2008
Posts: 210
|
|
EJB Core 14.3.4
MDB method throw Application Exception
and container re-throw Application Exception to resource adapter
|
SCJP 6, SCWCD 5, SCBCD 5
|
 |
Lee Kian Giap
Ranch Hand
Joined: Jan 23, 2008
Posts: 210
|
|
I think whether you can throw an exception is not decided by the type of bean
the term "Application Exception" in EJB 3 means :
(1) "Checked Exception" (i.e subclass of Exception) may or may not defined using @ApplicationException
OR
(2) "Unchecked Exception" (i.e subclass of RuntimeException or Error) , only RuntimeException can be defined as an application exception using @ApplicationException (otherwise it is a system exception if not defined using @ApplicationException)
BUT cannot be subclass of java.rmi.RemoteException
================
whether you can throw an exception is decided by the basic rules of exception handling
Rules:
(1) Checked Exception
~ either declare it in the throws clause of your method or catch it in the body of your method
(2) RuntimeException which is defined as an application exception using @ApplicationException
~ either declare it in the throws clause of your method or catch it in the body of your method
(3) RuntimeException which is not defined as an application exception
~ is a normal RuntimeException which not need to be declare in throws clause or catch
(4) RemoteException
~ if your bean interface using @Remote without extends java.rmi.Remote interface, method define in the bean interface does not need to declare RemoteException in the throws clause. In this case, container throws EJBTransactionRollbackException or EJBException depends on the transaction context (refer to EJB Core spec 14.3)
~ if your bean interface extends java.rmi.Remote interface, method define in the bean interface MUST declare RemoteException in the throws clause. In this case container throws TransactionRollbackException or RemoteException depends on the transaction context (refer to EJB Core spec 14.3)
================
so what you should not confuse is the mess between
(a) bean method can throw which exception ?
(b) container can throw which exception ?
================
The onMessage() method can throw only RuntimeExceptions therefore bean provider may throw an @ApplicationException which is the java.lang.RuntimeException.
I can confirm that onMessage() can throw Unchecked Exception (RuntimeException and Error) , since this type of exception does not need to be declare in throws clause of onMessage() and does not need to be catch
From my studying, if a RuntimeExceptions that is defined as application exception using @ApplicationException, that exception need to be either declare in the throws clause of the bean method or catch in the bean method. As referring to the rules of exception handling that "you cannot throw any checked Exceptions other than those declared in the method you are overriding", so in your bean class which override the onMessage() method cannot declare additional exception in the throws clause, so if inside the onMessage() method do receive a ducking application exception from the method invoked from onMessage() method, it need to be catch. Am I right ?
|
 |
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
|
|
From my studying, if a RuntimeExceptions that is defined as application exception using @ApplicationException, that exception need to be either declare in the throws clause of the bean method or catch in the bean method. As referring to the rules of exception handling that "you cannot throw any checked Exceptions other than those declared in the method you are overriding", so in your bean class which override the onMessage() method cannot declare additional exception in the throws clause, so if inside the onMessage() method do receive a ducking application exception from the method invoked from onMessage() method, it need to be catch. Am I right ?
I think here you are referring to exceptions extending java.lang.Exception class and marked as @ApplicationException. As any exceptin extending RuntimeException does not need to be declare in throws clause
Please confirm
|
 |
Lee Kian Giap
Ranch Hand
Joined: Jan 23, 2008
Posts: 210
|
|
I think here you are referring to exceptions extending java.lang.Exception class and marked as @ApplicationException. As any exceptin extending RuntimeException does not need to be declare in throws clause
Please confirm
Under EJB 3, for exception which extends java.lang.Exception class by default is confirmed as an Application Exception no matter you marked it using @ApplicationException.
Confirm that any exceptin extending RuntimeException does not need to be declare in throws clause, BUT any difference for a "RuntimeException which is marked as @ApplicationException" ?
Code Snippet 1:
Code Snippet 2:
Any differences in the above two code snippet , what I confuse is ... is it a MUST to throws/catch ALL application exception no matter it is a "Checked Exception" or "RuntimeException defined using @ApplicationException" ?
So , in Code Snippet 1 , the throws clause does not need to have MyException2.
How bout Code Snippet 2, does the throws clause need to have MyException2 ?
|
 |
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
|
|
My understanding is for RuntimeException, it is not required to be mentioned in throws clause whether it is annotated as @ApplicationException or not
Please correct me
|
 |
Lee Kian Giap
Ranch Hand
Joined: Jan 23, 2008
Posts: 210
|
|
My understanding is for RuntimeException, it is not required to be mentioned in throws clause whether it is annotated as @ApplicationException or not
Please correct me
I will write some code to test it out in glassfish application server, but might need some time to install my environment in virtual machine ... so might reply you tomorrow.
or you might test it if you have the environment on place and let me know the answer, appreciate ~
|
 |
Krzysztof Koziol
Ranch Hand
Joined: Nov 19, 2006
Posts: 133
|
|
Amirr Rafique wrote:My understanding is for RuntimeException, it is not required to be mentioned in throws clause whether it is annotated as @ApplicationException or not
Please correct me
Right. RuntimeException is not required to be defined in a throws clause.
|
 |
Bernd Peter Murlasits
Greenhorn
Joined: Mar 07, 2010
Posts: 1
|
|
As per section 5.4.17 of EJB 3.0 Core specification, a message-driven bean’s message listener method must not throw the java.rmi.RemoteException. It can throw any other exception. If it throws an application exception marked with rollback, the transaction (if any) is rolled back, the exception is rethrown to the resource adapter, and the bean is NOT discarded. If it throws a system exception, the exception is logged, the transaction (if any) is rolledback, and the bean is discarded.
note: if you wanna let the message listener method of a MDB throw any application-exception (inherited from java.lang.Exception) you must specify the messageListenerInterface via annotion (instead of just let the mdb-class implement MessageListener:
|
 |
 |
|
|
subject: Application Exception in MDB
|
|
|