Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECOM 6330 Java Parallel and Distributed computing

Similar presentations


Presentation on theme: "ECOM 6330 Java Parallel and Distributed computing"— Presentation transcript:

1 ECOM 6330 Java Parallel and Distributed computing
11/27/2019 ECOM Java Parallel and Distributed computing Lecture 5 – J2EE & EJB Professor Hatem Hamad Lecture 5 11/27/2019 Lecture 7

2 Components Distributed objects are independent software components. Components are not bound to a particular program, computer language, or implementation. Components are packaged as the basic unit of work and distribution for an application made up of distributed objects. 11/27/2019 Lecture 5

3 Key Characteristic Possibly the most important distinguishing characteristic between an application and a component is that components can be used in unpredictable combinations, while applications tend to be focused on a particular task or environment. 11/27/2019 Lecture 5

4 Why Components? Interoperability between: Different Languages
Different Compilers Different Vendors Different Operating Systems Different Tools Different Networks Different Applications Different Address Spaces 11/27/2019 Lecture 5

5 Properties of Components
A marketable entity Not a complete application Usable in unpredictable combinations Well specified interface Toolability Event notification Configuration and property management Scripting Metadata and Inspection Interoperability Ease of Use 11/27/2019 Lecture 5

6 Component Model Lecture 5 11/27/2019

7 Distributed Application with JEE
Lecture 5 11/27/2019

8 Server Side Components
Components on the Server side need to be more concerned with Security Licensing Versioning Life Cycle Management Transaction Control and Locking Persistence Relationships Self-Testing Semantic Messaging and Self-installation 11/27/2019 Lecture 5

9 Distributed Multi-tiered Applications
The J2EE platform uses a multi-tiered distributed application model for enterprise applications Application logic is divided into “components” according to function, and the various application components that make up a J2EE Applications are installed on different machines depending on the tier in the multi-tiered J2EE environment to which the application component belongs 11/27/2019 Lecture 5

10 J2EE Architecture J2EE multi-tiered applications are generally considered to be three-tiered applications because they are distributed over three different locations client machines the J2EE server machine the database or legacy machines at the back end 11/27/2019 Lecture 5

11 J2EE Architecture Three-tiered applications that run in this way extend the standard two- tiered client and server model by placing a multithreaded application server between the client application and back-end storage 11/27/2019 Lecture 5

12 J2EE Containers The application server maintains control and provides services through an interface or framework known as a container There are five defined container types in the J2EE specification 11/27/2019 Lecture 5

13 Server J2EE Containers Three of these are server-side containers:
The server itself, which provides the J2EE runtime environment and the other two containers An EJB container to manage EJB components A Web container to manage servlets and JSP pages 11/27/2019 Lecture 5

14 Client J2EE Containers The other two container types are client- side:
An application container for stand-alone GUIs, console An applet container, meaning a browser, usually with the Java Plug-in 11/27/2019 Lecture 5

15 J2EE Components J2EE applications are made up of components
A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and that communicates with other components 11/27/2019 Lecture 5

16 Components Client components run on the client machine, which correlate to the client containers Web components -servlets and JSP pages EJB Components 11/27/2019 Lecture 5

17 Packaging Applications and Components
Under J2EE, applications and components reside in Java Archive (JAR) files These JARs are named with different extensions to denote their purpose, and the terminology is important 11/27/2019 Lecture 5

18 Various File types Enterprise Archive (EAR) files represent the application, and contain all other server-side component archives that comprise the application Client interface files and EJB components reside in JAR files Web components reside in Web Archive (WAR) files 11/27/2019 Lecture 5

19 Deployment Descriptors
Deployment descriptors are included in the JARs, along with component-related resources Deployment descriptors are XML documents that describe configuration and other deployment settings (remember that the J2EE application server controls many functional aspects of the services it provides) The statements in the deployment descriptor are declarative instructions to the J2EE container; for example, transactional settings are defined in the deployment descriptor and implemented by the J2EE container 11/27/2019 Lecture 5

20 Deployment Descriptors
Most J2EE Web Services vendors provide a GUI tool for generating deployment descriptors and performing deployment because creating manual entries is tedious and error prone The deployment descriptor for an EJB component must be named ejb-jar.xml, and it resides in the META-INF directory inside the EJB JAR file 11/27/2019 Lecture 5

21 EJB Components EJB components are server-side, modular, and reusable, comprising specific units of functionality They are similar to the Java classes we create every day, but are subject to special restrictions and must provide specific interfaces for container and client use and access We should consider using EJB components for applications that require scalability, transactional processing, or availability to multiple client types 11/27/2019 Lecture 5

22 EJB Components- Major Types
Session beans These may be either stateful or stateless and are primarily used to encapsulate business logic, carry out tasks on behalf of a client, and act as controllers or managers for other beans Entity beans Entity beans represent persistent objects or business concepts that exist beyond a specific application's lifetime; they are typically stored in a relational database 11/27/2019 Lecture 5

23 The home and component interface
A bean's home interface specifies methods that allow the client to create, remove, and find objects of the same type The home interface provides bean management and life cycle methods EJB functionality is obtained through the bean's component interface, which defines the business methods visible to, and callable by, the client The developer writes the component interface, and the container creates the implementation for client interaction 11/27/2019 Lecture 5

24 Overview Enterprise JavaBeans is a specification for creating server-side secure, scalable, transactional, multi-user secure enterprise-level applications. These server-side components, called enterprise beans, are distributed objects that are hosted in Enterprise Java Bean containers and provide remote services for clients distributed throughout the network. 11/27/2019 Lecture 5

25 Java Beans vs. EJB Can be either visible non- visible.
Local Invocation Synchronous Invocation Decidedly non-visible remote objects Remote and Local Invocation Synchronous and Asynchronous Invocation Object Pooling Transparent Persistence Supports Transactions Support Relationships between entity EJBs J2EE Security Features Lecture 5 11/27/2019

26 Advantages of EJB Simplifies the development of middleware components that are secure, transactional, scalable & portable. Simplifies the process to focus mainly on business logic rather than application development. Overall increase in developer productivity Reduces the time to market for mission critical applications 11/27/2019 Lecture 5

27 Purpose of EJBs SESSION Beans (verbs of the system):
Model task or workflow Facade for Entity beans Maintain conversational state with clients ENTITY Beans (nouns of the system): Object/Relational (O/R) mapping Transparent and implicit persistence with transaction support Message Driven Beans: Asynchronous communication with MOM Conduit for non-J2EE resources to access Session and Entity Beans via JCA Resource adapters. 11/27/2019 Lecture 5

28 EJB server aka Enterprise Java Server (EJS)
EJS are analogous to the CORBA ORB. Part of an application server that hosts EJB containers EJBs do not interact directly with the EJB server EJB specification outlines eight services that must be provided by an EJB server: Naming Transaction Security Persistence Concurrency Life cycle Messaging Timer 11/27/2019 Lecture 5

29 Three Tier Architecture Using EJBs
11/27/2019 Lecture 5

30 EJB Container Functions as a runtime environment for EJB components beans Containers are transparent to the client in that there is no client API to manipulate the container Container provides EJB instance life cycle management and EJB instance identification. Manages the connections to the enterprise information systems (EISs) 11/27/2019 Lecture 5

31 EJB Container(cont’d)
11/27/2019 Lecture 5

32 EJB Client Finds EJB container via JNDI. Invokes methods on EJB beans.
11/27/2019 Lecture 5

33 EJB components 11/27/2019 Lecture 5

34 EJB Interfaces - Local and Remote
Local Interface Used for invoking EJBs within the same JVM (process) @Local annotation marks an interface local Parameters passed by reference Remote Interface Used for invoking EJBs across JVMs (processes) @Remote annotation marks an interface remote Parameters passed by value (serialization/de- serialization) Note: An EJB can be implement both interfaces if needed. 11/27/2019 Lecture 5

35 Business Interface Defines business methods
Session beans and message-driven beans require a business interface, optional for entity beans. Business interface do not extend local or remote component interface unlike EJB2.x Business Interfaces are POJIs (Plain Old Java Interfaces) 11/27/2019 Lecture 5

