Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Java programming (Java EE)

Similar presentations


Presentation on theme: "Advanced Java programming (Java EE)"— Presentation transcript:

1 Advanced Java programming (Java EE)
Advanced Internet Programming - EJB Spring 2001 Advanced Java programming (Java EE) Enterprise Java Beans (EJB) Chris Wong, Ryan Heise [based on prior course notes & the Sun J2EE tutorial] © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

2 32549 - Advanced Internet Programming - EJB
Enterprise Java Beans Spring 2001 Today we will briefly cover  Introduction to EJB EJB basics EJB Architecture Session Beans EJB Clients EJB Development Process © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

3 32549 - Advanced Internet Programming - EJB
Introduction to EJB Spring 2001 So far we have developed multi-tier applications: Web-based Applications (Servlets/JSPs/JDBC) “thin” client – using browser Model-View-Controller architecture simple business logic Some services provided by web container Distributed Applications (RMI/JDBC) “fat” client – using Java application Simple client/server architecture You have to write all other services © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

4 32549 - Advanced Internet Programming - EJB
EJB Introduction Spring 2001 © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

5 32549 - Advanced Internet Programming - EJB
Java EE architecture Spring 2001 © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

6 32549 - Advanced Internet Programming - EJB
EJB Introduction Spring 2001 EJB is the Java EE server-side component model The component software model is based on the idea of creating reusable components that “plug in” to “containers” It addresses some important software development goals: reuse high level development focus development automation via tools simplified deployment JavaBeans, EJB and ActiveX/COM are examples of component models © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

7 32549 - Advanced Internet Programming - EJB
EJB Introduction Spring 2001 Component models come in two basic flavours client-side; and enterprise Client-side component models such as JavaBeans are specialised to handle presentation and user-interface issues Enterprise component models such as Enterprise Java Beans are specialised to provide a framework for developing, deploying and executing distributed, transaction-aware middleware components © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

8 32549 - Advanced Internet Programming - EJB
EJB Introduction Spring 2001 Component developers write component “building blocks” that implement business logic Application developers hook up these pre- built components into finished applications (which may be components themselves) This building block approach facilitates off-the-shelf reuse of code packaged as components © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

9 32549 - Advanced Internet Programming - EJB
Why EJB? Spring 2001 EJB containers provide: Reusability of components Security Resource management Scalability – Load Balancing, Clustering Fault tolerance, Error handling Deployment in other environments Persistence, Independence from specific databases With web applications & RMI, we have to program this. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

10 32549 - Advanced Internet Programming - EJB
Enterprise Java Beans Spring 2001 Today we will briefly cover Introduction to EJB  EJB Basics EJB Architecture Session Beans EJB Clients EJB Development Process © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

11 32549 - Advanced Internet Programming - EJB
EJB – Types of Beans Spring 2001 There are 2 types of Enterprise Java Bean: Session beans Handle a conversation between a client and the server. Each message in the conversation is a method in the bean. Message Driven beans Handles messages from a client Communication is via asynchronous message passing rather than synchronous method calls. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

12 EJB – Types of Session Beans
Advanced Internet Programming - EJB Spring 2001 There are 3 types of session bean: Stateless - Maintains no conversational state Stateful - Maintains conversational state Singleton - Has a single instance shared by all clients. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

13 32549 - Advanced Internet Programming - EJB
EJB Basics Spring 2001 EJB interfaces and classes are defined in the javax.ejb package EJBs are configured and deployed in a similar manner to web applications Packaged into a jar file Optional XML deployment descriptor(s) META-INF/ejb-jar.xml weblogic-ejb-jar.xml © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

14 EJB Standards Evolution
Advanced Internet Programming - EJB EJB Standards Evolution Spring 2001 The EJB specification is an evolving API EJB 1.0 was originally defined as a Java extension in 1998, including definitions for Session beans and (optional) Entity beans EJB 1.1 is the J2EE 1.2 was defined in December as part of the first J2EE platform specification. EJB 1.1 included: Mandatory support for entity beans Enhancements to the deployment descriptor, including support for an XML format Significant tightening of the specification to improve EJB-based application portability EJB 2.0 is the J2EE 1.3 standard. JMS (Java Message Service) integration with Message Driven beans Improved support for container-managed persistence (CMP) Support for RMI/IIOP protocol for network interoperability EJB 3.0 is the Java EE 5 standard (installed at UTS) Significantly simplifed EJB development. Now use Java 5 annotations Replaced Entity EJB with JPA EJB 3.1 is the Java EE 6 standard: pojo EJBs, singleton beans, portable JNDI names. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

15 32549 - Advanced Internet Programming - EJB
EJB Containers Spring 2001 Containers provide services to deployed components so that component developers can concentrate on writing business logic instead of software infrastructure An EJB container is an environment in which Enterprise Java Beans execute. Its primary role is to serve as a buffer between EJBs and the outside world The container simplifies component development by providing services: Transactions Scalability (management of multiple instances) Persistence Security Synchronisation Distributed object invocation Monitoring and management of EJB instances © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

16 32549 - Advanced Internet Programming - EJB
EJB Contract Spring 2001 The services provided by the EJB container are defined by contracts between the container and the various kinds of EJB, for instance, the contract between a “stateless session bean” and the container. e.g The container promises to invoke lifecycle methods on the bean at specified moments. - The stateless session bean promises not to maintain “state”. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

17 EJB and application servers
Advanced Internet Programming - EJB EJB and application servers Spring 2001 We are using a Java EE application server Note that it has two different types of container: Web container (for servlets and JSPs) EJB/Business-logic container (for EJBs) They are logically two separate "tiers" In some cases they are implemented by two separate products, e.g. Tomcat + JBoss Each type of container provides different services © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

18 EJB and application servers
Advanced Internet Programming - EJB EJB and application servers Spring 2001 © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

19 EJB Containers - services
Advanced Internet Programming - EJB EJB Containers - services Spring 2001 EJB Containers must provide the following services Transaction management Security services Multiple instance support © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

20 Container Services – Transactions
Advanced Internet Programming - EJB Spring 2001 Declarative transaction management: An application's use of transactions can be declared in the XML deployment descriptor, rather than having a programmer write code to manage transactions Less code to write Less chance of programmer-generated errors Allows container to manage resources efficiently © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

21 Container Services – Security
Advanced Internet Programming - EJB Container Services – Security Spring 2001 Declarative security management: An application's use of authentication (login) and confidentiality (encryption) can be declared in the XML deployment descriptor, rather than having a programmer write code to manage security Less code to write Less chance of programmer-generated errors Allows system administrator to manage user access externally to the application code © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

22 Container Services – Instances
Advanced Internet Programming - EJB Container Services – Instances Spring 2001 Management of multiple instances: EJBs are written as if they are single threaded classes being accessed by only one client A singleton bean is intended to be shared by all clients, but concurrency is still managed by the container (unless overridden). The EJB container must ensure that each client’s requests are serviced in a timely manner © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

23 Container services - Instances
Advanced Internet Programming - EJB Container services - Instances Spring 2001 In order to accomplish the goal of timely responses to client requests, the server may perform several tasks: Instance passivation – the temporary swapping of an EJB out to storage. If a container needs resources, it may choose to temporarily passivate a bean Instance pooling – the creation of a pool of bean instances, so that beans can be shared between multiple clients, rather than a new bean being created for every client request (not possible in the case of stateful session beans) Database connection pooling – the creation of a pool of database connections, so that connections can be shared between multiple beans, rather than a new connection being created every time the database is accessed © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

24 Tasks containers perform (2)
Advanced Internet Programming - EJB Tasks containers perform (2) Spring 2001 Optimised method invocations –the short-circuiting of method calls between two beans in the same container in order to avoid the overhead of a full-blown remote method call (this is done by the container, not by the EJBs themselves - not strictly true as local interfaces in EJB 2.0 achieve the same) Instance passivation is the only optimisation required by the EJB specification, none of the others are explicitly required © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

25 32549 - Advanced Internet Programming - EJB
Enterprise Java Beans Spring 2001 Today we will briefly cover Introduction to EJB EJB Basics  EJB Architecture Session Beans EJB Clients EJB Development Process © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

26 32549 - Advanced Internet Programming - EJB
EJB Architecture Spring 2001 EJB container Interceptors filters Remote/Local Interface business methods eg: - deposit() - withdraw() Implementation Business logic client DB Container services Transaction mgmt security instance mgmt JNDI lookup © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

