EJB note-1 (EJB study notes)

Source: Internet
Author: User
Tags current time message queue stub
EJB annotation is a feature of EJB technology. Most of EJB functions are implemented through annotation.
EJB comment
1. Stateful @ Stateful and Stateless @ Stateless session
@ Stateless defines this session as a Stateless session. The stateless session Bean is a simple POJO (purely object-oriented java Object), which is automatically instantiated and managed by the EJB3.0 container. Stateless Session Bean is not responsible for recording the user status. Once instantiated, Stateless Session Bean is added to the Session pool and can be shared by all users. Even if the user has been extinct, the life cycle of the Stateless Session Bean may not end, and it may still exist in the Session pool for other users to call.
@ Stateful defines this session as a Stateful session. A stateful Bean is a session Bean that can maintain its own state. Each user has its own instance. During the user's lifetime, the Stateful Session Bean maintains the user information, that is, "Stateful "; once the user dies (the call ends or the instance ends), the life cycle of the Stateful Session Bean ends.
A bean can be stateful and stateless at the same time.

2. Local interface @ Local and Remote interface @ Remote
If neither @ Local nor @ Remote annotation exists, the interface implemented by the session Bean is the Local interface by default. If you call
Use EJB (ensure that the client and the EJB container run in the same JVM) and use the Local interface to access EJB is better than the Remote interface.
Interface access to EJBs requires remote method call (RPCs), while the Local interface access to EJBs directly returns EJB references from JVM.

3. JNDI naming rules
The composition rule of the JNDI name is "upper layer name/lower layer name", separated by "/" between each layer. The default JNDI name is session Bean + interface type.

4. Change the JNDI name of the Session Bean.
To customize the JNDI name, you can use @ LocalBinding and @ RemoteBinding annotations to specify the JNDI name of the Local interface of the Session Bean, @ RemoteBinding: specifies the JNDI name of the Remote interface of the Session Bean. For example:


@ RemoteBinding (jndiBinding = "com/RemoteHello ")
@ LocalBinding (jndiBinding = "com/LocalHello") the first sentence defines JNDI as com/RemoteHello, and the second sentence defines JNDI as com/LocalHello

5. Bean lifecycle
@ PostConstruct: After the bean object is instantiated, the method using this annotation will be called immediately. This annotation applies to stateful and stateless session beans at the same time.
· @ PreDestroy: This annotation method is called before the container destroys a useless or expired bean instance from its object pool. This annotation applies to stateful and stateless session beans at the same time.
· @ PrePassivate: When a stateful session bean instance is idle for a long time, the container will deactivate it and
The status is stored in the cache. The method for using this annotation is called before the container inactivates the bean instance. This annotation applies to stateful session beans.
After passivation, the bean is still not operated after a period of time, and the container will delete it from the storage medium. In the future, any
The call container of the method throws an exception.
· @ PostActivate: when the client uses a deactivated session bean again, a new instance is created and its status is restored. The session bean that uses this annotation is called when bean activation is complete. This annotation applies only to stateful session beans.
· @ Init: This annotation specifies the method for initializing stateful session beans. It is different from the @ PostConstruct annotation that multiple @ Init annotation methods can exist in stateful session bean at the same time, but only one @ Init annotation method will be called for each bean instance. This takes
It depends on how beans are created (for details, see the EJB 3.0 specification ). @ PostConstruct is called after @ Init. Another useful life cycle method annotation is @ Remove, especially for stateful session beans. When the application uses
@ Remove annotation method, the container knows to Remove the bean instance from the object pool after the method is executed.

6. Interceptor)
The interceptor can listen to one or all methods of the program. The interceptor provides fine-grained control over the method call stream.
@ Interceptors annotation specifies one or more interceptor defined in the external class.
@ AroundInvoke annotation specifies the method to be used as the interceptor. The method specified with @ AroundInvoke annotation must follow the following format:
Public Object XXX (InvocationContext ctx) throws Exception. XXX indicates that the method name can be arbitrary. (Same as below)
In addition to defining the interceptor, you can also define one or more methods in the Session Bean as the interceptor.

7. Dependency Injection
To access those Service objects, you need to use the server's JNDI to find the session bean or message queue (MDB ). JNDI lookup is a key step to decouple the client from the actual server. However, using a string directly for JNDI search is not elegant.
@ EJB comment the EJB stub object is injected into any POJO managed by the EJB 3.0 container.


@ EJB (beanName = "HelloWorldBean ")
// @ EJB (mappedName = "HelloWorldBean/remote") beanName specifies the class name of the EJB and the JNDI name of the Bean instance.
@ EJB annotation if it is used in the setter method of the JavaBean style, the container automatically uses the correct parameter to call the setter method of the bean before the attribute is used for the first time.
@ EJB annotation can only inject EJB stub objects. In addition to @ EJB annotation, EJB 3.0 also supports @ Resource annotation to inject any
Resources.
If the JNDI object is in the local (java: comp/env) JNDI directory, you only need to specify its name without a prefix.
@ Resource annotation can inject them without specifying the JNDI name, and obtain its JNDI name through the variable type. @ Resource annotation can be used in the setter method of the JavaBean style.

8. Scheduled service
The scheduled service is used to execute a program after a specific period of time, and the @ Timeout annotation is used to declare the timer method.
Obtain the SessionContext object through dependency injection @ Resource SessionContext ctx and call ctx. getTimerService (). the createTimer (Date arg0, long arg1, Serializable arg2) method creates a timer. The meanings of the three parameters are as follows:
Date arg0 timer start time. If the input time is earlier than the current time, the timer starts immediately.
How long will long arg1 trigger the scheduled event again. Unit: milliseconds
After the timer is created, you must declare the timer method. Timer method required


Void XXX (Timer timer) to be continued

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.