You are on page 1of 47

INTEGRATIVE PROGRAMMING AND

TECHNOLOGIES
(ITec4121)

CHAPTER TWO:
FUNDAMENTALS OF CLIENT-SERVER
ARCHITECTURE
Distributed system concepts

 Virtually all large computer-based systems are now distributed


systems.
 Information processing is distributed over several computers
rather than confined(limited) to a single machine.
 Distributed software engineering is therefore very important for
enterprise computing systems.
Definition:
 Distributed system is a collection of independent computers that
appears to its users as a single coherent(logical) system.
 Distributed system is parts of an application run on multiple
computers simultaneously.

2
Distributed system concepts…

System types:
 Personal systems that are not distributed and that are designed to
run on a personal computer or workstation.
 Embedded systems that run on a single processor or on an
integrated group of processors.
 Distributed systems where the system software runs on a loosely
integrated group of cooperating processors linked by a network.
 Examples of distributed systems:
 Local Area Network and Intranet
 Database Management System
 Automatic Teller Machine Network
 Internet/World-Wide Web
 Mobile Computing
3
Distributed system concepts…

Two aspects of Distributed Systems:


 Independent computers
 Single system = middleware.

4
Distributed system concepts…

Distributed system characteristics:


 Resource sharing:- sharing of hardware and software resources.
 Openness:- use of equipment and software from different vendors.
 Concurrency:- concurrent processing to enhance performance.
 Scalability:- increased throughput by adding new resources.
 Fault tolerance: -the ability to continue in operation after a fault has
occurred.
Distributed system disadvantages:
 Complexity:- typically, distributed systems are more complex than
centralized systems.
 Security:- more susceptible(subjected) to external attack.
 Manageability:- more effort required for system management.
 Unpredictability: -unpredictable responses depending on the system
organization and network load.
5
Distributed Systems Architectures

 Basic distributed systems architectures are:


 Multiprocessor Architectures
 Client-server Architectures
 Distributed Object Architectures
 Multiprocessor Architectures
– Simplest distributed system model.
– System composed of multiple processes which may execute
on different processors.
– Architectural model of many large real-time systems.
– Distribution of process to processor may be pre-ordered or
may be under the control of a dispatcher(sender).

6
Distributed Systems Architectures…

 Client-server Architectures
– The application is modeled as a set of services that are
provided by servers and a set of clients that use these services.
– Clients know of servers but servers need not know of clients.
– Clients and servers are logical(consistent) processes
– The mapping of processors to processes is not necessarily 1:1.
– Clients and servers can be distributed across different
machines
– Servers provide services related to a shared resources

7
Distributed Systems Architectures…

 Clients follow request/reply model with respect to using services


as shown in figure below.

8
Distributed Systems Architectures…

 Distributed Object Architectures


– There is no distinction in a distributed object architectures
between clients and servers.
– Each distributable entity is an object that provides services to
other objects and receives services from other objects.
– Object communication is through a middleware system called
an object request broker(agent).
– However, distributed object architectures are more complex
to design than client-server systems.

9
Distributed Systems Architectures…

o1 o2 o3 o4

S (o1) S (o2) S (o3) S (o4)

Object request broker

o5 o6

S (o5) S (o6)

10
Distributed Systems Architectures…

 Advantages of distributed object architectures:


– It allows the system designer to delay decisions on where and
how services should be provided.
– It is a very open system architecture that allows new
resources to be added to it as required.
– The system is flexible and scaleable.
– It is possible to reconfigure the system dynamically with
objects migrating across the network as required.

11
Distributed Systems Architectures…

 Distributed Object Infrastructures:


– Java Remote Method Invocation (RMI)
– Common Object Request Broker Architecture (CORBA)
– Microsoft's Distributed Component Object Model (DCOM)

12
Message and queuing services

 What is JMS(Java Message Service)?


 A specification that describes a common way for Java programs
to create, send, receive and read distributed enterprise messages
 JMS is java message-oriented middleware API for sending
messages between two or more clients.
 Messaging is a method of communication between software
components or applications
 Messages go into a queue, then they get pulled out of the queue
 JMS API enables communication that is :
 Loosely coupled communication to communicate
 Asynchronous messaging
 Reliable delivery
13
A JMS Application(Architecture)

 JMS provider:- is a messaging product that implements the JMS