27 32549 - Advanced Internet Programming - EJB
EJB Architecture Spring 2001 The client side contains: the EJB interfaces needed to invoke business methods on an EJB internally generated proxy/stubs for maintaining handles to the server-side objects The server side contains: the instances of the actual EJB components The EJB container maps calls from clients to EJB instances (after the appropriate service management infrastructure code has been executed) RMI remote interface semantics are implied by the interfaces to EJBs © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

28 EJB Architecture pieces (1)
Advanced Internet Programming - EJB EJB Architecture pieces (1) Spring 2001 The primary application-specific elements of the EJB architecture shown in the diagram are: EJB Client: Optionally use JNDI to look up home interfaces, and use home and remote interfaces to utilise all EJB-based functionality. Dependency injection adds Home interface transparently EJB clients can also be external to the application server © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

29 EJB Architecture pieces (2)
Advanced Internet Programming - EJB EJB Architecture pieces (2) Spring 2001 EJB Remote interfaces (and Stubs): EJB remote interfaces provide the business-specific methods Underlying stubs marshal remote interface requests and unmarshal remote interface responses for the client EJB implementations: implementations are the application components implemented by developers to provide any application specific business logic. May also include lifecycle methods eg: Remove() © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

30 EJB Architecture pieces (3)
Advanced Internet Programming - EJB EJB Architecture pieces (3) Spring 2001 Interceptors You can do "Aspect Oriented Programming" Interceptor methods can override EJB business methods Much like Servlet Filters. Container EJB implementations (Skeletons and Delegates): The container manages the distributed communication skeletons used to marshal and unmarshal data sent to and from the client. May also store EJB implementation instances in a pool. May use delegates to perform service management operations © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

31 32549 - Advanced Internet Programming - EJB
Enterprise Java Beans Spring 2001 Today we will briefly cover Introduction to EJB EJB Basics EJB Architecture  Session Beans EJB Clients EJB Development Process Next lesson we will cover Entity Beans Message Driven Beans EJB issues © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

32 32549 - Advanced Internet Programming - EJB
Spring 2001 Part of Java EE 5 Significantly simplifies EJB development Use Java 5 annotations. Assumes common defaults for most infrastructure classes/methods Removes the need to develop separate Home/Remote interfaces You write your bean as a POJO (Plain Ol’ Java Object). You put one of the following annotations before the class declaration @Stateless Stateless session bean @Stateful Stateful session bean @Singleton Singleton session bean @MessageDriven Message bean © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

33 32549 - Advanced Internet Programming - EJB
Session Beans Spring 2001 Session beans are: EJBs that perform actions on the enterprise system. Model a connection (session) with a single client “Controllers” for the system that are exposed to the client for manipulation “verbs” of the particular problem domain that they are implemented to solve Well suited to encapsulate coarse-grained “front line” service entry points into a system © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

34 32549 - Advanced Internet Programming - EJB
Entities Spring 2001 Entities are: Objects that encapsulate some data contained by the system Implemented as JPA Entities “Nouns” of a particular problem domain that they are implemented to solve Subject to CRUD (create, read, update, delete) operations © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

35 32549 - Advanced Internet Programming - EJB
Session & Entity Beans Spring 2001 EJB container BankTeller - deposit() - withdraw() Account - acctnum = getBalance() - setBalance() Client (e.g. servlet) DBMS Account - acctnum = getBalance() - setBalance() BankManager - openAccount() Account - acctnum = getBalance() - setBalance() Session Beans ("actions") Entity Beans ("data") © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

36 32549 - Advanced Internet Programming - EJB
Session Beans Spring 2001 Now we focus on session beans Session beans exist for the duration of one interaction with the client stateless interaction (no state) stateful interaction (remembers previous state) Session beans provide the "public" business logic methods of your application Session beans may be modelled on UML use cases or equivalent © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

37 32549 - Advanced Internet Programming - EJB
Session Beans Spring 2001 Session beans can be grouped into 2 distinct classifications: Stateless session beans: No conversational state kept between client and bean Typically pooled – so 1 bean may actually service 100's of clients Easy to cluster, no affinity between client and physical server Stateful session beans: Keeps conversational state between client and bean 1:1 between client and bean. So if 100 clients, need 100 beans. Need to manage lifecycle – performance improved by container "paging out" (Passivating) and reactivating beans Note: Session beans persist only for the life of the connection with the client – if the server crashes, the session bean dies © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

