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 {

    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:

    public void callback(InvocationContext context) throws Exception {

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.

    public void callback(InvocationContext context) {
        try {
        } 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:

    public void callback(InvocationContext context) {
        try {
        } 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.


Unknown said...

20170619 junda
rolex watches,rolex watches,swiss watches,watches for men,watches for women,omega watches,replica watches,rolex watches for sale,rolex replica,rolex watch,cartier watches,rolex submariner,fake rolex,rolex replica watches,replica rolex
ray ban sunglasses
ferragamo outlet
ray ban wayfarer
true religion jeans sale
christian louboutin sale
ralph lauren outlet
jordan 4
cheap nfl jerseys

eric yao said...

Coach Outlet ED Hardy Outlet Coach Outlet Store Online Kate Spade Outlet Cheap Jordans Coach Purses Coach Outlet Kate Spade Outlet Toms Outlet Louis Vuitton

Habib Malik said...

Sarkari Naukri
OBC Bank Recruitment 2018

Danish Ahmed said...

Thanks for sharing this article, now you can check your cut off marks by following below given links

rajasthan post office gds result 2017

north eastern post office gds result 2017

karnataka post office gds result 2017

punjab post office gds result 2017

uttarakhand post office gds result 2017

Danish Ahmed said...

Thanks for sharing this article, now you can check your Time table & Date sheet 2017 by following below given links

ap ssc time table 2017

assam hslc date sheet 2017

bihar board 10th date sheet 2017

cbse board 10th date sheet 2017

ch board 10th date sheet 2017

luu quoc trung said...

To make yourselves available FNAF Friv Friv Games Thank you both for sharing Holy Mass with us each day Friv Games Twizl Juegos De FNAF when we were called out late at night and any other time and for your support and encouragement in every respect. Friv 2021 Twizl Juegos Yepi Juegos Twizl Thank you for sharing with me a few of your ideas regarding the possibility.

Danish Ahmed said...

Thanks for sharing this article, now you can also check your Admit card & Recuritment 2017 by following below given links...

police si admit card 2017

gpsc medical officer admit card 2017

tamilnadu teacher recuritment 2017

bihar police constable recuritment 2017

gujarat high court recruitment 2017

ngoem199 said...

nice blog, thank you
Danke, dafur dass Ihr jeden Tag mit uns die Hl. Messe gefeiert habt, fur all die Reparaturen im ganzen Haus Friv 10 Friv 2018 Friv 2019 fur Euere Bereitschaft Friv 10 Friv 20 Friv 2 immer zur Verfugung zu stehen Geometry Dash Twizl Twizy Twizl Danke, dass Sie Ihr Muhen um den Aufbau des Leibes Christi mit uns teilten.

ngoem199 said...

Senhor Deputado Cashman, agradeço-lhe a informação. Juegos Friv Jogos Friv Friv4school A todos que partilham e trabalham sob estas mesmas convicções e princípios Jeux De Friv Juegos De Roblox Zoxy Mais uma vez, obrigada ao Parlamento por comungar da visão que informa a nova política dos consumidores Juegos Kizi Juegos Yepi Twizl Zoxy assente no mercado - a visão de um mercado de consumidores informados e capacitados que procuram e usufruem, com confiança,

Unknown said...

Delhi police Constable Physical Exam Date 2017
BRABU Part-i-ii-Exam Result
Tripura Court System Officer 13th August Exam result
Assam PSC BDO 2017
Gujarat high court Civil Judge Pre-Exam Result

ngoem199 said...

Wir danken Ihnen Friv4school 2018 Gry Friv 2 Gry Friv 5 dass Sie diese Hoffnung mit uns teilen und diesen Schritt auf dem Pilgerweg des Vertrauens Gry Friv Juegos Friv 100 Juegos Friv 1000 mit uns gegangen sind. Juegos Friv 5 Juegos De Friv 2 Juegos Friv 250 Juegos Yepi wir danken Ihnen für das Interesse an unseren Produkten und hoffe

Habib Malik said...

Kalyani University Routine 2018
Tripura Board Madhyamim Date Sheet 2018
Tripura Board HS Date Sheet 2018
Telangana SSC Date Sheet 2018
Telanagana Intermediate Date Sheet 2018

Habib Malik said...

This information is really good, students can also check Board Time Table 2018

oliviaallan said...

Most people think that online dating is just a waste of time as there are no ways to end up with a beautiful woman. The truth is there are tons of beautiful girls on online dating sites and they are always on the lookout for men to go out with.


oliviaallan said...

Emotional Hook Formula program is meant to help women be confident and trust themselves, so they can attract the men they like easily, without exaggerating their behavior.


Unknown said...

The Outback Vision Protocol is an excellent eBook uncovering the exact reasons that result in the vision loss.


Unknown said...

Increase the size of your penis, such as that of security, is not your main concern when trying to do anything.


Sharon Wood said...

Norton Toll Free Number
Malwarebytes Contact Number
McAfee Toll Free Phone Number
Avast Antivirus Toll Free Number

rithiaanandh said...

A good blog always comes-up with new and exciting information and while reading I have felt that this blog really has all those quality that qualify a blog to be a one. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
Sql server dba online training

Darren Demers said...

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. shawls for dresses , shawls for women , shawls for sale , black shawl , white shawl , shawl , silver shawl , gold shawl , navy shawl , woolen shawl

Anaminka said...

Men looking for Call Girls in Chanakyapuri usually have a variety of needs that need to be fulfilled. Some want to hire escorts just as a companion, some want them to be their dinner dates or dance floor partners while others may want them to play the role of the sexy girlfriends. Check our other services also..
Female Escorts in Chanakyapuri
Escorts in Chanakyapuri
Escorts Service in Vasant Kunj
Escorts Service in Vasant Kunj
Escorts in Vasant Kunj

No #1 Gurgaon Escorts Service Agency-Gurugram said...

With ourRussian Escorts in Gurgaonand escorts services, you may really improve stage of fun by its quality services different. Sure! This assists its people to victory over all the gloom and depressive disorders from your lifestyle and appeases your delicate wishes specifically.So, you may do a trip at web page and can get your wish suggested with an outstanding woman escort. Check our other services also...
Call Girls in Gurgaon
Escorts in Gurgaon
Escorts in Gurgaon
Female Escorts in Gurgaon
Russian Call Girls in Gurgaon

pkv games said...

layanan jasa cargo murah hanya disini


russian escorts in gurgaon
housewife escorts in gurgaon
gurgaon escort services
gurgaon escorts
escorts in gurgaon
escort services in gurgaon
gurgaon call girls
call girls in gurgaon

Ms Nikita Agarwal said...

Very Good way of attracting your reader's attention is to write blog like you. Please do contact me on genuinehotelescorts@gmail.com for
Jaipur call girls
Jaipur call girls
Jaipur call girls
Delhi call girls
Guwahati call girls
Guwahati call girls
Guwahati call girls
Guwahati call girls
Aerocity call girls
Lucknow call girls

alia said...

Bellary Escorts
Chennai Escorts

Coimbatore Escorts
Delhi Escorts
Dhanbad Escorts
Dharwad Escorts
Gaya Escorts
Gurgaon Escorts

Jammu Escorts
Pathankot Escorts

alia said...

Kanpur Escorts
Mumbai Escorts
Navi Mumbai Escorts
Noida Escorts
Pune Escorts
Ramagundam Escorts

Thane Escorts
Vasai-Virar Escorts
Ahmedabad Escorts
Bangalore Escorts

alia said...

Shillong Escorts
Shimla Escorts

Siliguri Escorts
Silvassa Escorts
Solapur Escorts
South Dumdum Escorts
Sri Ganganagar Escorts
Srinagar Escorts

Surat Escorts
Trivandrum Escorts

alia said...

Pimpri-Chinchwad Escorts
Port Blair Escorts
Raipur Escorts
Rajahmundry Escorts
Rajkot Escorts
Ranchi Escorts

Rourkela Escorts
Saharanpur Escorts
Salem Escorts
Sangli Escorts

alia said...

Mangalore Escorts
Meerut Escorts
Mysore Escorts
Nagpur Escorts

Nanded Escorts
Nashik Escorts
Nellore Escorts
Kolkata Escorts
Panaji Escorts
Patiala Escorts

alia said...

Kochi Escorts
Kolhapur Escorts

Kozhikode Escorts
Kurnool Escorts
Loni Escorts
Lucknow Escorts
Ludhiana Escorts
Madurai Escorts

Maheshtala Escorts
Malegaon Escorts

alia said...

Hyderabad Escorts
Indore Escorts
Itanagar Escorts
Jabalpur Escorts
Jaipur Escorts
Jalandhar Escorts

Jhansi Escorts
Jodhpur Escorts
Kakinada Escorts
Kalyan-Dombivali Escorts

alia said...

Erode Escorts
Faridabad Escorts
Gangtok Escorts
Gorakhpur Escorts

Gulbarga Escorts
Guntur Escorts
Guwahati Escorts
Gwalior Escorts
Howrah Escorts
Hubli Escorts

alia said...

Bhiwandi Escorts
Bhopal Escorts

Bhubaneswar Escorts
Bikaner Escorts
Bokaro Escorts
Chandigarh Escorts
Cuttack Escorts
Daman Escorts

Dehradun Escorts
Durgapur Escorts

alia said...

Agra Escorts
Akola Escorts
Aligarh Escorts
Allahabad Escorts
Amravati Escorts
Amritsar Escorts

Bareilly Escorts
Belgaum Escorts
Bhavnagar Escorts
Bhilai Escorts

ncrhotgirls.com said...

Thanks for sharing the article...You write very well...If you need backlinks please reach me on genuinehotelescorts@gmail.com
Call Girls in Jaipur
Call Girls in Jaipur
Call Girls in Jaipur
Call Girls in Delhi
Call Girls in Guwahati
Call Girls in Guwahati
Call Girls in Guwahati
Call Girls in Guwahati
Call Girls in Aerocity
Call Girls in Lucknow

guwahaticallgirl.com said...

Explore the profiles of guwahati escorts, guwahati call girls, call girls in guwahati and
escorts in guwahati for female companionship in top rated hotels.
Escort service in Guwahati
Call girl service in Guwahati

nituroy.com said...

Make your daily life passionately amazing with Guwahati escort and call girl services.
Romance is not a simple factor that you can bargain with.
Escorts in Guwahati
Escort services in Guwahati

diyaroy.com said...

Diya Roy is a premier escort agency in Guwahati that provides call girl services or
escort services in Guwahati hotels at cheap and affordable fixed rates
Call Girl service in Guwahati
Escort service in Guwahati

guwahaticallgirl.com said...

Explore the profiles of guwahati escorts, guwahati call girls, call girls in guwahati and
escorts in guwahati for female companionship in top rated hotels.
Get affordable escort services in guwahati hotels now.
Guwahati escort
Call girl in Guwahati

dialescorts.com said...

We have calls available in the best of categories, according to our need, we are waiting for you, erotic and beautiful call girls in Guwahati, escort
service Guwahati, you can come on our website and contact us 24x7 hours anytime in Guwahati. I look forward.
Guwahati Call Girl
Guwahati Escort Service