Amazon

Monday, January 28, 2013

EJB 3.1 - Client View of a Session Bean



Client access session object through the session bean's client view.
Type of client - Local, Remote or WebService.
Remote Client - Can be another EJB or Java program. It is implemented by container as "Remote Business Interface" (or EJBObject Interface). Its location independent and provides loose coupling between client and bean. Arguments and results of Remote Business Interface Methods are "passed by value" and must be serializable. Remote calls are potentially expensive and involve network latency, overhead of argument copying etc.
Note- Remote Interface is use to refer the client of EJB 2.1.
Local Client - Session object and Local Client should be colocated in same container as the bean. It means Bean cannot be deployed on the node different than the node where client is deployed, thus restricting the distribution of components. Its not location-independent and may be tightly coupled with Session Bean. In EJB3.1 Local Client access Session Bean through bean's Local Business Interface or through a no-interface client view representing all public methods of the bean class. Arguments and results of methods of Local Client View are "passed by reference".
Webservice Client View - Applicable for Stateless or Singleton Session bean. WSDL Document the bean implements describes the Webservice client view. The webservice methods of the session bean provide the basis of the web service client view of the bean that is exported through using WSDL. The web service client view of an enterprise bean is location independent and remotable.
A client can invoke session bean synchronously or asynchronously.
A client can obtain a session bean’s business interface through dependency injection or lookup in the JNDI namespace. Its irrespective of business interface is local or remote, the syntax is same.
Dependency Injection used to obtain business interface Cart as
@EJB Cart cart;
Or, below is the JNDI Lookup method
@Resource SessionContext ctx;
...
Cart cart = (Cart)ctx.lookup("cart");
Obtaining Business Interface of No-Interface View
The No-interface view of the CartBean session bean with bean class com.acme.CartBean may be obtained using dependency injection as follows :
@EJB CartBean cart;
Using JNDI
@Resource SessionContext ctx;
...
CartBean cart = (CartBean)ctx.lookup(“cart”);
Despite the fact that the client reference for the No-interface view has type , the client never directly uses the new operator to acquire the reference.
Session Bean's Business Interface View
--------------------------------------------------------
An ordinary java interface, which contain business method of Session Bean
Reference of Business Interface can be passed as parameter or return value of a business interface method.
NoSuchEJBException - Raised when attmpted method invocation of business interface method, when corresponsing StatefulSession Bean or SingleTonSession Bean does not exist or removed.
The container provides an implementation of a session bean’s business interface such that when the client invokes a method on the instance of the business interface, the business method on the session bean instance and any interceptor methods are invoked as needed.
Session Bean's No-Interface View
--------------------------------------------------------
Its variation on Local View that exposes public methods of the bean class without the use of separate business interface.
A reference to a no-interface view can be passed a parameter or return value of any Local business interface or no-interface view method.
The container provides an implementation of a reference to a no-interface view such that when the client invokes a method on the reference, the business method on the session bean instance and any interceptor methods are invoked as needed. As with the session bean remote and local views, a client acquires a no-interface view reference via lookup or injection only. A client does not directly instantiate (use the new operator on) the bean class to acquire a reference to the no-interface view.
javax.ejb.EJBException- Only public methods of the bean class (and any super-classes) may be invoked through the no-interface view. Attempted invocations of methods with any other access modifiers via the no-interface view reference must result in a javax.ejb.EJBException.
@PostConstruct method - It is recommended that the bean developer place component initialization logic in a @PostConstruct method instead of the bean class no-arg constructor, because, it is possible that the acquisition of a client reference to the no-interface view will result in the invocation of thebean-class constructor.
javax.ejb.NoSuchEJBException- If a stateful session or singleton session bean bean has been removed, attempted invocations on the no-interface view reference must result in a javax.ejb.NoSuchEJBException.

EJB 3.1
========================
Client View of a Session Bean
========================

Session bean is not shared among multiple clients.
Client access session object through the session bean's client view.

Type of client - Local, Remote or WebService.

Remote Client View - Can be another EJB or Java program. It is implemented by container as "Remote Business Interface" (or EJBObject Interface). Its location independent and provides loose coupling between client and bean. Arguments and results of Remote Business Interface Methods are "passed by value" and must be serializable. Remote calls are potentially expensive and involve network latency, overhead of argument copying etc.
Note- Remote Interface is use to refer the client of EJB 2.1.

Local Client View - Session object and Local Client should be colocated in same container as the bean. It means Bean cannot be deployed on the node different than the node where client is deployed, thus restricting the distribution of components. Its not location-independent and may be tightly coupled with Session Bean. In EJB3.1 Local Client access Session Bean through bean's Local Business Interface or through a no-interface client view representing all public methods of the bean class. Arguments and results of methods of Local Client View are "passed by reference".

Webservice Client View - Applicable for Stateless or Singleton Session bean. WSDL Document the bean implements describes the Webservice client view. The webservice methods of the session bean provide the basis of the web service client view of the bean that is exported through using WSDL. The web service client view of an enterprise bean is location independent and remotable and cannot identify, the bean is stateless or singleton.
The web service client’s access to the web service functionality provided by a session bean occurs through a web service endpoint. In the case of Java clients, this endpoint is accessed as a JAX-WS or JAX-RPC service endpoint using the JAX-WS or JAX-RPC client view APIs.

A client can invoke session bean synchronously or asynchronously.

A client can obtain a session bean’s business interface through dependency injection or lookup in the JNDI namespace. Its irrespective of business interface is local or remote, the syntax is same.
Dependency Injection used to obtain business interface Cart as
@EJB Cart cart;

