1, What is EJB?
EJB(Enterprise Java Beans), the Commercial application component technology in java EE , is java EE three major components ( Servlet , JSP , EJB ) one. EJB provides a framework for clients to use remote distributed objects, which greatly simplifies the development of enterprise-class applications with good extensibility. the EJB component structure is a distributed computing structure based on component and a component in distributed Application System.
EJB is Java the specification of server-side service framework defines a technical specification for server-side component system. This specification provides a standard, distributed, object-oriented architecture. It masks complex system-level low-level functionality implementations for component developers and users, and enables developers to focus on the implementation of business logic, some of which are complex, and are handled by EJB containers. EJB can be expanded according to the application growth,theEJB Server provides the load balancing function, as well as the resources access control.
2. communication between EJB container and component
the EJB container provides a running environment for the EJB component, EJB Container Management EJB in the same way as Web Container Management Servlet in a similar way, EJB must be in EJB the container runs. The EJB container primarily manages issues such as persistence, lifecycle management, security management, transaction management, remote connectivity, concurrency processing, clustering, and load balancing for EJB . The container manages instances of the EJB component, enabling the EJB component to achieve maximum performance and memory utilization. The container can activate and deactivate the EJB component, manage instance pool, and so on. The container is responsible for managing the complex issues of distributed transactions, managing low-level communication problems for remote connections, and concealing communication problems for both developers and customers of the EJB component. Therefore, the EJB component developer can focus on encapsulating business logic, and the container handles everything else. EJB through ejbcontent , jndj , Mechanisms such as callback functions interact with containers.
JBoss is a container and server for managing EJBS, supporting EJB1.1,EJB2.0 , and EJB3 The specification, which is typically used with Tomcat or Jetty bindings.
Here's a look at Figure one ( the working diagram of the EJB container):
Figure I: How theEJB Container works
An EJB component is a distributed object that, when instantiated, can communicate with applications in other address spaces. an EJB instance is encapsulated in a framework (skeleton) object thatcommunicates with the client through a stub object. The stub does not include business logic, but implements the business interface. Whenever a business method on a stub commercial interface is invoked, the stub sends the network message to the framework telling it which methods are invoked. The framework invokes the corresponding method of the EJB instance and sends The result returned by the EJB instance to the stub, which is then returned to the appropriate application by the stub. The complex communication process between distributed objects is shielded by the two intermediate objects, stub and frame. The framework is implemented by the container, and the stub is generated automatically by the development tool, neither of which requires coding. Here's a look at Figure two (EJB component communication schematics):
Figure II: Principles of communication between EJB components
3.EJB Classification
the EJB component can be divided into session beans and message-driven beans in two kinds. The session Bean encapsulates business logic, and the client can invoke the session Bean in a local, remote, Web service Way method to access the application deployed on the server, thus invoking the method of the other Bean , which is not persistent, that is, its data is not persisted in the database. Where the session Bean also includes stateful session Beans, stateless session beans , and a single session Bean three species. Message-driven beans are typically used as listeners for specific types of messages, so that Java EE can handle asynchronous messages, and clients do not access message-driven through interfaces Bean.
The following describes, in turn, stateful session beans, stateless session beans, single session beans , and message-driven sessions Bean .
4, stateless session Bean
stateless session Beans provide business logic only for clients and do not preserve session state for clients. When the client invokes a method for a stateless session Bean , the corresponding session Bean 's properties describe the invocation state, but only during the method invocation. When the method call is finished, the state is cleared.
The lifecycle of a stateless session Bean is controlled by the container. When the EJB container receives a request from the client for a stateless session Bean , if the EJB does not exist, the container creates a the instance of the Bean , then injects the required resources into the component, then the container recalls The Postconstruct method, and the component is created. At this point,theBean transitions from the nonexistent state to the existing state. The end of the client call, the container callback Predestroy method,and theBean is destroyed, at which pointthebean is removed from the There is a status transition to nonexistent state. Here's a look at figure three ( The lifecycle of a stateless session Bean):
Figure three: the lifecycle of a stateless session Bean
5, stateful session Bean
A stateful session Bean retains a session state for the user, and it cannot be put into a component pool to be shared by different users like a stateless session bean. For stateful session beans , whenever a client sends a request, the container creates an instance that corresponds to the client and one client corresponds to an instance. During the lifetime, stateful session beans Hold the user's information, and once the session is over, the lifecycle of the stateful session Bean ends.
Stateful session Beans have three active states: not present, active, and passivated. When a stateful session Bean is active for a period of time, if the request is still not received from the external client, in order to conserve system resources, the container will put the stateful session Bean The state information in is serialized to temporary storage and the stateful session Bean is removed from memory, a process known as "passivation." Before passivation, the container recalls the prepassivate method. When the container receives a request for a stateful session Bean that has been deactivated, the instance of the stateful session Bean is reinitialized and the state information is removed from the temporary space. To bring it back to the active state, this process is called "activation." After activation, the container recalls the proactivate method. When a stateful session Bean is deactivated for a period of time, the container completely clears the instance and recalls the Predestroy method. Here's a look at figure four (the lifecycle of a stateful session Bean ):
Figure IV: The lifecycle of a stateful session Bean
6. Single session Bean
A single session Bean instantiates each application once and persists throughout the lifecycle of the application. A single session Bean is designed for a particular scenario, and the client can access the unique EJB instance in a shared and concurrent mode .
A single session Bean is very similar to a stateless session Bean, except that a single session Bean has only one instance in the application and no stateful session the Bean can have many instances, each of which is placed in the component pool and shared by the user.
A single session Bean , like a stateless session Bean , is never deactivated. Its lifecycle contains only two types: "Not exist" and "exist." Below, see figure Five ( Lifecycle of a single session Bean):
Figure five: life cycle of a single session Bean
7. message-driven Bean
Message-driven beans are designed to deal specifically with message-based requests for components. The message-driven Bean integrates the Java messaging Service (Java message Services,JMS ) and Enterprise Bean the function. The method cannot be invoked directly by the client and can only be started by a system message.
The EJB container typically creates a Component Pool of message-driven beans. Like a stateless session Bean , a message-driven Bean is never deactivated, and its lifecycle contains only two phases: nonexistent and present.
The message-driven Bean class must implement the messagelistener interface. When a container detects A message in a queue that the Bean listens to, it calls the onMessage () method and passes the message as a parameter.
Above this Java-EE basic EJB Understanding is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.