Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB3.x SessionSynchronization & Asynchronous method calls

 
Bell Katapa
Ranch Hand
Posts: 57
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

I am studying for EJB3.x certification and have questions regarding EJB3.x SessionSychronization and Asychronous method calls on bean instances.
1. SessionSynchronization: If annotations are used to designate callback methods, does the the method annotated with @AfterCompletion have to include a boolean as its sole parameter in it's signature like its equivalent SessionSynchronization.afterCompletion(boolean). Can it define other parameters. Is it restricted with access modifiers and can it return values (even if it makes no sense to do this?) .
Also if a method defined with @Remove metadata without specifying the attribute retainIfException, what default value would be used. "True" or "False"?

2. @Asynchronous: After the bean method is invoked asynchronously at which point does the client receive a Future<V> object assuming the method called was declared to return a result? Is it immediately after control has been returned to the client or after the method has completed execution or thrown an error? If immediately after control is returned to the client, how does the client get to know about important events in the asynchronous call such as error or completion. Does it register a listener or maybe just make random guesses or optimistic synchronous checks, anticipating some kind of progress or useful feedback from the call it initiated?

I will appreciate explanations with good examples citing best practices.

Thank you.

 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bell Katapa wrote:I am studying for EJB3.x certification and have questions regarding EJB3.x SessionSychronization and Asychronous method calls on bean instances.
1. SessionSynchronization: If annotations are used to designate callback methods, does the the method annotated with @AfterCompletion have to include a boolean as its sole parameter in it's signature like its equivalent SessionSynchronization.afterCompletion(boolean). Can it define other parameters. Is it restricted with access modifiers and can it return values (even if it makes no sense to do this?) .

Why don't you try it and see if the container likes it?

Also if a method defined with @Remove metadata without specifying the attribute retainIfException, what default value would be used. "True" or "False"?

Check out what the default is on http://docs.oracle.com/javaee/7/api/javax/ejb/Remove.html#retainIfException().

2. @Asynchronous: After the bean method is invoked asynchronously at which point does the client receive a Future<V> object assuming the method called was declared to return a result? Is it immediately after control has been returned to the client or after the method has completed execution or thrown an error? If immediately after control is returned to the client, how does the client get to know about important events in the asynchronous call such as error or completion. Does it register a listener or maybe just make random guesses or optimistic synchronous checks, anticipating some kind of progress or useful feedback from the call it initiated?

Asynchronous methods return immediately. You get the Future object to interact with the asynchronous method, and only the Future object. There are no events; you need to call get() on the Future and see what it returns. If it returns successfully, then the asynchronous method has finished. If it throws an ExecutionException, then the asynchronous method has thrown an error, and the error is available as the cause of the ExecutionException. The get() method blocks until it either returns or throws an exception.
 
Bell Katapa
Ranch Hand
Posts: 57
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.

So basically the benefit one gets from asynchronous method calls is that logic not dependent on the 'called' method get to execute while the result is awaited. When the result is really needed before further logic can execute that's when the Future<V> object should be interrogated by calling any of the get() methods and possibly block until the result completes or ExecutionException is thrown.

Thank you for shedding light.
 
Bell Katapa
Ranch Hand
Posts: 57
Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rob,

I found something in the spec that clarifies javax.ejb.SessionSynchronization implementation via annotations in section 4.9.4 on page 122 of the EJB3.1 spec.

It says:

The bean class (or superclass) of a stateful session bean may use one or more of the session synchronization annotations @AfterBegin, @BeforeCompletion, and @AfterCompletion. Each bean has at most one session synchronization method for each of the three annotation types. In the case of method overriding of session synchronization methods declared by annotations, the most derived method takes precedence. The signatures of the session synchronization methods must follows these rules :
•The method must not be declared as final or static.
•The method may have any access type : public, private, protected, or package-level.
•The return type must be void.
•The @AfterBegin and @BeforeCompletion methods must take 0 arguments.
•The @AfterCompletion method must take a single argument of type boolean.

The attachment confirms. I am absolutely clear now,

Thanks.
session_synchronization.png
[Thumbnail for session_synchronization.png]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!