Mediumjee-mixed12 min read

Session Facade, Message Facade, Composite View

Session Facade Pattern

Definition

The Session Facade is a JEE Business Tier Pattern. It provides a coarse-grained interface to a set of fine-grained external services. Instead of the client making multiple network calls to multiple external services, the client makes ONE call to the Session Facade, which internally coordinates all the fine-grained calls on the server side.

One line: One dummy external comp that sits in front of multiple real external comps, reducing network round trips from the client.

Problem: Multiple Network Round Trips

Problem: BusinessDelegate calls 3 external services directly Client App Server Side (BSE/ICC) round trips BusinessDelegate ----[1]----> GetStockIdComp (External Service 1) <---[2]---- ----[3]----> GetStockValueComp (External Service 2) <---[4]---- ----[5]----> GetStockFutureValueComp (External Service 3) <---[6]---- Total: 6 network round trips (3 requests + 3 responses) Each round trip adds latency. Performance bottleneck.

Here BusinessDelegate comp of Client App is having multiple round trips with multiple external comps which leads to performance issues (problem).

Solution: Session Facade Aggregates Calls

Solution: SessionFacade aggregates 3 services into 1 Client App Server Side 1 round trip BusinessDelegate ----[1]----> SessionFacadeComp <---[2]---- | |--> GetStockIdComp (local call, no network) |--> GetStockValueComp (local call) |--> GetStockFutureValueComp (local call) | All four comps are placed in same environment Total: 2 network trips (1 request + 1 response) instead of 6. Session Facade coordinates all internal calls on the server side. BusinessDelegate is getting all the 3 external comp services by just having one network round trip with SessionFacade comp (dummy comp).

Implementation

// Session Facade interface
public interface IStockFacade {
    StockDetailsDTO getCompleteStockInfo(String stockName);
}

// Session Facade implementation: aggregates 3 services
public class StockFacadeImpl implements IStockFacade {
    private final IGetStockId stockIdService;
    private final IGetStockValue stockValueService;
    private final IGetStockFutureValue futureService;

    public StockFacadeImpl() {
        // All services are local to the Facade (same server)
        this.stockIdService = new GetStockIdImpl();
        this.stockValueService = new GetStockValueImpl();
        this.futureService = new GetStockFutureValueImpl();
    }

    @Override
    public StockDetailsDTO getCompleteStockInfo(String stockName) {
        // 3 internal calls, 0 extra network round trips
        int id = stockIdService.getStockId(stockName);
        double value = stockValueService.getStockValue(id);
        double future = futureService.getFutureValue(id);
        return new StockDetailsDTO(stockName, id, value, future);
    }
}

// Client side: ONE call instead of THREE
StockDetailsDTO result = facade.getCompleteStockInfo("Reliance");
Key Points
  • All four comps (SessionFacade + 3 external services) should be developed as Distributed comps either as WebService comp or as EJB comp. For simplification, we are developing ordinary Java classes implementing Java interfaces.
  • The Session Facade and all external comps it wraps must be placed in the same environment/server so internal calls are local (no network overhead).
  • The Session Facade provides a coarse-grained interface (one method that does everything) instead of multiple fine-grained calls.

Message Facade Pattern

Definition

The Message Facade is a JEE Business Tier Pattern. It is similar to Session Facade but uses asynchronous communication instead of synchronous. The client sends a message to the Message Facade and does not wait for a response. The Facade processes the request in the background using messaging systems like JMS (Java Message Service).

One line: An asynchronous version of Session Facade that uses message queues for non-blocking communication.

Synchronous vs Asynchronous Communication

AspectSynchronous (Session Facade)Asynchronous (Message Facade)
Client behaviorClient sends request and waits (blocked) until response is receivedClient sends message and continues immediately (non-blocking)
ResponseImmediate response in same callResponse delivered later via callback, polling, or notification
TechnologyRMI, EJB Session Beans, REST (default)JMS, Kafka, RabbitMQ, SQS
Use whenClient needs the result immediatelyClient does not need immediate result
ExamplesStock price lookup, balance checkEmail sending, report generation, order processing
Synchronous Communication (Session Facade): Client App External Service | | |---- request (HTTP) --------->| | (client WAITS) |---- process | |---- query DB |<--- response (HTTP) ---------| | (client RESUMES) | If the request takes 10 seconds, client is BLOCKED for 10 seconds. Asynchronous Communication (Message Facade): Client App Message Queue External Service | | | |---- send message ----------->| | | (client CONTINUES |--- deliver message -->| | immediately!) | |---- process | | |---- query DB |<--- notification/callback ---|<---- send response ---| | (later, when ready) Client is free to do other work while the request is being processed.

