CodeWeblog.com » bean entity,business logic,entity bean » ejb (1)

ejb (1)

What is EJB?

An enterprise JavaBean (EJB) is a reusable, portable J2EE components. EJB from the packaging of many possible approaches to business logic components. For example, an EJB can include a customer database to update data of the business logic. A number of remote and local client can call this method. In addition, EJB running in a container, allowing developers to focus on with the business logic bean without considering matters such as support, security, and remote object access, such as complex and error-prone things. EJB to POJO or general form of the old Java object development, developers can use the Notes to the definition of meta-data container how to manage these Bean.

EJB types

There are three types of EJB: Session Bean, Entity Bean and Message-Driven Bean. Session Bean to complete a clear decoupling of tasks, such as checking customer account history. Bean is a representative of entities exist in the database business object complexity of the business entity. Message-Driven Bean to receive asynchronous JMS messages. Let us a more detailed understanding of these types.

Session Bean (method)

General Session Bean represents a business process as "process an order" such action. Session Bean based on whether the state is divided into the maintenance of excessive or stateful stateless.

Stateless Session Bean is not the middle of the state. They do not keep track of a method call message another way. Therefore a state of operational methods are independent of each call in a call before it; for example, the transfer of accounts receivable or the calculation of taxes and fees. When the method of calculating the amount of taxes and charges is called, the value of taxes and fees are calculated and returned to the calling method, there is no need to store the caller to call back-up for the future of the internal state. Because they do not safeguard the state, it is only by these Bean container management. When the client requests a stateless Bean instance, it can receive free non-container managed session Bean instance of a state of concentration of a case in point. Because stateless session Bean can be shared, so the number of containers can be less maintenance for a large number of examples of client services. Bean as a simple increase in the Notes @ Stateless element to specify a Java Bean as a stateless Session Bean is deployed and managed.

A Bean to maintain session state across multiple method calls one of the session state; such as online shopping cart application. When customers shopping online, customers receive detailed information from the database. When the same information for customers from the shopping cart to add or remove the goods, etc. is called when other methods are also accessible. But because the state is not the end of the session, system crashes or the network fails to retain, there is a state of the session Bean is temporary. When a client requests a session Bean instance of the state, the client will be an example of a conversation, the state of the Bean only to the client to maintain. Million to increase the adoption of methods to tell the Notes @ Remove the container when a method call the state of the end of a session Bean instance should be removed.

Session Bean Examples

import javax.ejb.Stateless.*;

/** * 一个简单无状态会话Bean实现了CalculateEJB接口的incrementValue()方法 **/

@Stateless(name="CalculateEJB") public class CalculateEJBBean implements CalculateEJB { int value = 0; public String incrementValue() { value++; return "value incremented by 1"; } }


Entity Bean

Bean Managed Persistence entity is an object of data, the potential use of a number of Java objects and can rely on the primary key was unique identifier. @ Entity, including through the designation of a million notes to an entity type is Bean. Bean said that the database entities from the persistent data, such as a customer record in the table, or a table of employee records of an employee. Entity Bean can also be shared by multiple clients. For example, if an employee entity can be calculated more than the total amount of employee wages or an annual update addresses the client staff to use. Bean object entity-specific variables that can maintain persistent. Bean all entities not annotated @ Transient million variables to be considered persistent. EJB3.0 is one of the main characteristics include the use of meta-data to create annotated object / relational mapping entity's ability to Bean. For example, the designated entity Bean's empId variable is mapped to the employee table EMPNO attribute, such as the following examples use the same @ Table (name = "Employees") Note this table with the name and @ Column (name = "EMPNO") Notes empId variables. In addition, EJB3.0 in a feature that you can easily test of time in the development entity Bean, can use Oracle Application Server Entity Test Harness to run in the container outside of an entity Bean.

Entity Bean example

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;

@Entity @Table(name = "EMPLOYEES") public class Employee implements java.io.Serializable { private int empId; private String eName; private double sal;

@Id @Column(name="EMPNO", primaryKey=true) public int getEmpId() { return empId; }

public void setEmpId(int empId) { this.empId = empId; }

public String getEname() { return eName; }

public void setEname(String eName) { this.eName = eName; }

public double getSal() { return sal; }

public void setSal(double sal) { this.sal = sal; }

public String toString() { StringBuffer buf = new StringBuffer(); buf.append("Class:") .append(this.getClass().getName()).append(" :: ").

append(" empId:").append(getEmpId()).append(" ename:").

append(getEname()).append("sal:").append(getSal()); return buf.toString(); } }