38 32549 - Advanced Internet Programming - EJB
Session Beans Spring 2001 You need to create two files: Remote/Local interface (declares your business logic) implementation class (method implementations) Let's start with a stateless session bean the easiest kind © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

39 EJB 3.0 stateless session bean
Advanced Internet Programming - EJB EJB 3.0 stateless session bean Spring 2001 Just plain old Java Objects! Eg: BankTeller: @Remote public interface BankTeller { public string sayHello(String name); } @Stateless(name=“BankTeller”) public class BankTellerBean implements BankTeller { public string sayHello(String name) { return “Hello, “ + name; } } © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

40 Stateless Bean – code notes
Advanced Internet Programming - EJB Stateless Bean – code notes Spring 2001 @Remote indicates that the bean can be accessed via RMI-IIOP @Local indicates that this is just run within the same JVM @Stateless can have optional attributes to override defaults eg:name == name of bean beanInterface == name of bean class mappedName == JNDI name of bean © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

41 32549 - Advanced Internet Programming - EJB
Simple lifecycle Spring 2001 © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

42 32549 - Advanced Internet Programming - EJB
Lifecycle comments Spring 2001 When bean created, you can OPTIONALLY have callback method defined for initialisation @PostConstruct public void init() { //initialise classes? } @PreDestroy public void close() { // close databases? } © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

43 Stateful Session Beans
Advanced Internet Programming - EJB Stateful Session Beans Spring 2001 Stateful session bean is basically identical to a stateless session bean The difference? For stateful beans: you should define some state, i.e. instance variables inside your business methods, you can manipulate the state (read and change the variable values) 1 instance per client Need to deal with more complex lifecycle © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

44 Stateful beans – Lifecycle
Advanced Internet Programming - EJB Stateful beans – Lifecycle Spring 2001 © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

45 Stateful Beans – differences!
Advanced Internet Programming - EJB Stateful Beans – differences! Spring 2001 Keep state – eg: via instance variable Lifecycle tasks still optional. Extra callback methods @Init Similar but can have more than 1 init method. @PrePassivate Use this to store/save conversational information eg: to database @PostActivate Use this to retrieve conversational information eg: read from database @Remove Use this to delete the bean – eg: drop conversational state information © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

46 Stateful Beans – how not to use
Advanced Internet Programming - EJB Spring 2001 Stateful session beans do NOT represent the data in your application data is represented by JPA entities stateful session beans are transient – they come and go. Data is meant to be persistent if a server crashes unexpectedly, a stateful session bean will be destroyed with the crash (same for stateless session beans). Data is meant to be persistent. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

47 Session Beans – modelling
Advanced Internet Programming - EJB Session Beans – modelling Spring 2001 When do you use stateless beans vs. stateful beans? In most cases, it depends on how you choose to model your application Consider the bank teller examples on the next two slides – one is stateful, one is stateless © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

48 32549 - Advanced Internet Programming - EJB
Stateful Scenario Spring 2001 EJB container BankTeller - deposit() - withdraw() int account = 1234 create(1234) Client (e.g. servlet) deposit($100) © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

49 Teller Example 1 – stateful
Advanced Internet Programming - EJB Teller Example 1 – stateful Spring 2001 Scenario: When you walk up to a bank teller, you tell him/her your account number. You can then perform any number of deposits and withdrawals with that teller, without having to tell him/her the account number again each time – he/she remembers it When you create() the BankTeller EJB, you supply: the account number When you call deposit() and withdraw(), you pass only one parameter: the amount When you call deposit() or withdraw(), it uses ("remembers") the account number that you specified at creation time © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

50 32549 - Advanced Internet Programming - EJB
Stateless Scenario Spring 2001 EJB container BankTeller - deposit() - withdraw() create() Client (e.g. servlet) deposit(1234,$100) © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

51 Teller Example 2 – stateless
Advanced Internet Programming - EJB Teller Example 2 – stateless Spring 2001 Scenario: When you walk up to a bank teller, you don't give him/her any information. You can perform any number of deposits and withdrawals, but each time, you must specify your account number. He/she does NOT remember your account number from one transaction to the next When you create() the BankTeller EJB, you supply: nothing When you call deposit() and withdraw(), you pass two parameters every time: account number and amount © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

52 Stateless vs. stateful implications
Advanced Internet Programming - EJB Spring 2001 With the stateless teller, each time you perform a transaction, you can go to any teller The teller you go to doesn't "remember" anything about you, so you can go to a different one each time EJB containers create a pool of stateless session beans, and every time a client calls a method, that method is directed to any bean in the pool With the stateful teller, you must go back to the same teller each time ... ... because that teller knows your account number. If you go to a different teller, they won't know what your account number is EJB containers assign one stateful session bean to every client There is a one-to-one correspondence between clients and stateful session beans – no pooling! © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

53 32549 - Advanced Internet Programming - EJB
Enterprise Java Beans Spring 2001 Today we will briefly cover Introduction to EJB EJB Basics EJB Architecture Session Beans  EJB Clients EJB Development Process Next lesson we will cover Entity Beans Message Driven Beans EJB issues © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

54 32549 - Advanced Internet Programming - EJB
EJB Clients Spring 2001 In the EJB architecture, the client undertakes the following tasks: Finding the bean Calling the bean’s methods (call Remote interface methods) Getting rid of the bean A client can be local (in same JVM as bean) Eg: another EJB, Java class or web application component running in the same application server or remote Eg: a standalone Java application or a component running in another application server © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

55 32549 - Advanced Internet Programming - EJB
1. Finding the bean Spring 2001 2 techniques: Use Dependency Injection – this works only with Managed beans eg: Servlets, EJB. Container looks for the bean class Eg: servlets, ejb's, etc @EJB BankTeller teller; CAUTION: Only works for servlets version 2.5!! Does NOT work for JSP. See next page © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

56 32549 - Advanced Internet Programming - EJB
1. Finding the bean (2) Spring 2001 For non-managed clients eg: Java Application, Java Beans or JSP's Use JNDI - looks for the advertised name of the bean's interface. BankTeller teller = (BankTeller)new InitialContext().lookup("BankTeller"); JNDI name is vendor specific. Use the name & mappedName attribute on the EJB to define this Can also use XML deployment descriptor © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

57 2. Calling the bean's methods
Advanced Internet Programming - EJB 2. Calling the bean's methods Spring 2001 Now the client has a remote interface, it can invoke the methods the bean has made public i.e. the methods defined in the Remote interface teller.deposit(100); © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

58 32549 - Advanced Internet Programming - EJB
3. Getting rid of the bean Spring 2001 Stateless session beans are automatically removed by the container at its discretion no problem – they don't hold any state info Stateful session beans should be removed explicitly by the client by calling the method annotated Container won't remove these automatically because they contain valuable state information – your application needs to decide when it is finished with the state info © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

59 32549 - Advanced Internet Programming - EJB
Enterprise Java Beans Spring 2001 Today we will briefly cover Introduction to EJB EJB Basics EJB Architecture Session Beans EJB Clients  EJB Development Process © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

60 EJB Development Process
Advanced Internet Programming - EJB EJB Development Process Spring 2001 Checkpoint! Until now we have considered: what EJBs are, what the different kinds are what containers do and why they're a "good thing" what session beans look like and how they're used Now we change focus and look at the EJB development process same process for all kinds of EJBs © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

61 32549 - Advanced Internet Programming - EJB
EJB Development Steps Spring 2001 2 streams server side and client side development Server-side development of EJBs is simpler than RMI communications, state management, resource allocation and thread management infrastructure coding provided by the container Similar for EJB Clients © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

62 EJB Development Steps (1)
Advanced Internet Programming - EJB EJB Development Steps (1) Spring 2001 9 steps involved in server-side EJB development: 1. Create the remote interface - Defines business methods 2. Create the EJB implementation class - implements the bean 3. Compile the java code © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

63 EJB Development Steps (2)
Advanced Internet Programming - EJB EJB Development Steps (2) Spring 2001 4. Write the Optional EJB deployment descriptors - create an ejb-jar.xml file and maybe others 5. Package the EJB into a JAR file - EJBs get packaged in JAR files 6. Compile the EJB stubs - use an EJB compiler tool © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

64 EJB Development Steps (3)
Advanced Internet Programming - EJB EJB Development Steps (3) Spring 2001 OPTIONAL STEPS: 7. Configure the application deployment descriptor (optional) - deployment descriptor for the whole application, not the EJB 8. Package the EJB JAR into an EAR file (optional) - "applications" are packaged in EAR files 9. Deploy the EJB JAR or J2EE application EAR - deploy to the application server © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

65 Client Development Steps
Advanced Internet Programming - EJB Client Development Steps Spring 2001 3 steps for writing EJB clients: Establish the proper EJB client libraries – make available the correct versions of appropriate libraries for the client – JNDI, EJB, RMI/IIOP and possibly JMS and JDBC if required Implement client code – create your client application using the EJB client libraries and your EJB client interfaces Compile / package / deploy the client – as appropriate for the client type © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

66 1. Create Remote interface
Advanced Internet Programming - EJB 1. Create Remote interface Spring 2001 package bank; import javax.ejb.*; @Remote public interface BankTeller { public string sayHello(String name); } (Same as previous example) © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

67 2. Create EJB implementation
Advanced Internet Programming - EJB 2. Create EJB implementation Spring 2001 import javax.ejb.*; @stateless(name=“BankTeller”) public class BankTellerBean implements BankTeller { public string sayHello(String name) { return “Hello, “ + name; } } (Same as previous example) © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

68 32549 - Advanced Internet Programming - EJB
3. Compile Java code Spring 2001 javac –d . *.java © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

69 4. Write EJB deployment desc.
Advanced Internet Programming - EJB 4. Write EJB deployment desc. Spring 2001 OPTIONAL: Standard deployment descriptor (META-INF/ejb-jar.xml) EJB name Java class names for remote interface, home interface and bean implementation class Whether bean is session or entity For session beans, whether bean is stateful or stateless Container-specific deployment descriptor (META-INF/weblogic-ejb-jar.xml) For WebLogic stateless session beans, size of pool JNDI name (name by which the bean is advertised to clients) © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

70 32549 - Advanced Internet Programming - EJB
4a. ejb-jar.xml Spring 2001 <?xml version="1.0"?> <ejb-jar xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="3.0"> <enterprise-beans> <session> <ejb-name>BankTellerEJB</ejb-name> <home>bank.BankTellerHome</home> <remote>bank.BankTeller</remote> <ejb-class>bank.BankTellerBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <method-intf>Remote</method-intf> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

71 4b. weblogic-ejb-jar.xml
Advanced Internet Programming - EJB 4b. weblogic-ejb-jar.xml Spring 2001 <?xml version="1.0"?> <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic EJB//EN' ' <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>BankTellerEJB</ejb-name> <stateless-session-descriptor> <pool> <max-beans-in-free-pool>100</max-beans-in-free-pool> </pool> </stateless-session-descriptor> <jndi-name>ejb/BankTeller</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

72 5. Package EJB as a JAR file
Advanced Internet Programming - EJB 5. Package EJB as a JAR file Spring 2001 Same concept as for WAR files but EJBs are packaged in JAR files EJB deployment descriptors stored in a sub- directory called "META-INF" Directory structure in JAR must mirror Java package structure (as per normal Java rules) jar cf ../MyEJB.jar * © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

73 32549 - Advanced Internet Programming - EJB
6. Compile EJB stubs Spring 2001 Run appc (weblogic EJB compiler) on the EJB JAR file - this generates and compiles stubs and skeletons java weblogic.appc MyEJB.jar Different development tools may have different ways of compiling the EJB code and generating stubs/skeletons © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

74 32549 - Advanced Internet Programming - EJB
Deploy Spring 2001 You can now deploy if your client is not in same JAR file, then you need to have a copy of the bean interfaces in the classpath eg: WAR file can contain the BankTeller.class in the /WEB- INF/classes directory You can also put multiple bean JAR files and WAR files together to make a "Java EE Application" (EAR) archive © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

75 7. Configure app deployment desc.
Advanced Internet Programming - EJB Spring 2001 We'll come back to this next session (see next slide for a brief outline) Suffice to say that this is an optional step, and for now, we're going to leave it out! © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

76 8. Package app as an EAR file
Advanced Internet Programming - EJB 8. Package app as an EAR file Spring 2001 In J2EE terms, an "application" consists of: one or more WAR files containing servlets and JSPs one or more JAR files containing EJBs A collection of WARs and JARs that comprise a single application may be packaged in an EAR file EAR = Enterprise Application Archive We'll come back to this next session © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

77 32549 - Advanced Internet Programming - EJB
10. Deploy Spring 2001 Deploy your JAR file into the application server In WebLogic, either: copy the JAR file into your “autodeploy" subdirectory run the command line tool to deploy the file upload the file via the management console Exactly the same process as deploying WAR files If all goes well, in the management console you should see: a new EJB appear under the "Deployments > EJB" menu a new entry appear in the JNDI tree © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

78 32549 - Advanced Internet Programming - EJB
EJB Development Spring 2001 There are quite a few steps, but each is simple You will probably find it useful to create a short shell script / batch file to run the various commands (I use “ant” to do this) what is “ant”? - a build tool similar to makefiles on Unix The most common error is ... ... typos in the XML deployment descriptor Running appc will pick up many errors Use an XML editor to reduce syntax errors © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

79 32549 - Advanced Internet Programming - EJB
Summary – EJB Part 1 Spring 2001 Reflect on how little code was required only 2 Java files, an Interface and Implementation plus 2 Optional deployment descriptors Hard part about EJB is understanding: the J2EE architecture and how to model the real world how EJBs are invoked / loaded / etc. what the container does © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

80 32549 - Advanced Internet Programming - EJB
Questions? Spring 2001 ? © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

81 32549 - Advanced Internet Programming - EJB
Clustering Spring 2001 EJB is specifically designed in mind for clustering Scalability Availability Load Balancing Failover/Migration © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

82 Definition: Clustering
Advanced Internet Programming - EJB Advanced Internet Programming - EJB Definition: Clustering Spring 2001 Spring 2001 A cluster is a group of Oracle WebLogic Server instances that work in coordination. Clustering provides: High availability Load balancing Scalability domain machine machine server Admin server DNS Round Robin cluster server client Proxy Server Internet server client server client Load Balancer Definition: Clustering Clustering is configuring a group of Oracle WebLogic Servers to work together to provide client access to the services offered by the servers in the cluster. The services that the cluster provides can be a superset of the services that one server in the cluster provides. However, it is good practice to set all servers in a cluster to provide the same services. The cluster appears to a client as one instance, whether the client is a Web client or a Java application. By replicating the services provided by one instance, an enterprise system achieves a fail-safe and scalable environment. Scalability: Scalability is achieved by balancing the load of incoming requests across the servers in the cluster. For example, if there are four servers in a cluster and four requests are made, each server gets one request. The capacity of a cluster is not limited to one server or one machine. Servers can be added to the cluster dynamically to increase capacity. If more hardware is needed, a new server on a new machine can be added. If one server cannot fully use an existing machine, additional servers can be added to the machine. High availability: High availability is achieved through the replication of services, so that when one service fails, another service can resume where the first service left off. A cluster uses the redundancy of multiple servers to insulate clients from failures. The same service can be provided on multiple servers in the cluster. If one server fails, another can take over. The capability to execute failover from a failed server to a functioning server increases the availability of the application to clients. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved. © Copyright 2001 University of Technology Sydney. All Rights Reserved. 82

83 Basic Cluster Architecture
Advanced Internet Programming - EJB Advanced Internet Programming - EJB Basic Cluster Architecture Spring 2001 Spring 2001 A basic cluster architecture combines static HTTP, presentation logic, business logic, and objects into one cluster. domain Cluster Firewall server 1 Web container EJB container Load Balancer server 2 Web container EJB container Basic Cluster Architecture The basic recommended cluster architecture combines all Web application tiers and puts the related services (static HTTP, presentation logic, and objects) into one cluster. The basic architecture has the following advantages: Easy administration: Because one cluster hosts static HTTP pages, servlets, and EJBs, you can configure the entire Web application and deploy or undeploy objects using one Administration Console. You do not need to maintain a separate bank of Web servers (and configure Oracle WebLogic Server proxy plug-ins) to benefit from clustered servlets. Flexible load balancing: Using load-balancing hardware directly, in front of the Oracle WebLogic Server cluster, enables you to use advanced load-balancing policies for access to both HTML and servlet content. Robust security: Putting a firewall in front of your load-balancing hardware enables you to set up a demilitarized zone (DMZ) for your Web application using minimal firewall policies. Optimal performance: The combined-tier architecture offers the best performance for applications in which most or all the servlets or JSPs in the presentation tier typically access objects in the object tier, such as EJBs or JDBC objects. A DMZ is a logical collection of hardware and services that is made available to outside, untrusted sources. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved. © Copyright 2001 University of Technology Sydney. All Rights Reserved. 83

84 Multitier Cluster Architecture
Advanced Internet Programming - EJB Advanced Internet Programming - EJB Multitier Cluster Architecture Spring 2001 Spring 2001 The Web tier and the business logic with services can be separated into two clusters. domain Cluster A Cluster B Firewall server 1 server 3 Web container EJB container Load Balancer server 2 server 4 Web container EJB container Multitier Cluster Architecture The recommended multitier architecture uses two separate Oracle WebLogic Server clusters: one to serve static HTTP content and clustered servlets and one to serve clustered EJBs. The multitier cluster is recommended for Web applications that require: Load balancing for method calls to clustered EJBs More flexibility for balancing the load between servers that provide HTTP content and servers that provide clustered objects Higher availability (fewer single points of failure) Note: Consider the frequency of invocations from the presentation tier to the object tier when considering a multitier architecture. If presentation objects usually invoke the object tier, a combined-tier architecture may offer better performance than a multitier architecture. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved. © Copyright 2001 University of Technology Sydney. All Rights Reserved. 84

85 Basic Cluster Proxy Architecture
Advanced Internet Programming - EJB Spring 2001 This is similar to the basic cluster architecture, except that static content is hosted on nonclustered HTTP servers. domain Cluster Firewall server 1 HTTP Server Servlet/ JSP EJB container Load Balancer Plug-in server 2 HTTP Server Servlet/ JSP EJB container Plug-in Basic Cluster Proxy Architecture The two-tier proxy architecture contains two physical layers of hardware and software. Web Layer The proxy architecture utilizes a layer of hardware and software that is dedicated to the task of providing the application’s Web tier. This physical Web layer can consist of one or more identically configured machines that host one of the following application combinations: Oracle WebLogic Server with HttpClusterServlet Apache with the Oracle WebLogic Server Apache proxy plug-in Netscape Enterprise Server with the Oracle WebLogic Server NSAPI proxy plug-in Microsoft Internet Information Server with the Oracle WebLogic Server Microsoft-IIS proxy plug-in Regardless of which Web server software you select, remember that the physical tier of the Web servers should provide only static Web pages. Dynamic content— servlets and JSPs—are proxied via the proxy plug-in or HttpClusterServlet to an Oracle WebLogic Server cluster that hosts servlets and JSPs for the presentation tier. Servlet/Object Layer The recommended two-tier proxy architecture hosts the presentation and object tiers on a cluster of Oracle WebLogic Server instances. This cluster can be deployed either on a single machine or on multiple separate machines. The Servlet/Object layer differs from the combined-tier cluster in that it does not provide static HTTP content to application clients. © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

86 Multitier Cluster Proxy Architecture
Advanced Internet Programming - EJB Spring 2001 This is similar to the multitier cluster architecture, except that static content is hosted on nonclustered HTTP servers. domain Cluster A Cluster B Firewall server 1 server 3 HTTP Server Web container EJB container Load Balancer Plug-in server 2 server 4 HTTP Server Web container EJB container Plug-in Multitier Cluster Proxy Architecture You can also use a bank of Web servers as the front end to a pair of Oracle WebLogic Server clusters that host the presentation and object tiers. Using stand-alone Web servers and proxy plug-ins provides the following advantages: You can use existing hardware. If you already have a Web application architecture that provides static HTTP content to clients, you can easily integrate the existing Web servers with one or more Oracle WebLogic Server clusters to provide dynamic HTTP and clustered objects. You can use familiar firewall policies. Using a Web server proxy at the front end of your Web application enables you to use familiar firewall policies to define your DMZ. In general, you can continue placing the Web servers in your DMZ while disallowing direct connections to the remaining Oracle WebLogic Server clusters in the architecture. The diagram in the slide depicts this DMZ policy. There are some disadvantages, however: Additional administration Limited load balancing options © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.

87 32549 - Advanced Internet Programming - EJB
Spring 2001 © Copyright UTS Faculty of Information Technology 2002 – EJB © Copyright 2001 University of Technology Sydney. All Rights Reserved.


Download ppt "Advanced Java programming (Java EE)"

Similar presentations


Ads by Google