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.*;

/** * A simple, stateless session bean implements the CalculateEJB interface incrementValue () Method **/

@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'") } )

/** * Listen to configure JMS queue or topic and by when a message is sent to the queue or topic * Call its onMessage () Method to get a reminder a simple message-driven * The bean print the content of the message */

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;

/** * A call to a stateless session bean methods of simple Bean client */

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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of ejb (1)

  • What is the JPA

    Same, JDO, also started compatible JPA. At the field of ORM, it seems that JPA is a benevolent government, is the normative specification. At the support of major manufacturers, JPA use became widespread. 2 Spring Spring + Hibernate often referred to ...

  • J2EE Architect road

    J2EE Architect road Looking casual, working into the fifth year, the discovery came from the Java programmer to architect J2EE course. Found that computer to install a wide range of J2EE tools: JBuilder, WSAD, Eclipse, Rose, Together, Weblogic, Jtest ...

  • EJB

    public transient int counter; / / transient property private String firstname; / / persistent property @ Transient String getLengthInMeter () (...) / / transient property String getName () (...) / / persistent property @ Basic int getLength () (...) ...

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Messag ...

  • Java technology 25 study points

    1. You need to master the object-oriented analysis and design (OOA / OOD), involving patterns (GOF, J2EEDP) as well as the integrated model. You should understand the UML, especially class, object, interaction and statediagrams. 2. You need to learn basic

  • In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...

  • Based on JDBC, JPA Annotation achieve simple CRUD Generic Dao

    The origin of ideas are pretty long history of reasons: [Use iBATIS history] The use of iBATIS has been a long time, the system is to use the CRUD template tool to generate the code, although there are tools to generate, but looked at a lot of CRUD the Sq

  • Java Technology wishing cow needed 25 points of study

    1. You need to master the object-oriented analysis and design (OOA / OOD), involving patterns (GOF, J2EEDP) as well as the integrated model. You should understand the UML, especially class, object, interaction and statediagrams. 2. You need to learn basic

  • J2EE questions Noodles

    2. Abstract class and interface difference (1) interface can be multiple implements, can only be a single abstract class extends (2) only the definition of interfaces, abstract class can have the definition and implementation (3) the definition of th ...

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries