This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Life-Cycle Event Interceptor Method Skipped

 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
In my experiments I have noticed that a @PostConstruct method in my EJB class ceases to be invoked when I define a @PostConstruct method in a default interceptor class in the same web application.
I have studied the EJB 3.1 specifications in detail but haven't found anything that indicates that this is the behaviour I should expect.
On the contrary, I get the feeling that the default interceptor's @PostConstruct method should be invoked first and the @PostConstruct method in the EJB class be invoked later.
I am using GlassFish v3.0.1 for my experiments.
Anyone have any comments or insight on the above?
Thanks in advance!
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Krizsan wrote:

On the contrary, I get the feeling that the default interceptor's @PostConstruct method should be invoked first and the @PostConstruct method in the EJB class be invoked later.



Assuming the bean and interceptor methods are correct, you are right - that's how it's supposed to work. The exact details are in the "Interceptors 1.1 spec" section "Multiple Callback Interceptor Methods for a Life Cycle Callback Event"


 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivan

The interceptor life-cycle methods are called before the bean one.
Take a look in the specification on the 12.2 Interceptor Life Cycle


Both the interceptor instance and the bean instance are created or activated before any of the respective PostConstruct or PostActivate callbacks are invoked. Any PreDestroy and PrePassivate
callbacks are invoked before the respective destruction or passivation of either the bean instance or interceptor instance.




This is from 3.0 specs, but I bet that in 3.1 is the same.


Regards,
Mihai
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Mihai Radulescu wrote:The interceptor life-cycle methods are called before the bean one.


Yes, I know. The problem in this case is that one of the life-cycle methods is never invoked.
I have been a little busy, as of lately, with other things so I haven't had time to investigate further.
I suspect that the fact that one of the life-cycle methods is configured using an annotation and the other is configured using the ejb-jar.xml deployment descriptor may have cause the strange behaviour.
Thanks for your help!
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


I suspect that the fact that one of the life-cycle methods is configured using an annotation and the other is configured using the ejb-jar.xml deployment descriptor.



Take care the dd overwrites the annotations. This can be the reason why the method is only once called.

Regards,
Mihai
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Finally I had some time and inspiration to look into this problem.
I found that I made two mistakes in my code:
1. InvocationContext.proceed() must be invoked in a life-cycle event interceptor method in a default interceptor.

My failure to invoke proceed() resulted in the post-construct life-cycle event interceptor methods in subsequent interceptors, for instance in the EJB class or in other (non-default) interceptor classes, never being invoked.

2. A default interceptor need only to be registered in the ejb-jar.xml deployment descriptor.
This even though it contains an annotated post-construct interceptor method (as in the above example).
The mistake I made was to include it in the interceptors list in the @Interceptors annotation on the bean class as well as in the ejb-jar.xml deployment descriptor.
This resulted in the above interceptor method being invoked twice.
Hope this helps someone!
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the classical "broke of interceptor chain" failure.
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic