Before explaining how to inject other EJB services, let's first look at what the local interface is, and the first example is a remote interface, so let's talk about what the local interface is.
1. Local interface calls
The process of the remote calling interface we explained earlier is as follows:
In detail: First the client needs to establish socket communication with the EJB, they need to send the IIOP protocol message between them on the communication pipeline, because the data is to be transmitted over the network, the Java object that holds the data must be serialized. This process has the overhead of network communication, the cost of protocol parsing (Internet Inter-ORB Protocol (Internet Internal object Request Proxy Protocol)) because EJB is distributed technology, it allows the client and EJB to be applied on different machines, so these performance overhead is inevitable.
But in real life we cannot avoid this situation, that is, the client and the EJB application running in the same jboss, then it is necessary to go with the EJB between the above network communication? In fact, it is not necessary, they can be completely interactive through memory. This avoids the performance overhead and introduces a local interface. The EJB is called through the local interface and interacts directly with the memory.
However, it is important to note that the invocation of the local interface, the EJB and the client calls are all run within a JVM to invoke the local interface, or it can only be called the remote interface, and it may be asked, what is running in a JVM? The simplest understanding is that as long as the EJB and the client are published within the same JBoss, we assume that the local interface is called.
At this point, the previous interface was changed to local pop-up, and the remote interface could not be found when the call occurred again.
developing a Web client
The code is as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%@ page import= "javax.naming.*, baidu.com.ejb.* "%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Because you write code instead of HTML on a JSP page, you need to use <%%> to write code behind the scenes.
2. Annotation method injected into other EJBs After describing the call to the local interface, let's look at how to inject other EJB services.
Re-declare an interface and implementation class as other
Other interface
Package Baidu.com.ejb;public interface Other {String sayme ();}
Otherbean
Package Baidu.com.ejb.impl;import Javax.ejb.stateless;import baidu.com.ejb.other;//is called locally by default @statelesspublic class Otherbean implements other {@Overridepublic String Sayme () {return ' other ';}}
The first is Jndi lookup
@Stateless @local (helloworld.class) public class Helloworldbean implements HelloWorld {///This is a wrong way to get the EJB object, Instead of just a Java object//private Other other = new Otherbean ();//The second is EJB injection, the implementation of the principle is: according to the interface type to find out if there is an interface to implement the EJB, the search results will be injected into the EJB// What if I find more than one? explicitly indicate which of the calls is to be made. One way is to modify the implementation bean name so that it becomes unique, another way @overridepublic string SayHello (string name) {//The first call is: Jndi lookup initialcontext Ctx;try { CTX = new InitialContext (); Other other = (other) ctx.lookup ("otherbean/local"); return name + "Say hello, world!" "+ Other.sayme ();} catch (Namingexception e) {e.printstacktrace ();} InitialContext CTX = new InitialContext (); return name;}}
the second type is annotation injection @Stateless @local (helloworld.class) public class Helloworldbean implements HelloWorld {///This is a wrong way to get the EJB object, Instead of just a Java object//private Other other = new Otherbean ();//The second is EJB injection, the implementation of the principle is: according to the interface type to find out if there is an interface to implement the EJB, the search results will be injected into the EJB// What if I find more than one? explicitly indicate which of the calls is to be made. One way is to modify the name of the implementation bean to make it unique, another way to indicate which one to use (beanname= "Otherbean") @EJB (beanname = "Otherbean") Other other;@ Overridepublic string SayHello (string name) {///The first invocation method is: Jndi lookup try {return name + "Say hello, world! "+ Other.sayme ();} catch (Namingexception e) {e.printstacktrace ();} return name;}}
In addition we can also use other injections, this self can be personally practiced.
3. SummaryIn this case, the EJB explanation is part of the explanation, in the next study will continue to deepen, more with you to share and exchange, there is an article is the explanation of the entity Bean, please continue to pay attention to ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Tour EJB (4)--How to inject other EJB services