6/23/2006

Don't Overload EJB 3 Lifecycle and Interceptor Methods

Overloading is a natural part of java language. With overloading, we are just declaring multiple, totally separate methods that happen to share the same name. But don't use overloading when it comes to EJB 3 lifecycle and interceptor methods. For example, the following bean and interceptor classes are all wrong:

Invalid EJB 1.

package com.foo.ejb;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

@Stateless
public class InvalidBean1 implements HelloRemote {
@PostConstruct
private void init() {
System.out.println("InvalidBean1 PostConstruct method called.");
}

public void init(Properties props) {
System.out.println("InvalidBean1 public init(Properties) method, not a PostConstruct method.");
}
To fix it, just rename init(Properties) to init2(Properties), and make sure init method is not overloaded in the current class.

Invalid EJB 2.
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

@Stateless
public class InvalidBean2 implements HelloRemote {
@AroundInvoke
private Object intercept(InvocationContext inv)
throws Exception {
System.out.println("InvalidBean2 AroundInvoke method called.");
return inv.proceed();
}

public void intercept(Properties props) {
System.out.println("InvalidBean2 public intercept(Properties) method, not an AroundInvoke method.");
}
}
To fix it, just rename intercept(Properties) to intercept2(Properties), and make sure intercept method is not overloaded in the current class.

Invalid interceptor class
.
import javax.interceptor.InvocationContext;
import javax.interceptor.AroundInvoke;

public class InvalidInterceptor {
@AroundInvoke
private Object intercept(InvocationContext inv)
throws Exception {
System.out.println("InvalidInterceptor AroundInvoke method called.");
return inv.proceed();
}

public void intercept(Properties props) {
System.out.println("InvalidInterceptor public intercept(Properties) method, not an AroundInvoke method.");
}
}
To fix it, just rename intercept(Properties) to intercept2(Properties), and make sure intercept method is not overloaded in the current class.

What will happen with these invalid apps? Since these overloaded methods are legal in java, so your apps will be able to build, maybe even deploy successfully. But when the EJB is instantiated, EJB container looks for a lifecycle or interceptor method only by name. It may or may not get the correct method, depending on container implementation, operation systems, and other unknown factors.

With JavaEE SDK 5/Glassfish/Sun Java System Application Server 9 on Sparc Solaris 10 , I was able to deploy and run my app successfully, and the correct lifecycle method is found and invoked. With the same appserver on Windows, accessing the EJB will always fail with the following error:
[#|2006-06-23T10:07:11.291-0400|INFO|sun-appserver-pe9.0|javax.enterprise.system.container.ejb|_ThreadID=16;_ThreadName=p: thread-pool-1; w: 3;|EJB5070: Exception creating stateless session bean : [{0}]
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.ejb.containers.interceptors.BeanCallbackInterceptor$1.run(InterceptorManager.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.ejb.containers.interceptors.BeanCallbackInterceptor.intercept(InterceptorManager.java:595)
at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(InterceptorManager.java:448)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:204)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:546)
at com.sun.ejb.containers.StatelessSessionContainer.access$100(StatelessSessionContainer.java:96)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:746)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:186)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:469)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:1566)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1148)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:189)
at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:110)
...
It means on Windows, the appserver always gets the wrong, unannotated method by its overloaded name.

This is a bug in the application, not in the appserver. Unfortunately, it's a restriction we have to live with.

Continue to part 2 and part 3

38 comments:

Iklan Baris Gratis said...

I hope this tutorial can fix.

happy wheels said...

Thank you very much for your post, it makes us have more and more discs in our life, So kind for you, I also hope you will make more and more excellent post and let’s more and more talk, thank you very much, dear.
happy wheels| friv|monster high| shooting games| tetris| 8 ball pool
cool math games| barbie games| friv4school| agario| fighting games

hoa huy said...

Big Farm

Big Farm is an awesome multiplayer farm management game made by the Goodgame Studios.
Your mission is simple: Create a big farm, grow crops, breed animals,
and become the richest farmer of the universe. Enjoy Goodgame's Big Farm!

Happywheels Hau said...

You have a real ability for writing unique content. I like how you think and the way you represent your views in this article. I agree with your way of thinking. Thank you for sharing.
happy wheels
super mario bros
pacman
agario

NGOCLAN said...

Really impressive post. I enjoyed your article and planning to rewrite it on my own blog
Friv 14|
Kizi|
Friv4school

Friv online said...

when you see the stress in work, life, friv could well be a good site to try. thanks you for sharing friv games, friv online, friv.com, friv

Friv online said...

when you see the stress in work, life, friv could well be a good site to try. thanks you for sharing friv games, friv online, friv.com, friv

Friv Games said...

Play free Friv games flash online games flash.
Thanks you for sharing
Friv Games | Friv Online

Love KidsTV said...

Thanks for your post. Click to play Plants vs Zombies , Solitaire,Tom And Jerry Games, Brain Games, Happy Wheels , Five Nights At Freddy's

Love KidsTV said...

Truyen ngon tinh hay la the loai truyen tinh cam
Truyen teen hay la nhung truyen tinh yeu tuoi teen
Don doc tai trang doc truyen online.

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

hoang duong said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write.
Kizi 2
Friv 2

Kungfukangda said...

thank you very much for the information.
Friv 1000

Friv 10 said...

Thanks you for sharing !
Friv 10
Kizi 10
Yepi 2

Friv5 said...

You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
Thanks for sharing !
Friv 5
Kizi 1
Yepi 3

tranduyphong said...

after hours of work tired, you need entertainment. Refer to our website. hope you get the most comfortable
Kizi 1000

Imran Hashmi said...

Wonderful blog.
KMSpico 10.2.0
Vipre Internet Security
Microsoft Office 365
Connectify Hotspot
Driver Genius 16

Candy Sim said...

The blog or and best that is extremely useful to keep I can share the ideas
of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
wings io| earn to die 6
earn to die 5| happy wheels

Jasmine Thompson said...

Score Hero é um jogo de futebol que adapta para os celulares de maneira bastante interessante a jogabilidade do esporte mais amado do Brasil. Score Hero download - baixar Score Hero - Score Hero baixar - Score Hero gratis - Score Hero app - Score Hero - Score Hero download

Alice Denny said...

Welcomes to google terminal keep sharing such ideas in the future as well.
google snake this was actually EARN TO DIE
what i was looking for,and i am glad to came here! AGE OF WAR 2
Hi! I’ve been reading your blog for a while HAPPY WHEELS
I want you to thank for your time of this wonderful read!!! Slitherio
Amazing insight you have on this, Slither io This article always blew me BIG FARM

Pham Dinh said...

You want to relax after a stressful working hours. Refer to our website. Hope you get the most comfort.
Thanks for sharing !Y8 arcade

angelina jolie said...

Many thanks for sharing the information in your blog.
Y8Y8Y8

Red Ball 4 said...

The article you have shared here very awesome. I really like and appreciated your work. I read deeply your article, the points you have mentioned in this article are useful
red ball 3 | ninjago games | swords and souls game | strikeforce kitty 2 | geometry dash 2.0

Megan Nicole said...

Welcome, your blog is great. I have read many of your articles. It's very interesting and meaningful, it helps me have more knowledge about life.
atari breakout game | super mechs | wings io | wings.io | | supermechs wingsio

Oanh Tran said...

Thank you so much for commenting, it is lovely and really makes my day!
zoxy games |
friv 10 games | friv games | friv 100 play

Oanh Tran said...

Thank you so much for commenting, it is lovely and really makes my day!
wheely 8 games | zoxy 3 | twizlgames | papalouie 4

Craft Mine said...

Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!
minecraft | baixar facebook | traffic rider jogo | baixar whatsapp | facebook baixar | baixar photo grid

Bảo Gia said...

I liked it so much and very interesting, too! Thanks for sharing the experience.
Wonderful post, really great tips and advice.
friv 3
friv 4
kizi 2

Friv game said...

Play with millions of players around the world and try to become the longest of the day! Slither.io online is totally free. the best free online Twizl games, juegos Twizl, jogos Twizlfrom the top game developers all over the world. Zoxy is the best entertainment site with free games.

trump Ivanka said...

every day just relaxing a bit to increase the work efficiency of you. Take a look at our website

Friv 4 School
Friv 1
GirlsGoGames

Melinda Gates said...

You want to relax after a stressful working hours. Refer to our website. Hope you get the most comfort.
Thanks for sharing !

Friv Games
Y2
Friv 200

Williams Milada said...

Thank you for such a sweet tutorial - all this time later, I've found it and love the end result. I appreciate the time you spent sharing your skills.
run 2 | cookieclicker | crossword puzzles | dream league soccer

Alexander Sara said...

It gave me a lot of useful information. thank you very much. Can you play more games at :
super smash flash 2 unblocked | super smash flash | super smash flash 2

Gonzalez Caroline said...

Thanks for sharing this quality information with us. I really enjoyed reading.
descargar facebook | descargar facebook gratis | facebook descargar

Malia Obama said...

Thanks a lot! It is definitely an terrific internet site!
Friv 2
Friv 4
Friv 3

a10friv said...

you'd have time to look these kids active. Please visit our website and let us play the game interesting.
A10 Friv
Friv 10
Y6

Dong Hwa said...

Our website has many interesting games. Please share this with your friends and have fun.
Friv 2/
Kizi 1000

Linda Rose said...

You’ve written nice post, I am gonna bookmark this page, thanks for info. I actually appreciate your own position and I will be sure to come back here.
Facebook Lite