When to use Message Facade

  • If the client does not need the result immediately (e.g., sending email, generating PDF report)
  • If the processing takes a long time (e.g., data migration, batch processing)
  • If the system needs to handle spikes in traffic (message queue acts as buffer)
  • If the external service may be temporarily unavailable (messages are persisted in queue)
// Message Facade: sends message to JMS queue
public class OrderMessageFacade {

    public void submitOrder(OrderDTO order) {
        // Create JMS message
        // Send to "orders" queue
        // Client is NOT blocked - returns immediately
        jmsTemplate.convertAndSend("orders-queue", order);
        System.out.println("Order submitted to queue. Processing async.");
    }
}

// Message Listener (processes asynchronously)
@JmsListener(destination = "orders-queue")
public void processOrder(OrderDTO order) {
    // This runs in a separate thread, later
    orderService.process(order);
    notificationService.sendConfirmation(order);
}
Session Facade vs Message Facade
AspectSession FacadeMessage Facade
CommunicationSynchronous (request-response)Asynchronous (fire-and-forget)
Client blockingYes, client waitsNo, client continues
TechnologyEJB Session Bean, REST APIJMS, MDB (Message Driven Bean), Kafka
Error handlingImmediate exception to clientDead letter queue, retry mechanism
Use caseReal-time data (stock price, balance)Background tasks (email, report, notification)

Note: "QUEUE" concept in messaging is used in JEE where sent message is consumed by one and only one receiver. In "TOPIC" concept, the message published by producer gets delivered to all the subscribers. This follows the publish-subscribe model.

JMS implementations and modern equivalents

  • ActiveMQ, RabbitMQ (JMS implementations)
  • Apache Kafka (distributed streaming, high throughput)
  • AWS SQS/SNS (cloud-native message service)
  • Spring JMS, Spring AMQP, Spring Cloud Stream (framework support)

Composite View Pattern

Definition

The Composite View is a JEE Presentation Tier Pattern. A view that contains presentation logic having ability to generate views for each page independently, instead of the entire page. If the view logic is scattered in individual views, then those sub-views become a part of a larger composite view.

One line: Break a complex page into reusable sub-views (header, footer, sidebar, content) and compose them together dynamically.

JSP - JavaServer Pages: each view comp is a separate JSP file.

angular/html - PA (pagination applied) to a single column of the DB table.

Composite/PV - PA (pagination applied) to multiple columns from multiple sections from the table together.

Problem: Monolithic Views

Problem: Each page duplicates header and footer home.jsp: products.jsp: +---------------------+ +---------------------+ | header (duplicated) | | header (DUPLICATED!) | +---------------------+ | re-embedded without | | nav links | | any body content will| +---------------------+ | also be changed! | | body content | +---------------------+ | (home specific) | | body content | +---------------------+ | (products specific) | | footer (duplicated) | +---------------------+ +---------------------+ | footer (DUPLICATED!) | +---------------------+ If the header/footer logic (like header/footer logos) are not reusable logics, we need to change them in EVERY page when something changes.

Solution: Composite View with Includes

Solution: Composite View using JSP includes JSP page (main.jsp): header.jsp (reusable) +---------------------+ +---------------------+ | jsp:include | | Logo | Navigation | | file="header.jsp" | | Search | Login | +---------------------+ +---------------------+ | jsp:include | | file="sidebar.jsp" | sidebar.jsp (reusable) | + main content | +---------------------+ +---------------------+ | Categories | Links | | jsp:include | +---------------------+ | file="footer.jsp" | +---------------------+ footer.jsp (reusable) +---------------------+ Makes the common logics in separate each comp | Copyright | Social | and include them to the main page via the main +---------------------+ controller page as composite view. Changes to header happen in ONE file, reflected in ALL pages.

View Layout Types

