ENTERPRISE APPLICATION
DEVELOPMENT USING JAVA
UNIT - III
J2EE, APPLICATION SERVER AND SOFTWARE
ARCHITECTURES
J2EE architecture - EJB - Session, Entity
and Message driven beans - Model View
Control (MVC) architecture – Case
study:Application server - Apache Tomcat
- Installation - services - Hosting Java Apps
with server. Types of software
architectures – SOA and Monolith
Architecture - Micro Services - Micro
Service Architecture - Application Layer -
Business Layer - Enterprise Layer -Infra
Layer - REST API - Advantages with Micro
Services
J2EE Architecture
J2EE is a four-tier architecture that consists of:
•Client Tier/ Presentation Tier
•Web Tier
•Enterprise JavaBeans Tier/ Business Tier
•Enterprise Information Systems Tier
Categories of Client Tier: There are 5 categories of
clients
Web Client,
Java Beans Client
EIS Client
Web-service peers and
Multi-tier client.
Web Tier
It provides the Internet functionality to the J2EE application. The
components on the web tier use HTTP to receive requests and
send responses to the client that resides on the tier.
Web-Tier Provides services to Client-tier using HTTP.
The responsibilities act as an intermediary between components
working on the web tier and other tiers and the client tier.
The intermediary activities are:
• Accepting info from other s/w sent using POST, GET, and PUT
using HTTP.
• Transmit data such as images and dynamic content.
Components of Web Tier: Two components that work on the
web tier are mentioned below:
• Servlets
• Java server pages (JSP)
ENTERPRISE JAVA BEANS TIER
IMPLEMENTATION
• It contains business logic for J2EE applications. In this
tier two or more EJB reside.
• EJB beans are contained in the EJB server- which is a
distributed object server that works on the EJB tier and
manages transactions, and security, and ensures
multi-threading and persistence when EJB are
accessed.
• EJB tier provides concurrency, scalability, life cycle
management, and fault-tolerant ( Back-Up) to the J2EE
applications.
• Some of the system services include Resource
Pooling, Distributed object protocols, thread
management, State Management, Process
Management, Object persistence, Security, and
Deployment Configuration.
Enterprise Information System Tier
• EIS tier links the J2EE application to resources and legacy
systems that are available on the corporate backbone
network.
• It is the tier where the J2EE applications directly or indirectly
interface with a variety of technologies including DBMS and
Mainframes. Components on the EIS communicate to
resources using CORBA or Java Connectors.
Access Control List (ACL)
• It controls communication between tiers.
• It acts as a bridge on different virtual networks because it adds
security to web Applications.
• ACL prevents hackers to access DBMS and similar resources.
SESSION, ENTITY AND MESSAGE DRIVEN
BEANS
Types of EJB
• EJB is an acronym for Enterprise Java Beans. It is
a server-side software element. It encapsulates
the business logic of an application.
• It is a specification for developing a distributed
business application on the Java platform.
• There are three types of EJBs: Session
Bean, Entity Bean, and Message-Driven Bean.
SESSION BEAN
• Session bean encapsulates business logic only, it can be invoked by
local, remote and webservice client.
• It can be used for calculations, database access etc.
• The life cycle of session bean is maintained by the application server
(EJB Container).
Types of Session Bean
There are 3 types of session bean.
1) Stateless Session Bean: It doesn't maintain state of a
client between multiple method calls.
2) Stateful Session Bean: It maintains state of a client
across multiple requests.
3) Singleton Session Bean: One instance per application, it
is shared between clients and supports concurrent
access.
STATELESS SESSION BEAN
• Stateless Session bean is a business object that
represents business logic only. It doesn't have state (data).
• In other words, conversational state between multiple
method calls is not maintained by the container in case of
stateless session bean.
• The stateless bean objects are pooled by the EJB container
to service the request on demand.
• It can be accessed by one client at a time. In case of
concurrent access, EJB container routes each request to
different instance.
There are 3 important annotations used in stateless session bean:
• Annotations in Java provide additional information to the
compiler and JVM. An annotation is a tag representing
metadata about classes, interfaces, variables, methods, or
fields. Annotations do not impact the execution of the code
that they annotate. Some of the characteristics of
annotations are: Begin with '@'
@Stateless
@PostConstruct
@PreDestroy
Life cycle of Stateless Session Bean
EXAMPLE OF STATELESS SESSION BEAN
To create EJB application, you need to create bean component and bean
client.
1) Create stateless bean component
To create the stateless bean component, you
need to create a remote interface and a bean
class.
File: AdderImplRemote.java
package com.javatpoint;
import javax.ejb.Remote;
@Remote
public interface AdderImplRemote {
int add(int a,int b);
File: AdderImpl.java
package com.javatpoint;
import javax.ejb.Stateless;
@Stateless(mappedName="st1")
public class AdderImpl implements AdderImplRemote {
public int add(int a,int b){
return a+b;
}
}
2) CREATE STATELESS BEAN CLIENT
File: AdderImpl.java
package com.javatpoint;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Test {
public static void main(String[] args)throws Exception {
Context context=new InitialContext();
AdderImplRemote remote=(AdderImplRemote)context.looku
p("st1");
System.out.println(remote.add(32,32));
}
}
Output
Output: 64
STATEFUL SESSION BEAN
Stateful Session bean is a business object that represents business
logic like stateless session bean. But, it maintains state (data).
Annotations used in Stateful Session Bean
There are 5 important annotations used in stateful session bean:
@Stateful
@PostConstruct
@PreDestroy
@PrePassivate
@PostActivate
Example of Stateful Session Bean
1) Create stateful bean component
2) Create stateful bean client
https://www.javatpoint.com/stateful-session-
bean
ENTITY BEAN IN EJB 3.X
• Entity bean represents the persistent data stored in the
database. It is a server-side component.
MESSAGE DRIVEN BEAN
• A message driven bean (MDB) is a bean that contains
business logic. But, it is invoked by passing the message.
So, it is like JMS Receiver.
• The Java Message Service (JMS) is a messaging API that
allows Java applications to send, receive, create, and
read messages.
• MDB asynchronously receives the message and
processes it.
• A message driven bean receives message from queue or
topic, so you must have the knowledge of JMS API.
Message Driven Bean Example
• To create the message driven bean, you need to declare
@MessageDriven annotation and implement MessageListener
interface.
package com.javatpoint;
import javax.ejb.MessageDriven;
import javax.jms.*;
@MessageDriven(mappedName="myTopic")
public class MyListener implements MessageListener{
@Override
public void onMessage(Message msg) {
TextMessage m=(TextMessage)msg;
try{
System.out.println("message received: "+m.getText());
}catch(Exception e){System.out.println(e);}
}
}
Export the ejb project and deploy the application.
In tomcat server, click on applications -> deploy ->
select mdb jar file by Choose File -> OK.
MVC FRAMEWORK
• The Model-View-Controller (MVC) framework is an architectural/design
pattern that separates an application into three main logical
components Model, View, and Controller
• MVC is one of the most frequently used industry-standard web
development frameworks to create scalable and extensible projects. It is
also used for designing mobile apps.
Features of MVC
• It provides a clear separation of business logic, UI logic, and input logic.
• It offers full control over your HTML and URLs which makes it easy to
design web application architecture.
• It is a powerful URL-mapping component using which we can build
applications that have comprehensible and searchable URLs.
• It supports Test Driven Development (TDD). Test-driven
development (TDD) is a testing approach in which automated tests are
written and executed before the actual coding.
Components of MVC
The MVC framework includes the following 3 components:
Controller
Model
View
Controller:
• The controller is the component that enables
the interconnection between the views and the
model so it acts as an intermediary.
Responsibilities:
• Receiving user input and interpreting it.
• Updating the Model based on user actions.
• Selecting and displaying the appropriate View.
View:
• The View component is used for all the UI logic of the
application. It generates a user interface for the user.
Responsibilities:
• Rendering data to the user in a specific format.
• Displaying the user interface elements.
• Updating the display when the Model changes
Model:
• The Model component corresponds to all the data-related logic
that the user works with.
Responsibilities:
• Managing data: CRUD (Create, Read, Update, Delete) operations.
• Enforcing business rules.
• Notifying the View and Controller of state changes.
Advantages of MVC
• Codes are easy to maintain and they can be
extended easily.
• The MVC model component can be tested
separately.
• The components of MVC can be developed
simultaneously.
• It reduces complexity by dividing an application into
three units. Model, view, and controller.
• It supports Test Driven Development (TDD).
Popular MVC Frameworks
• Some of the most popular and extensively used MVC
frameworks are listed below.
• Ruby on Rails
• Django
• CherryPy
• Spring MVC
• Catalyst
• Rails
• Zend Framework
• Fuel PHP
• Laravel
• Symphony
Case study:
Application server: Apache Tomcat
- Installation - services - Hosting
Java Apps with server
SERVICE ORIENTED ARCHITECTURE
(SOA)
• A Service-Oriented Architecture or SOA is a design pattern which
is designed to build distributed systems that deliver services to
other applications through the protocol. It is only a concept and
not limited to any programming language or platform.
• A service is a well-defined, self-contained function that
represents a unit of functionality. A service can exchange
information from another service.
Service Connections
COMPONENTS OF SERVICE-ORIENTED
ARCHITECTURE
• The service-oriented architecture stack can be categorized into
two parts - functional aspects and quality of service aspects.
The functional aspect contains:
• Transport - It transports the service requests from the
service consumer to the service provider and service
responses from the service provider to the service
consumer.
• Service Communication Protocol - It allows the service
provider and the service consumer to communicate
with each other.
• Service Description - It describes the service and data
required to invoke it.
• Service - It is an actual service.
• Business Process - It represents the group of services
called in a particular sequence associated with the
particular rules to meet the business requirements.
• Service Registry - It contains the description of data
which is used by service providers to publish their
services.
THE QUALITY OF SERVICE ASPECTS
CONTAINS:
• Policy - It represents the set of protocols
according to which a service provider make
and provide the services to consumers.
• Security - It represents the set of protocols
required for identification and authorization.
• Transaction - It provides the surety of
consistent result. This means, if we use the
group of services to complete a business
function, either all must complete or none of
the complete.
• Management - It defines the set of attributes
used to manage the services.
Advantages of SOA:
• Easy to integrate - In a service-oriented architecture, the
integration is a service specification that provides
implementation transparency.
• Manage Complexity - Due to service specification, the
complexities get isolated, and integration becomes more
manageable.
• Platform Independence - The services are
platform-independent as they can communicate with other
applications through a common language.
• Loose coupling - It facilitates to implement services without
impacting other applications or services.
• Parallel Development - As SOA follows layer-based
architecture, it provides parallel development.
• Available - The SOA services are easily available to any
requester.
• Reliable - As services are small in size, it is easier to test and
MONOLITHIC ARCHITECTURE
• A monolithic architecture is a traditional approach to designing software
where an entire application is built as a single, indivisible unit.
• In this architecture, all the different components of the application, such
as the user interface, business logic, and data access layer, are tightly
integrated and deployed together.
Advantages of using a Monolithic Architecture
• Simplicity
• Development Speed
• Deployment
• Debugging
Disadvantages of using a Monolithic Architecture
• Complexity
• Scalability
• Technology Stack
Deployment
• Fault Tolerance
MICROSERVICES ARCHITECTURE
• In a microservices architecture, an
application is built as a collection of small,
independent services, each representing a
specific business capability.
• These services are loosely coupled and
communicate with each other over a
network, often using lightweight protocols
like HTTP or messaging queues.
• Each service is responsible for a single
functionality or feature of the application and can
be developed, deployed, and scaled independently.
• The Microservice architecture has a significant
impact on the relationship between the application
and the database.
• Instead of sharing a single database with other
microservices, each microservice has its own
database. It often results in duplication of some
data, but having a database per microservice is
essential if you want to benefit from this
architecture, as it ensures loose coupling.
Advantages of using a Microservices Architecture
• Scalability
• Flexibility
• Resilience
• Agility
• Easier Maintenance
• Technology Diversity
Disadvantages of using a Microservices Architecture
• Complexity
• Increased Overhead
• Deployment Complexity
• Monitoring and Debugging
• Cost
• Testing
Differences between Monolithic and Microservices Architecture
Aspect Monolithic Architecture Microservice Architecture
Architectur
Single-tier architecture Multi-tier architecture
e
Large, all components Small, loosely coupled
Size
tightly coupled components
Deployme Individual services can be
Deployed as a single unit
nt deployed independently
REST API
• REST technology is generally preferred to the more robust Simple
Object Access Protocol (SOAP) technology because REST uses less
bandwidth, simple and flexible making it more suitable for internet
usage.
• It’s used to fetch or give some information from a web service. All
communication done via REST API uses only HTTP request.
REST APIs
• It stands for Representational State Transfer (REST) is an
architectural style that defines a set of constraints and protocols to
be used for creating web services.
• REST API is a method of accessing web services in a very simple and
flexible manner without having any processing. A REST API
endpoint is a URL that utilizes HTTP verbs to execute CRUD (Create
Read Update Delete) operations over the resources.
• These HTTP verbs are GET, POST, PATCH, PUT and DELETE. It
focuses on providing resources from the server to the clients.
Advantages of REST APIs
• They are very scalable as the client and server are decoupled
easing to scale in the future.
• Simple, standardized, and easy to use.
• Uses already existing HTTP features.
• They have high performance because of their cache
capabilities.
• Allows Standard-based protection with the use of OAuth
protocols to verify REST requests.
• Brings flexibility by serializing data in XML or JSON format.
Disadvantages of REST APIs
• REST API’s payload is quite big hence the entire files get back
while you needed one field.
• It loses the ability to maintain state in REST.
Advantages of microservices
• Accelerate scalability
• Improve fault isolation
• Enhance team productivity
• Quicker deployment time
• Increase cost-efficiency