DALILA BURHAN 142568 NAZIRAH SHARIFUDDIN 142554
description
Transcript of DALILA BURHAN 142568 NAZIRAH SHARIFUDDIN 142554
ENTERPRISE JAVABEANS
(EJB)
Introduction
The Enterprise JavaBeans architecture is a component architecture for the development and deployment of component-based distributed business applications. Applications written using the Enterprise JavaBeans architecture are scalable, transactional, and multi-user secure. These applications may be written once, and then deployed on any server platform that supports the Enterprise JavaBeans specification.
Context: Three-tier Architectures
Presentation Business Logic Backend
Database
Database
First Tier Middle Tier Third Tier
Implementing Distributed Objects using Stubs and
SkeletonsClient
STUB
Server
SKELETON
ObjectServerNETWOR
K
2. Communicatemethod invoked
4. Communicatereturn value
1. Client invokes
a method
5. Return result
3. Invoke on server
Distributed object architectures are based on a network communication layer. Essentially, there are three parts to this architecture: the object server, the skeleton, and the stub.
The object server is the business object that resides on the middle tier.
The stub and the skeleton are responsible for making the object server, which lives on the middle tier, look like if it is running locally on the client machine. This is accomplished through some kind of remote method invocation protocol.
The stub acts as the object server’s surrogate on the client. This means from the outside that the stub must look like the Object server which means the stub implements the same interface. However it doesn’t contain any business logic, it only implements the networking operations that are required to forward the request to the object server and receive the results.
Different Types Within Business LogicA bean implements business logic:
Business process SessionBean
Business data EntityBean
Remote Interface
Home Interface
Primary Key
StatefulStateless
Entity bean might represent a customer, a piece of equipment, an item in inventory, or even a place. In othter words, entity beans model real-world objects; these objects are usually persistent records in some kind of database.
Session beans are an extension of the client application and are responsible for managing processes or tasks (making a reservation, …). A session bean doesn’t represent something in a database. Obviously, session beans have lots of side effects on the database.
The remote interface for an enterprise bean defines the bean’s business methods: the methods a bean presents to the outside world to do its work. The methods in this interface can be used by the client and the methods are implemented by the bean class.
The home interface defines the bean’s life cycle methods: methods for creating new beans, removing beans and finding beans
Message Driven Beans
Message listener interface that act in the business interface
No requirement to implement on other interface can process JMS messages or other kinds of
messages.
Message-driven beans have the following characteristics: They execute upon receipt of a single client
message. They are invoked asynchronously. They do not represent directly shared data in the
database, but they can access and update this data.
They can be transaction-aware. They are stateless.
Enterprise JavaBeans Architecture
Client EJB Server
EJB Container
Home interface
Remote interface Bean class
EJB object
EJB home
Red: define your selfYellow: automatically generated
Client code
Bean Deployment
Beans are deployed using JAR (Java ARchive) files.
JAR file
Deployment descriptor
Class files
Resource Management
Session beans reduce network traffic and thin down clients
EJB SERVER
Client using only Entity beans• Logic is in client• Clients does lots of small updates on entity beans
Client using Session beans• Logic is in the session beans• Client only gives small number of commands to Session beans
Conclusion EJB is About Hiding EJB hides a lot of things for the
developer. Such that the developer can focus on the business logic of the application.
EJB hides:• Distribution of objects
• Resource Management
• Transactions
• Security
• Concurrency
Sun Java System Application Server 9.1 targeted as the build and runtime
environment . to build, deploy, and run the program
Starting and Stopping the Application Server
To start the Application Server
asadmin start-domain --verbose domain1 When completed :
Domain domain1 started.
To stop the Application Server
asadmin stop-domain domain1 When the server has stopped :
Domain domain1 stopped.
Starting the Admin Console
To administer the Application Server and manage users, resources, and Java EE applications
The Application Server must be running before invoke the Admin Console.
To start, open a browser athttp://localhost:4848/asadmin/
Starting and Stopping the Java DB Database Server The App Server includes Java
Database Server
To start :asadmin start-database
To stop asadmin stop-database
Building Using NetBeans IDE Register your Application Server installation as a
NetBeans Server Instance :
1. Select Tools→Server Manager to open the Server Manager dialog.
2. Click Add Server.
3. Under Platform Location, enter the location of your Application Server installation.
4. Select Register Local Default Domain and click Next.
5. Under Admin Username and Admin Password, enter the admin name and password created when you installed the Application Server.
6. Click Finish.
Getting Started with Enterprise Beans
The purpose of converter is to calculate currency conversions between Japanese yen and Eurodollars. Create the enterprise bean: ConverterBean. Create the application client:
ConverterClient. Create the web client in converter-war. Deploy converter onto the server. Run the application client. Using a browser, run the web client.
Coding the Business Interface
package com.sun.tutorial.javaee.ejb; import java.math.BigDecimal;import javax.ejb.Remote;
@Remotepublic interface Converter {
public BigDecimal dollarToYen(BigDecimal dollars);
public BigDecimal yenToEuro(BigDecimal yen);
}
Coding the Enterprise Bean Classpackage com.sun.tutorial.javaee.ejb;
import java.math.BigDecimal;import javax.ejb.*;
@Statelesspublic class ConverterBean implements Converter {private BigDecimal yenRate = new
BigDecimal("115.3100");private BigDecimal euroRate = new BigDecimal("0.0071");
public BigDecimal dollarToYen(BigDecimal dollars) {BigDecimal result = dollars.multiply(yenRate);return result.setScale(2, BigDecimal.ROUND_UP);}
public BigDecimal yenToEuro(BigDecimal yen) {BigDecimal result = yen.multiply(euroRate);return result.setScale(2, BigDecimal.ROUND_UP);}}
Compiling and Packaging in NetBeans IDE
1. In NetBeans IDE, select File→Open Project.
2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/ejb/.
3. Select the converter folder.4. Select the Open as Main Project and
Open Required Projects check boxes.5. Click Open Project.6. In the Projects tab, right-click the
converter project and select Build. You will see the output in the Output tab.
Creating Application Clientpackage com.sun.tutorial.javaee.ejb;
import java.math.BigDecimal;
import javax.ejb.EJB;
public class ConverterClient {
@EJB
private static Converter converter;
public ConverterClient(String[] args) {
}
public static void main(String[] args) {
ConverterClient client = new ConverterClient(args);
client.doConversion(); }
public void doConversion() { try { BigDecimal param = new BigDecimal("100.00"); BigDecimal yenAmount = converter.dollarToYen(param); System.out.println("$" + param + " is " + yenAmount + " Yen."); BigDecimal euroAmount = converter.yenToEuro(yenAmount); System.out.println(yenAmount + " Yen is " + euroAmount + " Euro."); System.exit(0); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); }
}}Compiling -The application client files are
compiled at the same time as the enterprise bean files.
Creating Web Client
<%@ page import="converter.ejb.Converter, java.math.*, javax.naming.*"%>
<%!
private Converter converter = null;
public void jspInit() {
try {
InitialContext ic = new InitialContext();
converter = (Converter) ic.lookup(Converter.class.getName()); }
catch (Exception ex) {
System.out.println("Couldn’t create converter bean."+ ex.getMessage());
}
}
public void jspDestroy() {
converter = null;
}%>
<html>
<head>
<title>Converter</title>
</head>
<body bgcolor="white">
<h1>Converter</h1>
<hr>
<p>Enter an amount to convert:</p>
<form method="get">
<input type="text" name="amount" size="25">
<br>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
<%
String amount = request.getParameter("amount");
if ( amount != null && amount.length() > 0 ) {
BigDecimal d = new BigDecimal(amount);
BigDecimal yenAmount = converter.dollarToYen(d);
%>
<p>
<%= amount %> dollars are <%= yenAmount %> Yen.
<p>
<%
BigDecimal euroAmount = converter.yenToEuro(yenAmount);
%>
<%= amount %> Yen are <%= euroAmount %> Euro. <%
}
%>
</body>
</html>
Compiling - The Application Server automatically compiles web clients
Deploying in NetBeans IDE1. In NetBeans IDE, make sure the
converter application is open.2. In the Projects tab, right-click the
converter project and select Undeploy and Deploy. You will see the output in the Output tab.
Running Application Server in NetBeans IDE
1. In NetBeans IDE, make sure the converter application is open.
2. In the Projects tab, right-click the converter project and select Run. You will see the following output in the Output tab:
... $100.00 is 11258.00
Yen. 11258.00 Yen is 78.81
Euro. ...
Running Web Client To run the web client, point your browser at
the following URL. Replace host with the name of the host running the Application Server. If your browser is running on the same host as the Application Server, you can replace host with localhost.• http://host:8080/converter After entering 100 in the input field and
clicking Submit, you should see the screen shown in