#LayoutDescription
1Basic layoutHeader + Body + Footer (3 regions)
2Classic layoutHeader + Left sidebar + Body + Footer
3Menu layoutHeader + Body + Right sidebar + Footer
4Full classic layoutHeader + Left sidebar + Body + Right sidebar + Footer
5Divide layoutHeader + multiple equal columns + Footer
// main.jsp using Composite View pattern
<%-- Header: reusable across all pages --%>
<jsp:include page="header.jsp" />

<div class="layout">
    <%-- Sidebar: reusable navigation --%>
    <jsp:include page="sidebar.jsp" />

    <%-- Main content: page-specific --%>
    <div class="content">
        <jsp:include page="${contentPage}" />
    </div>
</div>

<%-- Footer: reusable across all pages --%>
<jsp:include page="footer.jsp" />
Modern Equivalents
  • Apache Tiles: Framework for Composite View in JEE. Define layouts with reusable regions. Each page fills in only the content region.
  • Thymeleaf Layouts: Template inheritance with th:fragment and th:replace.
  • JSF Facelets: Template composition with ui:composition, ui:define, ui:insert.
  • React/Next.js: Layout components that wrap page-specific content. This is exactly what our javabypankaj v2 does with layout.tsx wrapping TopBar + content + Footer.
  • Note: The Tiles framework will also work with Struts or JSF framework's based servlets into frontend web/view patterns.

Each portion of web page that contains static content is called a tile. If the application has multiple pages which contains/need same set of tiles (like the Styles and Layout), tiles pools to avoid re-development/duplication of tiles.

Comparison Table

AspectSession FacadeMessage FacadeComposite View
TierBusinessBusinessPresentation
PurposeAggregate multiple services into one coarse-grained callAsync version of Session Facade using message queuesCompose reusable view fragments into pages
CommunicationSynchronousAsynchronousN/A (view composition)
ReducesNetwork round tripsClient blocking timeView code duplication
Modern equivalentAPI Gateway, GraphQLKafka, RabbitMQ, SQSReact layouts, Thymeleaf templates
JEE Pattern Business Tier Presentation Tier

Session Facade, Message Facade, and Composite View Patterns

Three related JEE patterns | Synchronous vs Asynchronous Communication | Coarse-grained Service Access | Reusable View Components | Full Stock Trading and E-Commerce Examples | 12 Interview Questions

Session Facade Pattern

Definition

The Session Facade is a JEE Business Tier Pattern. It provides a coarse-grained interface to a set of fine-grained external services. Instead of the client making multiple network calls to multiple external services, the client makes ONE call to the Session Facade, which internally coordinates all the fine-grained calls on the server side.

One line: One dummy external comp that sits in front of multiple real external comps, reducing network round trips from the client.

Problem: Multiple Network Round Trips

Problem: BusinessDelegate calls 3 external services directly Client App Server Side (BSE/ICC) round trips BusinessDelegate ----[1]----> GetStockIdComp (External Service 1) <---[2]---- ----[3]----> GetStockValueComp (External Service 2) <---[4]---- ----[5]----> GetStockFutureValueComp (External Service 3) <---[6]---- Total: 6 network round trips (3 requests + 3 responses) Each round trip adds latency. Performance bottleneck.

Here BusinessDelegate comp of Client App is having multiple round trips with multiple external comps which leads to performance issues (problem).

Solution: Session Facade Aggregates Calls

Solution: SessionFacade aggregates 3 services into 1 Client App Server Side 1 round trip BusinessDelegate ----[1]----> SessionFacadeComp <---[2]---- | |--> GetStockIdComp (local call, no network) |--> GetStockValueComp (local call) |--> GetStockFutureValueComp (local call) | All four comps are placed in same environment Total: 2 network trips (1 request + 1 response) instead of 6. Session Facade coordinates all internal calls on the server side. BusinessDelegate is getting all the 3 external comp services by just having one network round trip with SessionFacade comp (dummy comp).

Implementation

// Session Facade interface
public interface IStockFacade {
    StockDetailsDTO getCompleteStockInfo(String stockName);
}

// Session Facade implementation: aggregates 3 services
public class StockFacadeImpl implements IStockFacade {
    private final IGetStockId stockIdService;
    private final IGetStockValue stockValueService;
    private final IGetStockFutureValue futureService;

