Tuesday, September 21, 2010

EJB.next Interceptor Improvements - Method signatures

Interceptors are great, but some parts of them are odd when applied practically. For as fundamentally reflective as interceptors are, it isn't possible to pass all interceptable calls through a single method, like so:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.interceptor.AroundInvoke;
import javax.interceptor.AroundTimeout;
import javax.interceptor.InvocationContext;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;

public class StatsInterceptor {

    @PostConstruct
    @PreDestroy
    @PrePassivate
    @PostActivate
    @AroundInvoke
    @AroundTimeout
    public Object intercept(InvocationContext context) throws Exception {
        final long start = System.nanoTime();
        try {
            return context.proceed();
        } finally {
            final long nanos = System.nanoTime() - start;
            final long millis = MILLISECONDS.convert(nanos, NANOSECONDS);
            System.out.println("invoke time "+ millis +" milleseconds");
        }
    }
}

This looks very nice, but currently is not legal. The spec currently requires different formats for callback-based interception vs business-method-based interception. This sounds like a nice safe and conservative rule, but is it benefiting users? Not really. Let's see what we have to do to our interceptor to make it compliant.

First, callback intercept methods are not allowed to return anything and must have a void return type. The logic here is that since callbacks have no return value, interceptors should know this. So fine, let's work around that and just make a new method:

    @PostConstruct
    @PreDestroy
    @PrePassivate
    @PostActivate
    @AroundInvoke
    public void callback(InvocationContext context) throws Exception {
        intercept(context);
    }

Second, callback intercept methods are not allowed to throw any exceptions. Again the rationale is that callbacks aren't allowed to throw checked exceptions, so interceptors should be aware of that as well. The trick is we can't simply remove the 'throws Exception' clause without doing any extra work. The InvocationContext.proceed() method throws java.lang.Exception but we can't, so we are forced to take responsibility for it. If we just catch and ignore the exception all hell will break loose, so instead we should at least wrap it as a RuntimeException.

    @PostConstruct
    @PreDestroy
    @PrePassivate
    @PostActivate
    @AroundTimeout
    public void callback(InvocationContext context) {
        try {
            intercept(context);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

We've just made a mistake. Did you see it? If there are several interceptors in the chain and all of them wrap the exception like this, when an actual exception is thrown it will end up wrapped N times -- once per interceptor -- before finally reaching the container. Instead, we should at least check if the exception needs wrapping and hope that other interceptors do the same:

    @PostConstruct
    @PreDestroy
    @PrePassivate
    @PostActivate
    @AroundTimeout
    public void callback(InvocationContext context) {
        try {
            intercept(context);
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else{
                throw new RuntimeException(e);
            }
        }
    }

Now we're done. Summary: all of the above is boilerplate code you need in any well implemented callback-based interceptor.

What have we achieved? Not much.

In terms of user code, it doesn't help you any to have these restrictions. If you wanted special logic for callback-based interceptor methods you can easily put that annotation on a separate method.

In terms of container code, there's little gain as well. Ultimately, most containers use reflection to kick off the interceptor chain. The java.lang.reflect.Method.invoke() method returns Object and throws InvocationTargetException. Restricting users from returning Object and throwing Exception have no real impact on the container code. It's a callback. Ultimately all exceptions, checked or not, go into the log file and no further and return values are simply ignored.

Summary: The restriction on returning void and throwing Exception should be lifted. For backwards compatibility we can just say this former requirement is now optional and allow interceptor signatures of 'Object <method>(InvocationContext c) throws Exception' even if what is being intercepted is a callback.

47 comments:

Narender Godara said...

Packers and movers Jalandhar
Packers and movers Jammu
Packers and movers Jamnagar
Packers and movers Jodhpur

Arjun Singh said...

They are constant in name of Packers and Moving organizations and they are eye-catching shifting useful items from one’s existing position to another.
Bangalore Packers and Movers
Hyderabad Packers and Movers
Mumbai Packers and Movers
Pune Packers and Movers

Arjun Singh said...

Well, relocations are done in a able and comfortable way. To provide relocations, Expert5th.in Packers and Moving organizations are putting their awesome projects. Chennai Packers and Movers
Delhi Packers and Movers
Gurgaon Packers and Movers
Noida Packers and Movers

Chitransh Andola said...

Packers and Movers India
Packers and Movers in Bangalore
Packers and Movers in Delhi
Packers and Movers in Hyderabad
Packers and Movers in Mumbai
Packers and Movers in Chennai
Packers and Movers in Pune
Packers and Movers in Noida
Packers and Movers in Gurgaon
Packers and Movers in Ghaziabad
Packers and Movers in Faridabad

Chitransh Andola said...

Movers and Packers in Bangalore
Movers and Packers in Delhi
Movers and Packers in Hyderabad
Movers and Packers in Chennai

amine lahragui said...

Your work is very good and I appreciate you and hopping for some more informative posts. happy new year 2016 new year 2016 bonne année 2016 happy new year 2016 images feliz año nuevo 2016 imagenes de año nuevo 2016 feliz año 2016

Harish Yes said...

Thank you for the wonderful effort on this post. packers and movers bangalore packers and movers bangalore marathahalli packers and movers marathahalli packers and movers hsr layout packers and movers btm layout packers and movers bommanahalli packers and movers whitefield packers and movers koramangala packers and movers jp nagar

Mary Brown said...

Java EE article is practical oriented and real time examples. How Java EE address the enterprise development is very important. for that you need a practical orieneted Java Training you need.

Online Java Training
Online Java Training from India
Java Training Institutes in Chennai
Java Training in Chennai
Java Training

Making Move said...

With best compliments from
https://www.makingmove.com/
Packers and Movers
Packers and Movers Ahmedabad
Packers and Movers Bangalore
Packers and Movers Chandigarh
Packers and Movers Chennai
Packers and Movers Delhi
Packers and Movers Hyderabad
Packers and Movers Kochi
Packers and Movers Kolkata
Packers and Movers Mumbai
Packers and Movers Pune
Movers and Packers India
Car Transportation
Two Wheeler Transportation

Ranveer Patil said...

This is a very good blog written by you.This blog gives me good information.Really this blog was very instructive.I really appreciate your blog. Thanks..........................
Bookmyshow T20 Cricket World Cup Tickets Buy
ICC T20 World Cup 2016 Qualifier Venues Groups

Packers Movers Hyderabad said...

I really enjoyed reading your blog, you have lots of great content.Please visit here:
http://packersmovershyderabadcity.in/
We are expert in Handling, Movers Packers in Hyderabad and Packing & Shifting of Household Goods, Office goods, Commercial Equipment and Car, Logistics and Transportation.
Packers And Movers Hyderabad

aarohi sharma said...



WWE PPV main events were rocking in 2015, hope this year it will rocks again, like all previous years all events were perfect now hope so this year also. It begins with royal rumble, then next WWE Fastlane 2016 live stream will be available on internet.
Fastlane 2016 Results
Fastlane 2016 Live Stream
WWE Fastlane 2016 Live


Really Horror Show American Horror Story, i ever watched, here are some more interesting tv shows like the walking dead, arrow, Breaking Bad, Game of Thrones Season 6 Online we can stream, After watching these tv shows one can feel interested more an excited more.
Game of Thrones Season 6
Game of Thrones Season 6 Online
Watch Game of Thrones Season 6
Watch Game of Thrones Season 6 Online
Watch Game of Thrones Online
Watch Game of Thrones Online Free

I watched this event, really very much interesting scenes created while selecting Miss Universe 2015. But here is one more .big event going to held Academy Awards 2016 here is direct link for this event.
Academy Awards 2016 Nominations
Oscar Awards 2016 Nominations
Academy Awards 2016
Oscar Awards 2016
Academy Awards 2016 Live
Oscar Awards 2016 Live
Academy Awards 2016 Live Stream
Oscar Awards 2016 Live Stream
Academy Awards 2016 Winner
Oscar Awards 2016 Winner

All episodes of Torrent The Walking Dead Season 6 series are available here for viewers. These episodes can be watched online in HD Quality Like 1080 PX or 4K.
The Walking Dead Season 6
Walking Dead Season 6
The Walking Dead Season 6 Online
Walking Dead Season 6 Online
Watch The Walking Dead Season 6
Watch Walking Dead Season 6

Emily Walker said...

Thank you very much, the post is really helpful super smash flash 2 | happy wheels demo | gmail sign in | hotmail sign in | unblocked games | lightsaber escape | gmail sign up | gmail account login | gmail login | cool math games

Prashant Thite said...

Fastlane 2016
WWE Fastlane 2016
Fastlane 2016 Live Stream
WWE Fastlane 2016 Live Streaming

Vinod Singh said...

Packers and Movers in Hyderabad
Packers and Movers in Pune
Packers and Movers in Bangalore

Vinod Singh said...



Packers and Movers in Gurgaon
Packers and Movers in Delhi
Packers and Movers in Mumbai

asa akter said...

T20 CRICKET WORLD CUP 2016

Tahir Bahi said...

Hello my friend! I want to say that this post is awesome, great written and include almost all
important infos.I really love this post I will visit again to read your post in a very short time and I hope you will make more posts like this.

T H A N K S
F O R
S H A R I N G

WinAVI Video Converter 11.5 Keygen
Trend Micro Antivirus
Movie Converter 7 Keygen
Neuratron Audio Score Ultimate Keygen

Computaholics said...

Thanks for great post
opengl
ipl 9
Hdfs Commands
love shayari

Shubham patel said...

Hey guys!! also check the all latest updates of Vivo IPL t20 2016 live streaming-

Vivo ipl t20 live on Sony Max

Sweta Sehgal said...



game of thrones season 6 online

hbo game of thrones season 6 online

hbo game of thrones season 6 full episodes


AMC walking dead season 7 online

The jungle book 2016 full movie online

Batman v Superman: Dawn of justice full movie online

Watch Deadpool Full Movie online

Deadpool Full Movie dvd

Sweta Sehgal said...



game of thrones season 6 episodes 1 online

captain america civil war full movie online

captain america 3 civil war full movie online

captain america civil war full movie online

captain america 3 civil war full movie online



X-Men: Apocalypse full movie online

civil war full movie

https://youtu.be/csHbEOTgJqA

mitchel lee said...

Its Been 10 long weeks and i'v just watched the last episode of Game Of Thrones, ending season 5 in a dramatic way. Now all i have to do is wait a billion years for season 6, which every GOT fan hates! Lets go over some of my favorite & memorable parts of season 5.

Game of Thrones Season 6
Game of Thrones Season 6 Streaming
Watch Game of Thrones Season 6 Online
Watch Game of Thrones Season 6 Streaming
Watch Game of Thrones Season 6 Full Episode
Game of Thrones Season
Game of Thrones Season 6 Streaming
Watch Game of Thrones Season 6 Online
Watch Game of Thrones Season 6 Streaming
Watch Game of Thrones Season 6 Full Episode

Watch Movie Free, Download Movies Free, Free Online Movies 2016

Watch Movies Online
Download Movies Free
Watch Elvis & Nixon Online
Watch Ice Age: Collision Course Online
Watch Pete’s Dragon Online
Watch Hardcore Henry Online
Watch Louder Than Bombs Online
Watch The Jungle Book Online
Watch Barbershop: The Next Cut Online
Watch Criminal Online
Watch Deadpool Online
Watch Zootopia Online
Watch Kung Fu Panda 3 Online
Watch The Divergent Series: Allegiant Online
Watch Batman v Superman: Dawn of Justice Online
Watch God’s Not Dead 2 Online
Watch My Golden Days Online

Sankalp Jain said...

BlueDart Tracking
Happy Diwali SMS
happy 4th of july
French Open Live French
Halloween Props

Duy Kieu said...

All fixed set patterns are incapable of adaptability or pliability. The truth is outside of all fixed patterns.
- quotes about love
- snake game
- chinese food near me
- panama papers
- Fancam Kpop

sushilkr said...


Packers and Movers Bangalore
Packers and Movers Hyderabad
Packers and Movers Ahmedabad
Packers and Movers Ambala
Packers and Movers Amritsar
Packers and Movers Bhatinda

sushilkr said...


Packers and Movers Gurgaon
Packers and Movers Delhi
Packers and Movers Chandigarh
Packers and Movers Dehradun
Packers and Movers Faridabad
Packers and Movers Surat

sushilkr said...


Packers and Movers Pune
Packers and Movers Mumbai
Packers and Movers Hisar
Packers and Movers Jalandhar
Packers and Movers Jamnagar
Packers and Movers Karnal

Raja Hindustani said...

Sultan Film Review Rating
Sultan Worldwide Box Office Expectation

Sonia Sharma said...

Technology Wow

Sonia Sharma said...

Cafe How To

Sonia Sharma said...

Episode 9

appu said...


RRB NTPC Result
RRB NTPC Admit Card
RRB NTPC 2016 Sylabus
RRB NTPC 2016
RRB NTPC Result
RRB NTPC Result 2016
SSC CGL Result 2016
Happy New Year 2017 Wishes
IBPS PO 2016 Result
GCSE Results 2016
SBI PO Result 2016
NEET 2016 Result
NTPC Result 2016
New Year 2017 Wallpaper
Good Morning Quotes
Merry Christmas wishes Christmas wishes
Happy Birthday Wishes
Happy Birthday Messages
Good Morning Quotes

Zheng junxai5 said...


zhengjx20160804
michael kors outlet clearance
cheap ray ban sunglasses
tory burch outlet online
ray bans
christian louboutin wedding shoes
montblanc pens
louis vuitton purses
nike roshe one
coach factory outlet online
coach outlet online
polp ralph lauren
louis vuitton outlet online
louis vuitton
oakley sunglasses
air jordan 11
burberry outlet
oakley sunglasses wholesale
longchamp outlet
michael kors handbags
true religion jeans
louis vuitton outlet
hermes birkin handbags
christian louboutin outlet
coach factory outlet
ray ban sunglasses uk
louis vuitton
montblanc pen
louis vuitton handbags
coach outlet online
michael kors handbags
tiffany and co
michael kors outlet clearance
louis vuitton purses
coach outlet
michael kors bags
christian louboutin outlet
jordan retro 11
toms shoes
michael kors outlet

Zheng junxai5 said...


zhengjx20160804
michael kors outlet clearance
cheap ray ban sunglasses
tory burch outlet online
ray bans
christian louboutin wedding shoes
montblanc pens
louis vuitton purses
nike roshe one
coach factory outlet online
coach outlet online
polp ralph lauren
louis vuitton outlet online
louis vuitton
oakley sunglasses
air jordan 11
burberry outlet
oakley sunglasses wholesale
longchamp outlet
michael kors handbags
true religion jeans
louis vuitton outlet
hermes birkin handbags
christian louboutin outlet
coach factory outlet
ray ban sunglasses uk
louis vuitton
montblanc pen
louis vuitton handbags
coach outlet online
michael kors handbags
tiffany and co
michael kors outlet clearance
louis vuitton purses
coach outlet
michael kors bags
christian louboutin outlet
jordan retro 11
toms shoes
michael kors outlet

Hua Cai said...

celine outlet
longchamp handbags
louis vuitton outlet stores
nike huarache
ray ban sunglasses
nike outlet store online
ray ban sunglasses sale
gucci outlet
louis vuitton outlet
ysl outlet online
polo ralph lauren
ralph lauren uk
coach outlet
louis vuitton handbags
lebron shoes
coach outlet online
ray ban sunglasses
tory burch outlet online
ferragamo outlet
air max 90
chicago blackhawks
tory burch outlet
michael kors outlet clearance
tory burch shoes
yeezy boost 350
true religion outlet
puma outlet
cheap uggs
nike outlet online
true religion uk
tiffany jewelry
cheap nike shoes sale
celine outlet online
michael kors outlet
tory burch outlet online
20160812caihuali

Hua Cai said...

celine outlet
longchamp handbags
louis vuitton outlet stores
nike huarache
ray ban sunglasses
nike outlet store online
ray ban sunglasses sale
gucci outlet
louis vuitton outlet
ysl outlet online
polo ralph lauren
ralph lauren uk
coach outlet
louis vuitton handbags
lebron shoes
coach outlet online
ray ban sunglasses
tory burch outlet online
ferragamo outlet
air max 90
chicago blackhawks
tory burch outlet
michael kors outlet clearance
tory burch shoes
yeezy boost 350
true religion outlet
puma outlet
cheap uggs
nike outlet online
true religion uk
tiffany jewelry
cheap nike shoes sale
celine outlet online
michael kors outlet
tory burch outlet online
20160812caihuali

Meiqing Xu said...

louis vuitton outlet
true religion outlet
nike air force 1
michael kors outlet
ugg boots
christian louboutin shoes
louis vuitton handbags
rolex daytona
converse outlet
coach outlet
20161208caiyan

Unknown said...

coach factory outlet
gucci bags
cartier watches
mac cosmetics sale
cheap nba jerseys
coach outlet
michael kors factory outlet
prada sunglasses
michael kors outlet
coach outlet online coach factory outlet
chanyuan2017.01.7

Unknown said...

coach factory outlet
gucci bags
cartier watches
mac cosmetics sale
cheap nba jerseys
coach outlet
michael kors factory outlet
prada sunglasses
michael kors outlet
coach outlet online coach factory outlet
chanyuan2017.01.7

Johnabraham said...

Great blog here with all of the valuable information you have. Keep up the good work you are doing here.
Easter Quotes
propose day quotes
chocolate day 2017
kiss day messages
teddy day images
happy valentines day quotes
Father's Day 2017

Thomas pedro said...

With the better opportunity to develop significantly more, various individuals incline toward working together. Since you may require a decent aggregate at such undertakings, getting money related jars be a decent approach to cruise over the achievement course. Auto Title Loans Chicago

Angel said...

This is also a very good contribution that I love to read is not every day that I have the opportunity to see such a thing.
Raising funds for startups

Prins Upadhyay said...

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.Natural Stone Supplier India

Prins Upadhyay said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keeponsharing more...good luck.luxury honeymoon tour

chenmeinv0 said...

louis vuitton
true religion outlet
michael kors outlet online sale
ray ban sunglasses
ray ban sunglasses
louis vuitton
adidas nmd
adidas outlet
michael kors outlet
dallas mavericks jerseys
hzx20170301

chenmeinv0 said...

louis vuitton
true religion outlet
michael kors outlet online sale
ray ban sunglasses
ray ban sunglasses
louis vuitton
adidas nmd
adidas outlet
michael kors outlet
dallas mavericks jerseys
hzx20170301