Or, below is the JNDI Lookup method
@Resource SessionContext ctx;
...
Cart cart = (Cart)ctx.lookup("cart");

Obtaining Business Interface of No-Interface View
The No-interface view of the CartBean session bean with bean class com.acme.CartBean may be obtained using dependency injection as follows :
@EJB CartBean cart;
Using JNDI
@Resource SessionContext ctx;
...
CartBean cart = (CartBean)ctx.lookup(“cart”);
Despite the fact that the client reference for the No-interface view has type , the client never directly uses the new operator to acquire the reference.

Session Bean's Business Interface View
--------------------------------------------------------
An ordinary java interface, which contain business method of Session Bean
Reference of Business Interface can be passed as parameter or return value of a business interface method.

NoSuchEJBException - Raised when attmpted method invocation of business interface method, when corresponsing StatefulSession Bean or SingleTonSession Bean does not exist or removed.

The container provides an implementation of a session bean’s business interface such that when the client invokes a method on the instance of the business interface, the business method on the session bean instance and any interceptor methods are invoked as needed.

Session Bean's No-Interface View
-------------------------------------------------------
Its variation on Local View that exposes public methods of the bean class without the use of separate business interface.
A reference to a no-interface view can be passed a parameter or return value of any Local business interface or no-interface view method.

The container provides an implementation of a reference to a no-interface view such that when the client invokes a method on the reference, the business method on the session bean instance and any interceptor methods are invoked as needed. As with the session bean remote and local views, a client acquires a no-interface view reference via lookup or injection only. A client does not directly instantiate (use the new operator on) the bean class to acquire a reference to the no-interface view.

javax.ejb.EJBException - Only public methods of the bean class (and any super-classes) may be invoked through the no-interface view. Attempted invocations of methods with any other access modifiers via the no-interface view reference must result in a javax.ejb.EJBException.

@PostConstruct method - It is recommended that the bean developer place component initialization logic in a @PostConstruct method instead of the bean class no-arg constructor, because, it is possible that the acquisition of a client reference to the no-interface view will result in the invocation of thebean-class constructor.

javax.ejb.NoSuchEJBException- If a stateful session or singleton session bean bean has been removed, attempted invocations on the no-interface view reference must result in a javax.ejb.NoSuchEJBException.

Removing a Session Bean
Stateful - A client may remove by invoking a business method of its business interface designated as Remove Method.
Stateless and Singleton - Removal of a these bean instances is performed by the container, transparently to the client. Life cycle of these bean doesnot require client view to remove.

Session Object Identity
A client can test two EJB 3.x Remote/Local view references for identity by means of the Object.equals and Object.hashCode methods.

Stateful -


@EJB Cart cart1;
@EJB Cart cart2;
...
if (cart1.equals(cart1)) { // this test must return true
...
}
...
if (cart1.equals(cart2)) { // this test must return false
...
}

1. References to the same business interface for the same stateful session bean instance will be equal.
2. All references to the no-interface view of the same stateful session bean instance will be equal.
3. Stateful session bean references to different interface types or between an interface type and a no-interface view or to different session bean instances will not have the same identity.

Stateless or Singleton -

@EJB Cart cart1;
@EJB Cart cart2;
...
if (cart1.equals(cart1)) { // this test must return true
...
}
...
if (cart1.equals(cart2)) { // this test must also return true
...
}


1. The equals method always returns true when used to compare references to the same business interface type of the same stateless/singleton session bean.
2. The equals method always returns true when used to compare references to the no-interface view of the same stateless/singleton session bean.
3. Stateless/singleton session bean references to either different business interface types or between an interface type and a no-interface view or to different session beans will not be equal.

Asynchronous Invocations -
1. Clients can achieve asynchronous invocation behavior by invoking session bean methods that have been designed to support asynchrony.
2. When a client invokes an asynchronous method, the container returns control to the client. immediately and continues processing the invocation on a separate thread of execution.
3. The client should expect to receive a system exception (in the form of a javax.ejb.EJBException) on the client thread if the container has problems allocating the internal resources required to support the asynchronous method. If a system exception is received on the client thread, the client can expect that the container will not be able to dispatch the asynchronous method. The client may wish to retry the asynchronous method at a later time.
4. If no system exception is received, then the client can expect that the container will make an attempt to dispatch the asynchronous method. An exception resulting from the asynchronous method execution(e.g. an authorization failure, transaction commit failure, application exception, etc.) will be available via the Future object.

FutureAsynchronous method have return type void or Future, where V represents the result value of asynchronous invocation. allows client to retrieve the invocation result value, fetch any invocation exception, or attempt to cancel invocation exception.

Future.cancle(boolean myInterruptIfRunning) -If the invocation is already not dispatched, call to cancel on Future object by client will result in canceling of associated asynchrounous invocation. But there is not guarantee that invocation can be cancelled. Method must return true, when the invocation can be cancelled, false will be returned in other case.
If mayInterruptIfRunning is set to true, SessionContext.wasCancelCalled will return true and false vice versa.

Future.get - Provides result value or resulting excetion after asynchronous invocation. This method can be called if Future.cancle is not called by client.

Concurrent Access to Session Bean References - It is permissable to acquire a session bean reference and attempt to invoke the same reference object concurrently from multiple threads. However, the resulting client behavior on each thread depends on the concurrency semantics of the target bean.

No comments:

Post a Comment

Amazon Best Sellors

Weblogic - Server Creation - Issues

Datasource creation using python script.  Python Script was failing after creating Datasource. From UI Admin Console, if I try to activat...