    public StockFacadeImpl() {
        // All services are local to the Facade (same server)
        this.stockIdService = new GetStockIdImpl();
        this.stockValueService = new GetStockValueImpl();
        this.futureService = new GetStockFutureValueImpl();
    }

    @Override
    public StockDetailsDTO getCompleteStockInfo(String stockName) {
        // 3 internal calls, 0 extra network round trips
        int id = stockIdService.getStockId(stockName);
        double value = stockValueService.getStockValue(id);
        double future = futureService.getFutureValue(id);
        return new StockDetailsDTO(stockName, id, value, future);
    }
}

// Client side: ONE call instead of THREE
StockDetailsDTO result = facade.getCompleteStockInfo("Reliance");
Key Points
  • All four comps (SessionFacade + 3 external services) should be developed as Distributed comps either as WebService comp or as EJB comp. For simplification, we are developing ordinary Java classes implementing Java interfaces.
  • The Session Facade and all external comps it wraps must be placed in the same environment/server so internal calls are local (no network overhead).
  • The Session Facade provides a coarse-grained interface (one method that does everything) instead of multiple fine-grained calls.

Message Facade Pattern

Definition

The Message Facade is a JEE Business Tier Pattern. It is similar to Session Facade but uses asynchronous communication instead of synchronous. The client sends a message to the Message Facade and does not wait for a response. The Facade processes the request in the background using messaging systems like JMS (Java Message Service).

One line: An asynchronous version of Session Facade that uses message queues for non-blocking communication.

Synchronous vs Asynchronous Communication

AspectSynchronous (Session Facade)Asynchronous (Message Facade)
Client behaviorClient sends request and waits (blocked) until response is receivedClient sends message and continues immediately (non-blocking)
ResponseImmediate response in same callResponse delivered later via callback, polling, or notification
TechnologyRMI, EJB Session Beans, REST (default)JMS, Kafka, RabbitMQ, SQS
Use whenClient needs the result immediatelyClient does not need immediate result
ExamplesStock price lookup, balance checkEmail sending, report generation, order processing
Synchronous Communication (Session Facade): Client App External Service | | |---- request (HTTP) --------->| | (client WAITS) |---- process | |---- query DB |<--- response (HTTP) ---------| | (client RESUMES) | If the request takes 10 seconds, client is BLOCKED for 10 seconds. Asynchronous Communication (Message Facade): Client App Message Queue External Service | | | |---- send message ----------->| | | (client CONTINUES |--- deliver message -->| | immediately!) | |---- process | | |---- query DB |<--- notification/callback ---|<---- send response ---| | (later, when ready) Client is free to do other work while the request is being processed.

When to use Message Facade

  • If the client does not need the result immediately (e.g., sending email, generating PDF report)
  • If the processing takes a long time (e.g., data migration, batch processing)
  • If the system needs to handle spikes in traffic (message queue acts as buffer)
  • If the external service may be temporarily unavailable (messages are persisted in queue)
// Message Facade: sends message to JMS queue
public class OrderMessageFacade {

    public void submitOrder(OrderDTO order) {
        // Create JMS message
        // Send to "orders" queue
        // Client is NOT blocked - returns immediately
        jmsTemplate.convertAndSend("orders-queue", order);
        System.out.println("Order submitted to queue. Processing async.");
    }
}

// Message Listener (processes asynchronously)
@JmsListener(destination = "orders-queue")
public void processOrder(OrderDTO order) {
    // This runs in a separate thread, later
    orderService.process(order);
    notificationService.sendConfirmation(order);
}
Session Facade vs Message Facade
AspectSession FacadeMessage Facade
CommunicationSynchronous (request-response)Asynchronous (fire-and-forget)
Client blockingYes, client waitsNo, client continues
TechnologyEJB Session Bean, REST APIJMS, MDB (Message Driven Bean), Kafka
Error handlingImmediate exception to clientDead letter queue, retry mechanism
Use caseReal-time data (stock price, balance)Background tasks (email, report, notification)

Note: "QUEUE" concept in messaging is used in JEE where sent message is consumed by one and only one receiver. In "TOPIC" concept, the message published by producer gets delivered to all the subscribers. This follows the publish-subscribe model.

