- EJB 3 Developer Guide
- Michael Sikora
- 526字
- 2021-07-02 11:34:54
Local Interfaces
Up to this point all our examples have used the session bean remote interface, as the clients have run in their own JVM outside the EJB container. Behind the scenes, a remote interface uses the RMI-IIOP protocol for network operations. This protocol stipulates that method arguments are passed by value and not by reference. Passing by value means that an object being passed from the client to the remote bean, or vice versa, is first serialized then passed over the network then deserialized. This all has an impact in terms of performance. Even if our client is a session bean invoking another in the same container there is a performance overhead if we use a remote interface because of the serialization and deserialization taking place. For this reason EJB technology provides a local interface option for session beans. Method arguments are passed by reference and not by value so improving performance.
To illustrate all this we shall create a stateless session bean which will be invoked by and run in the same EJB container, as our original TimeServiceBean
. This invoked bean will just return the string Have a Nice Day
. First we define the beans interface, NiceDayService
:
package ejb30.session; import javax.ejb.Local; @Local public interface NiceDayService { public String getMessage(); }
Note that we have prefixed the interface definition with the @Local
annotation. This indicates to the EJB container that this bean may only be invoked by a local client running in the same container. The interface consists of just one method definition, getMessage()
. Next we look at the bean implementation, NiceDayServiceBean
:
package ejb30.session; import java.util.*; import javax.ejb.Stateless; @Stateless public class NiceDayServiceBean implements NiceDayService { public String getMessage() { return " - Have a Nice Day"; } }
This is all straightforward. We note that the session bean is stateless and the getMessage()
method returns the string - Have a Nice Day
.
Now let's look as to how we might modify the TimeServiceBean
to invoke NiceDayService
. The TimeServiceBean
will append the NiceDayService
message to the current time. Here is the modified code for TimeServiceBean
:
package ejb30.session; import java.util.*; import javax.ejb.Stateless; import javax.ejb.EJB; @Stateless public class TimeServiceBean implements TimeService { private @EJB NiceDayService niceDay; public String getTime() { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt.format("%tr", cal); return fmt.toString() + niceDay.getMessage(); } }
We use the @EJB
annotation to instruct the container to lookup the NiceDayService
bean and inject a bean reference into the niceDay
field. Recall this is an example of field injection. We can then invoke the niceDay.getMessage()
method.
An alternative to field injection is setter injection. In this case a method rather than a field is annotated. The following version of TimeServiceBean
uses setter injection:
@Stateless public class TimeServiceBean implements TimeService { private NiceDayService niceDay; public String getTime() { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt.format("%tr", cal); return fmt.toString() + niceDay.getMessage(); } @EJB public void setNiceDay(NiceDayService niceDay) { this.niceDay = niceDay; } }
By annotating the setNiceDay()
method with the @EJB
annotation, we are instructing the container to lookup the NiceDayService
bean, then invoke the setNiceDay()
method passing the NiceDayService
bean reference as a parameter to setNiceDay().
- Expert Cube Development with Microsoft SQL Server 2008 Analysis Services
- Photoshop圖形圖像設計案例教程(高等院校計算機任務驅動教改教材)
- Adobe Photoshop 網頁設計與制作標準實訓教程(CS5修訂版)
- Photoshop CS6平面設計應用教程(第4版)
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- Photoshop+Adobe Camera Raw+Lightroom(攝影后期照片潤飾實戰)
- 量化投資與FOF投資:以MATLAB+Python為工具
- ASP.NET 3.5 Social Networking
- 手機/電腦雙平臺剪映短視頻后期編輯從新手到高手
- TopSolid Wood軟件設計技術與應用
- Mastering Redis
- 夢幻森林Procreate童話風插畫繪制專業技法
- Autodesk Maya 2015標準教材II
- Creo 4.0中文版基礎教程
- 和秋葉一起學:秒懂Photoshop后期修圖