interfaces and provides administrative and control features
 Administered objects:- is a pre-configured JMS objects created
by an administrator for the use of clients.
 JMS clients:- the programs or components that produce and
consume messages.
 Messages:- the objects that communicate information between
JMS clients.

14
JMS Administration

 Note: JNDI(Java Naming & Directory Interface) is used to access


objects, devices & files of naming and directory services.

15
JMS Models (Message exchange Models)

 Publish-Subscribe Messaging
 Point- To-Point Messaging
 Request-Reply Messaging

16
Publish-Subscribe Messaging

 Uses a “topic” to send and receive messages


 Each message has multiple consumers that is broadcast message
to all Subscribers.
 Each message has multiple customers

17
Point- To-Point Messaging

 Built around the concept of message queues


 Each message has only one consumer
 Only one consumer of queue message
 No timing dependencies between Sender and Receiver

18
Request-Reply Messaging

 When an application sends a message and expects to receive a


message in return.
 It could be:
 Synchronous
 Asynchronous
 JMS does not explicitly support Request-Reply Messaging,
though it allows it in the context of the other methods.

19
JMS API Programming Model

20
The Messages

 Message Components:
 Header
 Properties
 Body

21
Message Header

 Used for Message Identification and Routing


 Destination Field includes:
 Subject or Topic (for Publish/Subscribe)
 queue(for point-to-point)
 Also other data
 delivery mode, message ID, Timestamp, Priority, ReplyTo

22
Message Properties

 Application-specific properties
 Messaging system provider-specific properties
 Optional fields
 Properties are Name/Value pairs
 Values can be int, string, byte etc

23
Message Body

 Holds content of message


 Five types of messages are supported
 Each type defined by a message interface include:
 StreamMessage
 MapMessage
 BytesMessage
 TextMessage
 ObjectMessage

24
Message Body Interfaces(Message types)

Message Type Contains Some Methods

TextMessage String getText,setText

MapMessage set of name/value pairs setString,setDouble,setLong,


getDouble,getString

BytesMessage stream of uninterpreted bytes writeBytes, readBytes

StreamMessage stream of primitive values writeString, writeDouble,


writeLong, readString

ObjectMessage serialize object setObject, getObject

25
Example: Creating a Text Message

 To create a simple TextMessage:


TextMessage message =session.createTextMessage( );
message.setText(“greetings”);

26
Building a JMS Client

Steps:
1. Create a Connection to the provider
2. Create Sessions to send/receive messages
3. Create MessageProducers
4. Create MessageConsumers
Step 1: Creating a Connection
 Connection provides access to messaging system
 Performs resource allocation and management
 ConnectionFactory used to create connection and it is located
through JNDI.

27
Building a JMS Client…

 Get the Connection Factory:


Context messaging = new InitialContext ( ) ;
TopicConnectionFactory factory = (TopicConnectionFactory);
messaging . lookup ( “TopicConnectionFactory” ) ;
 JNDI is used to get a ConnectionFactory
 2 Types ConnectionFactory
- QueueConnectionFactory for P-to-P
- TopicConnectionFactory for Pub/Sub
 Create the Connection:
TopicConnection topicConnection = factory . createTopicConnection ( ) ;

28
Building a JMS Client…

Step 2: Create Sessions


TopicSession session = topicConnection . createTopicSession
(false, CLIENT_ACKNOWLEDGE);
 First parameter controls transactions
 Second parameter specifies message acknowledgement
 Locate a Topic:
Topic weatherTopic = messaging . Lookup ( “WeatherData” ) ;
 JNDI is used to locate the topic using Queue or Topic
 Start the Connection:
topicConnection.start ( ) ;
 During initialization, message flow is inhibited; Connection must
now be started before messages will be transmitted

29
Building a JMS Client…

Step 3: Create Message Producer


TopicPublisher publisher =
session . createPublisher (weatherTopic) ;
 Publisher will publish messages to weathetData topic
 Publish a Message:
TextMessage message = session . createMessage ( ) ;
message . setText ( “text : 35 degrees” ) ;
publisher . Publish (message) ;
 Specifying Quality of Service:
Publish (Message message, int deliveryMode, int priority, long timeToLive) ;
 Delivery Modes
- NON-PERSISTENT
- PERSISTENT
 Priority
 Time-to-Live 30
Building a JMS Client…

