• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

question about business method interceptor

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mock question:
A developer writes an interceptor class called Foolnterceptor containing the following
Aroundlnvoke method:
11 . @Aroundlnvoke
12. public Object intercept(lnvocationContext ctx) {
13. return "intercepted";
14.}
Foolnterceptor is applied to a business method in a stateless session bean:
11. @lnterceptors(Foolnterceptor.class)
12. public String testzero(int i) {
13. return (i == 0)? "zero": "not zero";
14. }
Which describes the result when a client invokes the testzero method with a value of 1?
A. The intercept method is NEVER invoked.
B. The client receives a return value of "zero".
C. The client receives a return value of "not zero".
D. The client receives a return value of "intercepted".

given answer is D,why business method returns value from interceptor method according given answer?I think it's C,when business method was called,it's interceptor method will run too,but how can business method get return value from interceptor method?anyone clear this for me,thanks.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is very simple.
When you call business method which have interceptor only interceptor is called (from client view) and some informations about this method is given in InvocationContext object.

If you want to return value from business method you should write following code in your interceptor method

@Aroundlnvoke
public Object intercept(lnvocationContext ctx) {
try{
return ctx.proceed();
}catch(Exception e){...}
return null;
}
or another way

@Aroundlnvoke
public Object intercept(lnvocationContext ctx) {
try{
return ctx.getMethod().invoke(ctx.getTarget(),ctx.getParameters());
}catch(java.lang.reflect.InvocationTargetException e){...}
return null;
}
 
No more fooling around. Read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic