Component-Based Software Engineering
ECE493-Topic 4Winter 2006
Lecture 17– Java Enterprise(Part B)
Ladan TahvildariLadan TahvildariAssistant ProfessorAssistant Professor
Dept. of Elect. & Comp. Eng.Dept. of Elect. & Comp. Eng.University of WaterlooUniversity of Waterloo
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 22
J2EE Application Server
Java 2 Enterprise Edition standardizes interfaces for Application Server components
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 33
EJB Overview
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 44
EJB – Enterprise Java Beans
Enterprise Java Beans are components that are deployed into containers
The container provides services– Loading / Initialization
– Transactions
– Persistence
– Communication with EJB clients
– Enterprise Naming Context (JNDI name space)
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 55
Deployment Phase
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 66
Abstract Schema
Part of an entity bean's deployment descriptor
Defines the bean's persistent fields and relationship fields.
The term abstract distinguishes this schema from the physical schema of the underlying data store
Specify the name of an abstract schema in the deployment descriptor
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 77
Abstract Schema
Persistent Fields– Are stored in the underlying data store
– Constitute the state of the bean. At runtime, the EJB container automatically synchronizes this state with the database
– During deployment, the container • Maps the entity bean to a database table
• Maps the persistent fields to the table's columns
Relationship Fields– It’s like a foreign key in a database table.It identifies a related bean
– Like a persistent field, a relationship field is virtual and is defined in the enterprise bean class with access methods
– Unlike a persistent field, a relationship field does not represent the bean's state
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 88
Abstract Schema
Multiplicity in Container-Managed Relationships– One-to-one: Each entity bean instance is related to a single
instance of another entity bean
– One-to-many: An entity bean instance may be related to multiple instances of the other entity bean
– Many-to-one: Multiple instances of an entity bean may be related to a single instance of the other entity bean
– Many-to-many: The entity bean instances may be related to multiple instances of each other
Direction in Container-Managed Relationships– Bidirectional relationship: each entity bean has a relationship field
that refers to the other bean. Through the relationship field, an entity bean's code can access its related object
– Unidirectional relationship: only one entity bean has a relationship field that refers to the other
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 99
Example of Abstract Schema
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1010
Deployment Phase
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1111
Anatomy of an EJB
Remote Interface– Methods that can be accessed by the outside world.– Extends javax.ejb.EJBObject
Remote Home Interface– Life-cycle methods (create, findByPrimaryKey)– Extends javax.ejb.EJBHome which extends
java.rmi.Remote
Bean class– The class performing the actual business process– Implements an interface based on type of bean
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1212
Client / EJB RelationshipHow does a client application (Java class) utilize EJBs?
– Lookup • JNDI (Java Naming and Directory Interface)
• ENC (Environment Naming Context)
– Network protocol - RMI
– EJB container creates object with RemoteHome and Home interfaces –this object passes calls to the bean class
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1313
Anatomy of an EJB
EJB 2.0 New Interfaces– New Interfaces allow bean to bean method calls within
the same container
– Local Interface• Similar to the remote interface, but without RMI
• Extends javax.ejb.EJBLocalObject
– Local Home Interface• Similar to the remote home interface, but without RMI
• Extends javax.ejb.EJBLocalHome
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1414
Java versus .NET for the Enterprise
Activation/Passivate (on stateful beans)JIT Activation
Java Server Page (JSP) ASP.NET
Instance PoolingObject Pooling
Can be implemented through JMSQueued Components
Java Message Service (JMS)Loosely Coupled Events
Java Transaction Service (JTS)Transactions
EJB
Session Beans (Stateless, Stateful)
Entity Beans
Message-Driven Beans
COM+ Components
Java.NET
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1515
JTA / JTS – TransactionsThe Java Transaction API (JTA) and the Java Transaction Service (JTS) allow J2EE application servers to take the burden of transaction management from the component developer
Developers can define the transactional properties of Enterprise JavaBeans technology based components during design or deployment using declarative statements in the deployment descriptor
The application server takes over the transaction management responsibilities
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1616
Transactions
In EJB only addresses persistent data– Application code required to rollback changes in application
variables
Either Bean or Container Managed Transaction– Bean Managed
• Explicit use of the Java Transaction API by the Bean
– Container Managed• Completely declarative in deployment descriptor
• Container invokes business method in specified scope
– Entity Beans must use Container Managed transactions
– Session and Message Beans may use Bean Managed
Clients can also establish transactional scope
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1717
Transaction Attributes
NotSupported– Method never called within a transaction
– Container suspends client context if it exists
Required– Runs in client’s context if it exists otherwise Container create a
new context
– Used for a method that requires transaction, but can participate in a broader unit of work
• Example: depositing money in an account
• Can be atomic by itself or part of a greater transaction involving other operations
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1818
Transaction Attributes
Supports– Uses client’s context if it exists otherwise runs without a context– Needs to be used with caution
RequiresNew– Container always runs the method in a new transaction– Useful for work that commits regardless of results of outer unit of
work
Mandatory– Client must invoke the method from within a transaction– Container uses this context
Never– Client must not invoke the method from within a transaction– Container does not provide transaction context
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 1919
Java versus .NET for the Enterprise
Activation/Passivate (on stateful beans)JIT Activation
Java Server Page (JSP) ASP.NET
Instance PoolingObject Pooling
Can be implemented through JMSQueued Components
Java Message Service (JMS)Loosely Coupled Events
Java Transaction Service (JTS)Transactions
EJB
Session Beans (Stateless, Stateful)
Entity Beans
Message-Driven Beans
COM+ Components
Java.NET
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2020
JMS – Java Message ServiceEnterprise messaging provides a reliable, flexible service for the asynchronous exchange of critical business data and events throughout an enterprise
The JMS API adds to this a common API and provider framework that enables the development of portable, message based applications in the Java programming language
Why should we use JMS?– Loosely-Coupled systems
• Connectionless• Removes dependence on client and server platform / programming language /
version – Publish / Subscribe metaphor
• Send / receive information with many, unknown clients– Integration with other messaging systems
• IBM MQ-Series• Microsoft Message Queue
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2121
JMS – Java Message Service
JMS Queue
JMS Topic
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2222
Java versus .NET for the Enterprise
Activation/Passivate (on stateful beans)JIT Activation
Java Server Page (JSP) ASP.NET
Instance PoolingObject Pooling
Can be implemented through JMSQueued Components
Java Message Service (JMS)Loosely Coupled Events
Java Transaction Service (JTS)Transactions
EJB
Session Beans (Stateless, Stateful)
Entity Beans
Message-Driven Beans
COM+ Components
Java.NET
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2323
J2EE Application Server
Java 2 Enterprise Edition standardizes interfaces for Application Server components
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2424
Java ServletsServlets are the Java platform technology of choice for extending and enhancing web servers
Servlets provide a component-based, platform-independent method for building web-based applications
Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases
Servlets can also access a library of HTTP-specific calls and receive all the benefits of Java language, including– Portability– Performance– reusability
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2525
Anatomy of a Servletinit() – the init() function is called when the servlet is initialized by the server. This often happens on the first doGet() or doPost() call of the servlet
destroy() – this function is called when the servlet is being destroyed by the server, typically when the server process is being stopped
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2626
Anatomy of a ServletdoGet() – the doGet() function is called when the servlet is called via an HTTP GET
doPost() – the doPost() function is called when the servlet is called via an HTTP POST
– POSTs are a good way to get input from HTML forms
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2727
JSP – Java Server Pages
Java Server Pages technology uses XML-like tags and scripts written in the Java programming language to encapsulate the logic that generates the content for the page
Any and all formatting (HTML or XML) tags are passed directly back to the response page
By separating the page logic from its design and display and supporting a reusable component-based design, JSP technology makes it faster and easier than ever to build web-based applications
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2828
EJB ExampleThe OnLine Bank
– Services: what the client can do in the system such as see the foreign currency , listed shares or make operations on his hown account.
– Accounts: a database containing the accounts of all the clients of the bank with information about credit,debit,access etc.
– Security: is a subsystem that receives all the alarm caused from wrong access and performs action about the situation (calls police and stops operation of that client keeping information about him/her)
Virtual Bank
Client
Security Accounts
Services
March 2, 2006March 2, 2006 ECE493ECE493--T4T4 2929
EJB Example: The OnLine Bank
Easy to create an EJB structure:
– Client will have a web page at client side to insert values and connect the system
• This will be done using JSP (Java Servlet Pages )
– Services will be a Stateful Session Bean• it will be different for each client connecting the system mantaining data about
the client connected.
– Accounts will be formed by an Entity Bean for each account in the system with a code-account as primary key.
– Security will be a Message Driven Bean and will be called only from container if some operation are abnormal for result or the autentification for the same client fails too much times.