36 Business Interface - examples
Shopping cart that maintains state public interface ShoppingStatefulCart { void startShopping(String customerId); void addProduct(String productId); float getTotal(); } Shopping cart that does not maintain state public interface ShoppingStatelessCart { String startShopping(String customerId); // return cartId void addProduct(String cartId, String productId); float getTotal(String cartId); 11/27/2019 Lecture 5

37 Stateless Session EJB (SLSB)
Does not maintain any conversational state with client Instances are pooled to service multiple clients @Stateless annotation marks a been stateless. Lifecycle event callbacks supported for stateless session beans (optional) @PostConstruct occurs before the first business method invocation on the bean @PreDestroy occurs at the time the bean instance is destroyed 11/27/2019 Lecture 5

38 Stateless Session EJB example (1/2)
The business interface: public interface HelloSessionEJB3Interface{ public String sayHello(); } 11/27/2019 Lecture 5

39 Stateless Session EJB example (2/2)
The stateless bean with local interface: import javax.ejb.*; import javax.annotation.*; @Local({HelloSessionEJB3Interface.class}) @Stateless public class HelloSessionEJB3 implements HelloSessionEJB3Interface{ public String sayHello(){ return "Hello from Stateless bean“; } @PreDestroy void restInPeace() { System.out.println(“I am about to die now“); 11/27/2019 Lecture 5

40 Stateful Session EJB (SFSB)
Maintains conversational state with client Each instance is bound to specific client session Support callbacks for the lifecycle events Lecture 5 11/27/2019

41 SFSB Lifecycle Events @PostConstruct same as SLSB, once for each session @PreDestroy same as SLSB, once for each session @PostActivate container picks a instance from pool and assigns to a specific client session @PrePassivate container returns the instance to pool after the session is terminated @Init designates the initialization method of a stateful session bean @Remove causes the container to remove the stateful session bean, first invoking the bean’s PreDestroy method if any 11/27/2019 Lecture 5

42 Stateful Session EJB – example (1/2)
Define remote business interface (remote can be marked in bean class also) : @Remote public interface ShoppingCart { public void addItem(String item); public void removeItem(String item); public Collection getItems(); } 11/27/2019 Lecture 5

43 Stateful Session EJB – example (2/2)
@Stateful public class CartBean implements ShoppingCart { private ArrayList items; @PostConstruct public void initArray() { items = new ArrayList(); } public void addItem(String item) { items.add(item); public void removeItem(String item) { items.remove(item); public Collection getItems() { return items; @Remove void logoff() {items=null;} 11/27/2019 Lecture 5

44 Entity EJB (1) It is permanent.  It is a standard Java objects come into existence when they are created in a program.  When the program terminates, the object is lost.  But an entity bean stays around until it is deleted.  In practice, entity beans need to be backed up by some kind of permanent storage, typically a database. A program can create an entity bean, then the program can be stopped and restarted.  The entity bean will continue to exist.  After being restarted, the program can again find the entity bean it was working with, and continue using the same entity bean. 11/27/2019 Lecture 5

45 Entity EJB (2) It is identified by a primary key.
Entity Beans must have a primary key.  The primary key is unique -- each entity bean is uniquely identified by its primary key.  For example, an "employee" entity bean may have Social Security numbers as primary keys.  You can only use entity beans when your objects have a unique identifier field, or when you can add such a field. Note: Session beans do not have a primary key. 11/27/2019 Lecture 5

46 Entity Bean Class @Entity annotation marks a class as Enity EJB
Persistent state of an entity bean is represented by non-public instance variables For single-valued persistent properties, these method signatures are: <Type> getProperty() void setProperty(<Type> t) Must be a non-final concrete class Must have public or protected no-argument constructor 11/27/2019 Lecture 5

47 Entity Bean Class (cont.)
No methods of the entity bean class may be final If entity bean must be passed by value (through a remote interface) it must implement Serializable interface. 11/27/2019 Lecture 5

48 Entity EJB CMP (Container Managed Persistence)
Container maintains persistence transparently using JDBC calls BMP (Bean Managed Persistence) Programmer provides persistence logic Used to connect to non-JDBC data sources like LDAP, mainframe etc. Useful for executing stored procedures that return result sets 11/27/2019 Lecture 5

49 Entity EJB – example (1) @Entity // mark as Entity Bean
public class Customer implements Serializable { private Long id; private String name; private Collection<Order> orders = new HashSet(); @Id(generate=SEQUENCE) // primary key public Long getId() { return id; } public void setId(Long id) { this.id = id; 11/27/2019 Lecture 5

50 Entity EJB – example (2) @OneToMany // relationship between Customer and Orders public Collection<Order> getOrders() { return orders; } public void setOrders(Collection<Order> orders) { this.orders = orders; 11/27/2019 Lecture 5

51 EntityManager EntityManager API is used to:
create and remove persistent entity instances to find entities by their primary key identity, and to query over entities EntityManager supports EJBQL and (non-portable) native SQL 11/27/2019 Lecture 5

52 Entity Bean Life Cycle Entity bean instance has four possible states:
New entity bean instance has no persistent identity, and is not yet associated with a persistence context. Managed entity bean instance is an instance with a persistent identity that is currently associated with a persistence context. Detached entity bean instance is an instance with a persistent identity that is not (or no longer) associated with a persistence context. Removed entity bean instance is an instance with a persistent identity, associated with a persistence context, scheduled for removal from the database. 11/27/2019 Lecture 5

53 Example of Use of EntityManager API
@Stateless public class OrderEntry { @Inject EntityManager em; public void enterOrder(int custID, Order newOrder) { Customer cust = (Customer)em.find("Customer", custID); cust.getOrders().add(newOrder); newOrder.setCustomer(cust); } 11/27/2019 Lecture 5

54 Message Driven EJB Invoked by asynchronously by messages
Cannot be invoked with local or remote interfaces @MessageDriven annotation with in class marks the Bean message driven Stateless Transaction aware 11/27/2019 Lecture 5

55 Message Driven EJB example
import javax.jms.Message; import javax.jms.MessageListener; import javax.ejb.MessageDriven; @MessageDriven public class MessageDrivenEJBBean implements MessageListener { public void onMessage(Message message) { if(message instanceof MyMessageType1) doSomething(); // business method 1 if(message instanceof MyMessageType2) doSomethingElse(); // business method 2 } 11/27/2019 Lecture 5

56 EJB Query Language (EJBQL)
EJBQL : RDBMS vendor independent query syntax Query API supports both static queries (i.e., named queries) and dynamic queries. Since EJB3.0, supports HAVING, GROUP BY, LEFT/RIGHT JOIN etc. 11/27/2019 Lecture 5

57 EJBQL - examples Define named query: Use named query: @NamedQuery(
name="findAllCustomersWithName", queryString="SELECT c FROM Customer c WHERE c.name LIKE :custName" ) Use named query: @Inject public EntityManager em; //.. List customers = em.createNamedQuery("findAllCustomersWithName") .setParameter("custName", "Smith") .getResultList(); 11/27/2019 Lecture 5

58 EJB Security Architecture
Client Security: The Enterprise JavaBean (EJB) server automatically performs the steps necessary to ensure that deployed enterprise bean applications are only available to authorized users. One of these steps is authenticating clients that request access to EJB homes, beans, and individual methods on the beans. 11/27/2019 Lecture 5

59 Understanding EJB Security
Two security measures that client must pass when you add security to EJB system – Authentication and Authorization. Authentication must be performed before any EJB method is called. Authorization occurs at the beginning of each EJB method call. 11/27/2019 Lecture 5

60 Authentication in EJB Two ways to perform authentication in EJB:
We can call authentication logic through Java Authentication and Authorization Service (JAAS), a separate J2EE API. JAAS is a portable interface that enables you to authenticate and authorize users in Java. It allows to login to the system without knowing about the underlying security system being used. 11/27/2019 Lecture 5

61 Authorization in EJB Two ways to perform authorization in EJB:
With programmatic authorization, we can hard code security checks into our bean code. With declarative authorization, the container performs all authorization checks for us. 11/27/2019 Lecture 5

62 Deploying EJBs EJB 3.0 annotations will replace EJB 2.0 deployment descriptors in almost all cases Values can be specified using annotations in the bean class itself Deployment descriptor may be used to override the values from annotations 11/27/2019 Lecture 5

63 Some EJB Servers Company Product IBM WebSphere
BEA Systems BEA WebLogic Sun Microsystems Sun Application Server Oracle Oracle Application Server JBoss JBoss 11/27/2019 Lecture 5

64 References (1) SUN EJB Specifications
IBM RedBooks IBM WebSphere Developer Technical Journal Oracle Technology Network 11/27/2019 Lecture 5

65 References (2) Java.net http://www.java.net/
JavaWorld index.shtml TheServerSide Richard Monson-Haefel, Enterprise JAVABEANS Tate, Clark, Lee, Lisnkey, BITTER EJB Feghhi, Jalal, Web developer's guide to JavaBeans 11/27/2019 Lecture 5


Download ppt "ECOM 6330 Java Parallel and Distributed computing"

Similar presentations


Ads by Google