320 likes | 598 Views
CG0165: Advanced Applications Development in Java. Introduction to Enterprise JavaBeans. Enterprise Computing -- 3-tier and multi-tier applications Enterprise JavaBeans EJB Features Types of EJBs Example – A Currency Converter Packaging the Session EJB Deploying the Bean
E N D
CG0165: Advanced Applications Development in Java Introduction to Enterprise JavaBeans • Enterprise Computing -- 3-tier and multi-tier applications • Enterprise JavaBeans • EJB Features • Types of EJBs • Example – A Currency Converter • Packaging the Session EJB • Deploying the Bean • Running the Client Michael Brockway, Sajjad Shami School of Computing, Engineering & Information Sciences Northumbria University
Introduction to EJBs • References: • Tutorial and reference material from the sun web site - Enterprise Edition pages • http://java.sun.com/javaee/5/docs/tutorial/doc/ • http://java.sun.com/javaee/5/docs/tutorial/doc/JavaEETutorial.pdf • http://docs.sun.com/app/docs/doc/819-3656/6n5s3qjg2?a=view • The relevant material in the Tutorial is in chapter 1: Overview, chapter 20: Enterprise Beans, and chapter 21: Getting Started with Enterprise Beans
Enterprise Computing • Same as distributed computing: • computation done by groups of programs communicating over a network • Not necessarily a major corporation, university, government agency … • Small businesses also engage in plenty of distributed computing • The architecture of Enterprise applications can be thought of as consisting of three logical tiers • Presentation (Client side, User) • displays data to client • Application • provides business logic to Presentation tier • holds business components & objects • Data Systems • central data storage/ retrieval
Application Tier Presentation Tier Data Systems Tier/ EIS /Client Backend / backoffice Database/ legacy sys Application 1 Application 2 Business components Application 3 Three Tiers Server
Multi-tier applications • This architecture generalises: • In general an enterprise application involves client-to-server communication. Inventory DB server Goods purchasing system 1 Client(s) Stock control server Goods purchasing system 2 . . . ... Goods purchasing system n = data systems tier = application tier
Multi-tier applications • For a rationale, imagine a client who manages an inventory for a chain of shops • Has web-based (thin client) interfaces to a number of server-based applications • the chain's own inventory • the goods purchasing system for supplier no 1 • the goods purchasing system for supplier no 2 • .... • the goods purchasing system for supplier no n • There is a set of logical rules: one for each inventory item, which causes a certain quantity of the item to be ordered-in (from the appropriate supplier) when stocks fall below a pre-determined threshold. • It would clearly be silly for all this logic to occur in the client • Better: • The goods ordering logic is implemented on a server which talks to all the other servers
Multi-tier applications • A further development is the Thin Client • The clients run web browsers or other very light-weight software • The presentation tier resides in a web server Application server communication with clients Client(s): HTTP requests, XHTML responses Application server Web server . . . servlets, JSPs Application server Business logic
Enterprise JavaBeans (EJBs) • (Other) Technologies for implementing business & presentation logic in multi-tier applications: • Java Servlets • JSPs • Enterprise JavaBeans are a technology for the application server side • and provide a component model for building business logic in enterprise Java applications • References: • Deitel, Deitel & Santry Advanced Java 2 HTP, chap 14 • Tutorial and reference material on the SUN web site - Enterprise Edition pages • They provide a component model • Component developers write component "building blocks" that implement business logic • Application developers hook up these pre-built components into finished applications, which may themselves be components. • Not to be Confused with JavaBeans!!!
Java EE Components • Java EE applications are made up of components • A Java EE component is a self-contained functional software unit that is assembled into a Java EE application • with its related classes and files and that communicates with other components. • The Java EE specification defines the following Java EE components: • Application clients and applets are components that run on the client. • Java Servlet, JavaServer Faces, and JavaServer Pages™ (JSP™) technology components are web components that run on the server. • Enterprise JavaBeans™ (EJB™) components (enterprise beans) are business components that run on the server.
Java EE Clients • Web Client • two parts: • (1) dynamic web pages containing various types of markup language (HTML, XML, etc), which are generated by web components running in the web tier, and • (2) a web browser, which renders the pages received from the server. • A web client is sometimes called a thin client. • Applets • A web page received from the web tier can include an embedded applet. • An applet is a small client application written in the Java programming language • that executes in the Java virtual machine installed in the web browser
Java EE Clients …contd. • Application Client • runs on a client machine • typically has a graphical user interface (GUI) • a command-line interface is also possible. • directly access enterprise beans running in the business tier • can also open an HTTP connection to establish communication with a servlet running in the web tier • application clients written in languages other than Java can interact with Java EE 5 servers • enabling the Java EE 5 platform to interoperate with legacy systems, clients, and non-Java languages.
EJB Features • EJBs improve distributed transactions • By ensuring data integrity across databases and application servers • EJBs utilise Java EE’s distributed transaction features and update data across several databases • To develop an enterprise bean, the following files are required: • Enterprise bean class: Implements the methods defined in the business interface. •Business Interfaces: The business interface defines the method implemented by the enterprise bean class. • Helper classes: Other classes needed by the enterprise bean class, such as exception and utility classes.
Packaging of an Enterprise Bean • The three files are packaged into an EJB JAR file, the module that stores the enterprise bean. An EJB JAR file is portable and can be used for different applications. • To assemble a Java EE application, package one or more modules—such as EJB JAR files—into an EAR file, the archive file that holds the application. • When deploying the EAR file that contains the bean’s EJB JAR file, also deploy the enterprise bean to the Application Server. • Note: An EJB JAR that is not contained in an EAR file can also be deployed
Advantages of Enterprise Beans • enterprise beans simplify the development of large, distributed applications: • 1) as the EJB container provides system-level services to enterprise beans, the bean developer can concentrate on solving business problems • 2) the client developer can focus on the presentation of the client • does not have to code the routines that implement business rules or access databases. • result: clients are thinner • 3) the application assembler can build new applications from existing beans • can run on any compliant Java EE server
When to Use Enterprise Beans • the application must be scalable. • to accommodate a growing number of users, may need to distribute an application’s components across multiple machines. • transactions must ensure data integrity • Enterprise beans support transactions, the mechanisms that manage the concurrent access of shared objects • the application will have a variety of clients • with only a few lines of code, remote clients can easily locate enterprise beans. • these clients can be thin, various, and numerous.
The EJB Implementation and the EJB Container • The EJB Implementation • implements the business logic methods declared in the EJB remote interface • The EJB Container • within the application server • manages client interactions • invocations from a client first go to the EJB container which then delegates to the EJB Implementation • manages life cycles of its EJBs • can create new EJB instances and remove existing ones
Types of EJBs • Session EJBs • distributed objects which do jobs within the business logic / application server • can manipulate data in a database • ...but are not persistent • lost if EJB container crashes • two types: • Stateful • Stateless • Message-driven EJBs – will not be discussed in detail
Sun Java System Application Server Platform Ed. 9 • is a fully compliant implementation of the Java EE 5 platform. • In addition to supporting all the APIs the Application Server includes a number of Java EE tool and instructions • for starting and stopping the Application Server, starting the Admin Console, and starting and stopping the Java DB database server. • Admin Console • asadmin • asant • appclient • …..
A First EJB Example – A Currency Converter • See listings given out with the lecture notes • a stateless Session Enterprise Java Bean with remote interface. • a very primitive application: has just two business methods • a method to convert dollars to yen • a method to convert yen to euro. • you will get an opportunity to upgrade this application as a practical exercise. • Converter.java • the remote interface for the Enterprise Bean • declares the business methods described above.
Converter.java package converter.ejb; import java.math.BigDecimal; import javax.ejb.Remote; @Remote public interface Converter { public BigDecimal dollarToYen(BigDecimal dollars); public BigDecimal yenToEuro(BigDecimal yen); }
Example – Currency Converter • ConverterBean.java • the EJB implementation • a stateless session EJB for performing currency conversions • in effect, this class implements the business methods declared in Converter remote interface
ConverterBean.java package converter.ejb; import java.math.BigDecimal; import javax.ejb.Stateless; @Stateless public class ConverterBean implements converter.ejb.Converter { private BigDecimal euroRate = new BigDecimal("0.0070"); private BigDecimal yenRate = new BigDecimal("112.58") 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 • compile the remote business interface (Converter.java) and the enterprise bean class (ConverterBean.java), and package the compiled classes into an enterprise bean JAR. • 1. In a terminal window, go to this directory: <INSTALL>/javaeetutorial5/examples/ejb/converter/ • 2. Type the following command: ant
The Application Client • is a program written in the Java programming language • at runtime, the client program executes in a different virtual machine than the Application Server • The application client in this example requires two JAR files. • The first JAR file is for the Java EE component of the client. This JAR file contains the client’s deployment descriptor and class files • The second JAR file contains all the classes that are required by the client program at runtime. These classes enable the client to access the enterprise beans that are running in the Application Server.
ConverterClient.java package converter.client; import converter.ejb.Converter; import java.math.BigDecimal; import javax.ejb.EJB; public class ConverterClient { @EJB private static Converter converter; /** Creates a new instance of Client */ public ConverterClient(String[] args) { } /** * @param args the command line arguments */ public static void main(String[] args) { ConverterClient client = new ConverterClient(args); client.doConversion(); }
ConverterClient.java … contd. 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(); } } }
Running the Application Client • ant • ant deploy • ant run • In the terminal window, the client displays these lines: ... $100.00 is 11531.00 Yen. 11531.00 Yen is 81.88 Euro. ... • That's it! you have deployed the primitive EJB application in the application server, and used a client program to connect to it.
Extension • The Converter application is an example provided as part of Sun's Java EE tutorial: • http://java.sun.com/javaee/5/docs/tutorial/doc/ • You can make the Converter much more versatile and useful than this – refer to the practical work sheet.