Message-Driven Bean

Driven Bean (MDB) provides a realization of asynchronous communication than the direct use of Java Message Service (JMS) method more easily. MDB created to receive asynchronous JMS messages. Container handling for JMS queues and topics required to deal with most of the work load. It MDB to send all relevant information. MDB allows a J2EE application to send asynchronous messages, the application can handle such news. Achieve javax.jms.MessageListener interface and use the Bean Notes @ MessageDriven to specify a message-driven Bean is Bean.

Examples of Message-Driven Bean

import javax.ejb.MessageDriven;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.Inject;
import javax.jms.*;
import java.util.*;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import javax.ejb.TimerService;
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName="connectionFactoryJndiName", 

propertyValue="jms/TopicConnectionFactory"), @ActivationConfigProperty(propertyName="destinationName",

propertyValue="jms/myTopic"), @ActivationConfigProperty(propertyName="destinationType",

propertyValue="javax.jms.Topic"), @ActivationConfigProperty(propertyName="messageSelector",

propertyValue="RECIPIENT = 'MDB'") } )

/** *监听可配置JMS队列或者主题和通过当一个消息发送到队列或者主题 *调用它的onMessage()方法得到提醒的一个简单的消息驱动 *该Bean打印消息的内容 */

public class MessageLogger implements MessageListener, TimedObject {

@Inject javax.ejb.MessageDrivenContext mc;

public void onMessage(Message message) { System.out.println("onMessage() - " + message); try { String subject = message.getStringProperty("subject"); String inmessage = message.getStringProperty("message"); System.out.println("Message receivedntDate: " + new java.util.Date() +

"ntSubject: " + subject + "ntMessage: " + inmessage + "n"); System.out.println("Creating Timer a single event timer"); TimerService ts = mc.getTimerService(); Timer timer = ts.createTimer(30000, subject); System.out.println("Timer created by MDB at: " +

new Date(System.currentTimeMillis()) +" with info: "+subject); } catch (Throwable ex) }

public void ejbTimeout(Timer timer) { System.out.println("EJB 3.0: Timer with MDB"); System.out.println("ejbTimeout() called at: " +

new Date(System.currentTimeMillis())); return; } }


The use of EJB

Bean is a visit to the client applications. Although there is no need to save the client layer, but can be used as a standalone application, JSP, Servlet, or another EJB. Bean client through the remote or local EJB interface methods, mainly depends on the client and the Bean running in the same or in a different JVM. These interfaces define the methods of the Bean by Bean type of the actual realization of these methods. When a client visits the Bean class a method, the container generates a proxy of the Bean, the local or remote object is called object. Remote or local objects receive requests, assigned it to the Bean instance, return the results to the client. To invoke a method in Bean, the client is not using the definition described in the EJB find the name Bean. In the following example, the use of the context of the client to find the object named "StateLessejb" Bean.

Examples of EJB client

import javax.naming.Context;
import javax.naming.InitialContext;

/** * 一个调用无状态会话Bean中方法的简单的Bean客户端 */

public class CalculateejbClient { public static void main(String [] args) { Context context = new InitialContext(); CalculateEJB myejb = (CalculateEJB)context.lookup("java:comp/env/ejb/CalculateEJB"); myejb.incrementValue(); } }


Summary

Enterprise JavaBean development of EJB 3.0 is pretty easy. This normative definition of the use of meta-data Notes Bean exposed to the type and method of the client. Therefore, whether you will perform a specific task to create a session Bean is a table mapping the entity Bean to update data, you can use as ordinary as Java objects and interfaces to deal with business methods in the use of metadata to the Notes client exposure method . Since the EJB you have to understand the basis for the OTN can be in the EJB 3.0 Resources Page to find more information.
Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: bean entity (RSS), business logic (RSS), entity bean (RSS), business object (RSS), ejb (RSS), java object (RSS), j2ee components (RSS), operational methods (RSS), stateless session bean (RSS), meta data (RSS), customer database (RSS), decoupling (RSS), bean container (RSS), business entity (RSS), logic components (RSS), jms messages (RSS), container management (RSS), database business (RSS), account history (RSS), support security (RSS)

Permalink: http://www.codeweblog.com/ejb-1/

Leave a reply