JMS implementations and modern equivalents

  • ActiveMQ, RabbitMQ (JMS implementations)
  • Apache Kafka (distributed streaming, high throughput)
  • AWS SQS/SNS (cloud-native message service)
  • Spring JMS, Spring AMQP, Spring Cloud Stream (framework support)

Composite View Pattern

Definition

The Composite View is a JEE Presentation Tier Pattern. A view that contains presentation logic having ability to generate views for each page independently, instead of the entire page. If the view logic is scattered in individual views, then those sub-views become a part of a larger composite view.

One line: Break a complex page into reusable sub-views (header, footer, sidebar, content) and compose them together dynamically.

JSP - JavaServer Pages: each view comp is a separate JSP file.

angular/html - PA (pagination applied) to a single column of the DB table.

Composite/PV - PA (pagination applied) to multiple columns from multiple sections from the table together.

Problem: Monolithic Views

Problem: Each page duplicates header and footer home.jsp: products.jsp: +---------------------+ +---------------------+ | header (duplicated) | | header (DUPLICATED!) | +---------------------+ | re-embedded without | | nav links | | any body content will| +---------------------+ | also be changed! | | body content | +---------------------+ | (home specific) | | body content | +---------------------+ | (products specific) | | footer (duplicated) | +---------------------+ +---------------------+ | footer (DUPLICATED!) | +---------------------+ If the header/footer logic (like header/footer logos) are not reusable logics, we need to change them in EVERY page when something changes.

Solution: Composite View with Includes

Solution: Composite View using JSP includes JSP page (main.jsp): header.jsp (reusable) +---------------------+ +---------------------+ | jsp:include | | Logo | Navigation | | file="header.jsp" | | Search | Login | +---------------------+ +---------------------+ | jsp:include | | file="sidebar.jsp" | sidebar.jsp (reusable) | + main content | +---------------------+ +---------------------+ | Categories | Links | | jsp:include | +---------------------+ | file="footer.jsp" | +---------------------+ footer.jsp (reusable) +---------------------+ Makes the common logics in separate each comp | Copyright | Social | and include them to the main page via the main +---------------------+ controller page as composite view. Changes to header happen in ONE file, reflected in ALL pages.

View Layout Types

#LayoutDescription
1Basic layoutHeader + Body + Footer (3 regions)
2Classic layoutHeader + Left sidebar + Body + Footer
3Menu layoutHeader + Body + Right sidebar + Footer
4Full classic layoutHeader + Left sidebar + Body + Right sidebar + Footer
5Divide layoutHeader + multiple equal columns + Footer
// main.jsp using Composite View pattern
<%-- Header: reusable across all pages --%>
<jsp:include page="header.jsp" />

<div class="layout">
    <%-- Sidebar: reusable navigation --%>
    <jsp:include page="sidebar.jsp" />

    <%-- Main content: page-specific --%>
    <div class="content">
        <jsp:include page="${contentPage}" />
    </div>
</div>

<%-- Footer: reusable across all pages --%>
<jsp:include page="footer.jsp" />
Modern Equivalents
  • Apache Tiles: Framework for Composite View in JEE. Define layouts with reusable regions. Each page fills in only the content region.
  • Thymeleaf Layouts: Template inheritance with th:fragment and th:replace.
  • JSF Facelets: Template composition with ui:composition, ui:define, ui:insert.
  • React/Next.js: Layout components that wrap page-specific content. This is exactly what our javabypankaj v2 does with layout.tsx wrapping TopBar + content + Footer.
  • Note: The Tiles framework will also work with Struts or JSF framework's based servlets into frontend web/view patterns.

Each portion of web page that contains static content is called a tile. If the application has multiple pages which contains/need same set of tiles (like the Styles and Layout), tiles pools to avoid re-development/duplication of tiles.

Comparison Table

AspectSession FacadeMessage FacadeComposite View
TierBusinessBusinessPresentation
PurposeAggregate multiple services into one coarse-grained callAsync version of Session Facade using message queuesCompose reusable view fragments into pages
CommunicationSynchronousAsynchronousN/A (view composition)
ReducesNetwork round tripsClient blocking timeView code duplication
Modern equivalentAPI Gateway, GraphQLKafka, RabbitMQ, SQSReact layouts, Thymeleaf templates