Step 4: Message Consumer


 Subscribing to topics
 Durable Subscriptions
 Non-durable Subsriptions
 Receiving Messages
 Asynchronous
 Synchronous
 Asynchronous Message Receipt:
setMessageListener(MessageListener listener)
 Listener object must implement onMessage ( ) of MessageListener interface
 Synchronous Message Receipt:
Message receive ( ) ;
Message receive (long timeout) ;
Message receiveNoWait ( ) ;

31
Building a JMS Client…

 Point-to-Point Programming:
 Queue Browsing:
session.createBrowser (Queue queue);
session.createBrowser (QueueSession session, Queue queue,
String messageSelector) ;

32
Object broker design pattern

 A pattern is a recurring solution to a standard problem


 Design patterns represent the best practices used by experienced
object-oriented software developers.
 Design patterns are solutions to general problems that software
developers faced during software development.
 Usage of design pattern:
 Common platform for developers
 Best Practices

33
Types of Design Patterns

S.N. Pattern & Description

1 Creational Patterns
These design patterns provide a way to create objects while hiding the creation logic,
rather than instantiating objects directly using new opreator. This gives program more
flexibility in deciding which objects need to be created for a given use case.

2 Structural Patterns
These design patterns concern class and object composition. Concept of inheritance is
used to compose interfaces and define ways to compose objects to obtain new
functionalities.

3 Behavioral Patterns
These design patterns are specifically concerned with communication between objects.

4 J2EE Patterns
These design patterns are specifically concerned with the presentation tier. These
patterns are identified by Sun Java Center.

34
Proxy Design Pattern

 A class represents functionality of another class.


 It comes under structural pattern.
 Create object having original object to interface its functionality to outer
world.
Implementation:

35
Proxy Design Pattern…

 Step 1: Create an interface:


Image.java
public interface Image {
void display();
}
 Step 2: Create concrete classes implementing the same interface.
RealImage.java
public class RealImage implements Image {
  private String fileName;
  public RealImage(String fileName){
this.fileName = fileName;
loadFromDisk(fileName);
}
 
36
Proxy Design Pattern…

@Override
public void display() {
System.out.println("Displaying " + fileName);
}
  private void loadFromDisk(String fileName){
System.out.println("Loading " + fileName);
}
}
//ProxyImage.java
public class ProxyImage implements Image{
  private RealImage realImage;
private String fileName;
  public ProxyImage(String fileName){
this.fileName = fileName;
}
37
Proxy Design Pattern…

@Override
public void display() {
if(realImage == null){
realImage = new RealImage(fileName);
}
realImage.display();
}
}

38
Proxy Design Pattern…

 Step 3: Use the ProxyImage to get object of RealImage class


when required.
ProxyPatternDemo.java
public class ProxyPatternDemo {
public static void main(String[] args) {
Image image = new ProxyImage("test_10mb.jpg");
  //image will be loaded from disk
image.display();
System.out.println("");
//image will not be loaded from disk
image.display();
}
}

39
Proxy Design Pattern…

 Step 4: Verify the output.


Loading test_10mb.jpg
Displaying test_10mb.jpg
Displaying test_10mb.jpg

40
Facade Design Pattern…

 Hides the complexities of the system and provides an interface to the client
using which the client can access the system.
 Comes under structural pattern.
 Involves a single class which provides simplified methods required by client
and delegates calls to methods of existing system classes.
Implementation:

41
Facade Design Pattern…

 Step 1:Create an interface.


//Shape.java
public interface Shape {
void draw();
}
 Step 2: Create concrete classes implementing the same interface.
//Rectangle.java
public class Rectangle implements Shape {
  @Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}

42
Facade Design Pattern…

//Square.java
public class Square implements Shape {
  @Override
public void draw() {
System.out.println("Square::draw()");
}
}
Circle.java
public class Circle implements Shape {
  @Override
public void draw() {
System.out.println("Circle::draw()");
}
}
43
Facade Design Pattern…

 Step 3: Create a facade class.


//ShapeMaker.java
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
  public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}

44
Facade Design Pattern…

public void drawCircle(){


circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}

45
Facade Design Pattern…

 Step 4: Use the facade to draw various types of shapes.


//FacadePatternDemo.java
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
  shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
 Step 5: Verify the output.
Circle::draw()
Rectangle::draw()
Square::draw()
46

You might also like