Skip to content

Commit

Permalink
feature: Claim check pattern azure (iluwatar#1897)
Browse files Browse the repository at this point in the history
* Archietecture Diagram added

* Added pom.xml

* Architecture Diagram Updated

* Added Microservices and kafka

* ReadME File Added

* ReadME file Updated

* iluwatar#1329 ReadMe file updated and working pattern code added

* iluwatar#1329 readme file updated

* iluwatar#1329 readme file updated and java documentation added

* iluwatar#1329 repository merged

* Update claim-check-pattern/ReadME.md

iluwatar#1329 Real world description updated

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Update claim-check-pattern/ReadME.md

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Applicability section in ReadMe file updated

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Tutorial section in ReadMe file updated

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Storage Data section in ReadMe File updated

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 workflow section Update claim-check-pattern/ReadME.md

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Update claim-check-pattern/pom.xml

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 deleted mvnw.cmd file

* iluwatar#1329 deleted  drawio file

* iluwatar#1329 deleted mvnw file

* iluwatar#1329 deleted mvnw file

* iluwatar#1329 mvnw.cmd file deleted

* iluwatar#1329 Update claim-check-pattern/usage-cost-processor/src/main/java/com/callusage/domain/MessageHeader.java

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Update claim-check-pattern/usage-cost-processor/src/main/java/com/callusage/domain/UsageCostDetail.java

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Update claim-check-pattern/usage-cost-processor/src/main/java/com/callusage/domain/UsageDetail.java

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 deleted mvnw file

* iluwatar#1329 deleted mvnw.cmd file

* iluwatar#1329 Update claim-check-pattern/usage-detail-sender/src/main/java/com/callusage/domain/Message.java

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 Update claim-check-pattern/ReadME.md

Co-authored-by: Subhrodip Mohanta <[email protected]>

* iluwatar#1329 pom file dependencies fixed, readmeflie updated, removed unused imports

* iluwatar#1329 Readfile updated, class javadoc added

* iluwatar#1329  UML class diagrams added, readme file updated

* iluwatar#1329  lombok annotations used on model classes, common dependencies moved to parent pom

* iluwatar#1329  test cases added

* include claim-check-pattern in parent pom

* iluwatar#1329 code smells fixed

* iluwatar#1329 code smells removed

* iluwatar#1329 security issues fixed

* iluwatar#1329 updated pom files and refactored packages name

* iluwatar#1329 checkstyle warning fixed

* iluwatar#1329 code coverage increased

* iluwatar#1329 minor changed.

* checkpoint created with common utility

* Claim-Check-Pattern | Shrirang97 | Implemented using Java Azure Functions

* Claim-Check-Pattern | Shrirang97 | Updated Functions logic

* Update MessageHandlerUtility.java

* Update UsageCostProcessorFunction.java

* claim-check-pattern | Shrirang97 | Added test cases

* claim-check-pattern | Shrirang97 | Test cases for Azure functions fixed

* Claim-Check-Pattern | Shrirang | Used string as request body

* claim-check-pattern | Shrirang | Working test cases

* claim-check-pattern | Shrirang | Issue fixed while deserializing

* claim-check-pattern | Shrirang | removed unused import

* claim-check-pattern | Shrirang | fixed refactoring

* claim-chek-pattern | Shrirang | added lombok | fixed dependencies & test cases

* Delete .DS_Store

* claim-check-pattern | Shrirang | Fixed unrelated file

* Update BookService.java

* Update BookService.java

* claim-check-pattern | Shrirang | Fixed unrelated files

* claim-check-pattern | Shrirang | Fixed review comments

* Update UsageCostProcessorFunction.java

* Update ReadME.md

* Rename ReadME.md to README.md

* claim-check-pattern | Shrirang | Incorporated review comments.

* Update cloud-claim-check-pattern/README.md

Co-authored-by: Ilkka Seppälä <[email protected]>

* Update cloud-claim-check-pattern/README.md

Co-authored-by: Ilkka Seppälä <[email protected]>

* Update cloud-claim-check-pattern/README.md

Co-authored-by: Ilkka Seppälä <[email protected]>

* Update cloud-claim-check-pattern/README.md

Co-authored-by: Ilkka Seppälä <[email protected]>

* Update cloud-claim-check-pattern/README.md

Co-authored-by: Ilkka Seppälä <[email protected]>

* Update cloud-claim-check-pattern/README.md

Co-authored-by: Ilkka Seppälä <[email protected]>

* Update cloud-claim-check-pattern/README.md

Co-authored-by: Ilkka Seppälä <[email protected]>

* Updated readme file

* Read me file updated | Added more description

Co-authored-by: Subhrodip Mohanta <[email protected]>
Co-authored-by: Subhrodip Mohanta <[email protected]>
Co-authored-by: Ilkka Seppälä <[email protected]>
  • Loading branch information
4 people authored Dec 11, 2021
1 parent b22c8bc commit 9063336
Show file tree
Hide file tree
Showing 28 changed files with 2,010 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cloud-claim-check-pattern/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Build output
target/
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IDE
.idea/
*.iml
.settings/
.project
.classpath

# macOS
.DS_Store

# Azure Functions
local.settings.json
bin/
obj/
82 changes: 82 additions & 0 deletions cloud-claim-check-pattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
layout: pattern
title: Claim Check Pattern
folder: cloud-claim-check-pattern
permalink: /patterns/cloud-claim-check-pattern/
categories: Cloud
tags:
- Cloud distributed
- Microservices
---

## Name

[Claim Check Pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/claim-check)

## Also known as

[Reference-Based Messaging](https://www.enterpriseintegrationpatterns.com/patterns/messaging/StoreInLibrary.html)

## Intent

- Reduce the load of data transfer through the Internet. Instead of sending actual data directly, just send the message reference.
- Improve data security. As only message reference is shared, no data is exposed to the Internet.

## Explanation

Real-World Example

> Suppose if you want to build a photo processing system. A photo processing system takes an image as input, processes it, and outputs a different set of images. Consider system has one persistent storage, one input component, ten processing components, messaging platform. Once a photo is given to the input component, it stores that image on persistent storage. It then creates ten different messages/events with the same image location and publishes them to the messaging platform. The messaging platform triggers ten different processing components. The ten processing components extract information about image location from the received event and then read an image from persistent storage. They generate ten different images from the original image and drop these images again to persistent storage.
In Plain words

> Split a large message into a claim check and a payload. Send the claim check to the messaging platform and store the payload to an external service. This pattern allows large messages to be processed while protecting the message bus and the client from being overwhelmed or slowed down. This pattern also helps to reduce costs, as storage is usually cheaper than resource units used by the messaging platform.([ref](https://docs.microsoft.com/en-us/azure/architecture/patterns/claim-check))
## Architecture Diagram

![alt text](./etc/Claim-Check-Pattern.png "Claim Check Pattern")

## Applicability

Use the Claim Check Pattern when

- Huge processing data causes a lot of bandwidth consumption to transfer data through the Internet.
- To secure your data transfer by storing in common persistent storage.
- Using a cloud platform - Azure Functions or AWS Lambda, Azure EventGrid or AWS Event Bridge, Azure Blob Storage or AWS S3 Bucket.
- Each service must be independent and idempotent. Output data is dropped to persistent storage by the service.
- Publish-subscribe messaging pattern needs to be used.

## Consequences

- This pattern is stateless. Any compute API will not store any data.
- You must have persistent storage and a reliable messaging platform.

## Tutorials

### Workflow

Suppose a telecom company wants to build call cost calculator system which generate the call cost daily. At the end of each day, details of the calls made by the consumers are stored somewhere. The call calculator system will read this data and generate call cost data for each user. Consumers will be billed using this generated data in case of postpaid service.

Producer class( `UsageDetailPublisherFunction` Azure Function) will generate call usage details (here we are generating call data in producer class itself. In real world scenario, it will read from storage). `UsageDetailPublisherFunction` creates a message. Message consists of message header and message body. Message header is basically an event grid event or claim or message reference. Message body contains actual data. `UsageDetailPublisherFunction` sends a message header to Event Grid topic `usage-detail` and drops an entire message to the blob storage. Event Grid then sent this message header to the `UsageCostProcessorFunction` Azure function. It will read the entire message from blob storage with the help of the header, will calculate call cost and drop the result to the blob storage.

### Class Diagrams

![alt text](./etc/class-diagram.png "Claim-Check-Class-Diagram")

### Setup

- Any operating system can be used macOS, Windows, Linux as everything is deployed on Azure.
- Install Java JDK 11 and set up Java environmental variables.
- Install Git.
- Install Visual Studio Code.
- Install [ Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) to be able to deploy using Visual studio.

### Storage Data

The data is stored in the Azure blob storage in the container `callusageapp`. For every trigger, one GUID is created. Under the `GUID folder`, 2 files will be created `input.json` and `output.json`.
`Input.json` is dropped `producer` azure function which contains call usage details.` Output.json` contains call cost details which are dropped by the `consumer` azure function.

## Credits

- [Messaging Pattern - Claim Check](https://www.enterpriseintegrationpatterns.com/patterns/messaging/StoreInLibrary.html)
- [Azure Architecture Pattern - Claim Check Pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/claim-check)
38 changes: 38 additions & 0 deletions cloud-claim-check-pattern/call-usage-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Build output
target/
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IDE
.idea/
*.iml
.settings/
.project
.classpath

# macOS
.DS_Store

# Azure Functions
local.settings.json
bin/
obj/
117 changes: 117 additions & 0 deletions cloud-claim-check-pattern/call-usage-app/etc/call-usage-app.urm.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
@startuml
package com.iluwatar.claimcheckpattern.producer.calldetails.functions {
class UsageDetailPublisherFunction {
- eventHandlerUtility : EventHandlerUtility<MessageHeader>
- messageHandlerUtility : MessageHandlerUtility<UsageDetail>
+ UsageDetailPublisherFunction()
+ UsageDetailPublisherFunction(messageHandlerUtility : MessageHandlerUtility<UsageDetail>, eventHandlerUtility : EventHandlerUtility<MessageHeader>)
+ run(request : HttpRequestMessage<Optional<String>>, context : ExecutionContext) : HttpResponseMessage
}
}
package com.iluwatar.claimcheckpattern.domain {
class Message<T> {
- messageBody : MessageBody<T>
- messageHeader : MessageHeader
+ Message<T>()
+ getMessageBody() : MessageBody<T>
+ getMessageHeader() : MessageHeader
+ setMessageBody(messageBody : MessageBody<T>)
+ setMessageHeader(messageHeader : MessageHeader)
}
class MessageBody<T> {
- data : List<T>
+ MessageBody<T>()
+ getData() : List<T>
+ setData(data : List<T>)
}
class MessageHeader {
- data : Object
- dataVersion : String
- eventTime : String
- eventType : String
- id : String
- subject : String
- topic : String
+ MessageHeader()
+ getData() : Object
+ getDataVersion() : String
+ getEventTime() : String
+ getEventType() : String
+ getId() : String
+ getSubject() : String
+ getTopic() : String
+ setData(data : Object)
+ setDataVersion(dataVersion : String)
+ setEventTime(eventTime : String)
+ setEventType(eventType : String)
+ setId(id : String)
+ setSubject(subject : String)
+ setTopic(topic : String)
}
class MessageReference {
- dataFileName : String
- dataLocation : String
+ MessageReference()
+ MessageReference(dataLocation : String, dataFileName : String)
+ getDataFileName() : String
+ getDataLocation() : String
+ setDataFileName(dataFileName : String)
+ setDataLocation(dataLocation : String)
}
class UsageCostDetail {
- callCost : double
- dataCost : double
- userId : String
+ UsageCostDetail()
+ getCallCost() : double
+ getDataCost() : double
+ getUserId() : String
+ setCallCost(callCost : double)
+ setDataCost(dataCost : double)
+ setUserId(userId : String)
}
class UsageDetail {
- data : int
- duration : int
- userId : String
+ UsageDetail()
+ getData() : int
+ getDuration() : int
+ getUserId() : String
+ setData(data : int)
+ setDuration(duration : int)
+ setUserId(userId : String)
}
}
package com.iluwatar.claimcheckpattern.utility {
class EventHandlerUtility<T> {
- customEventClient : EventGridPublisherClient<BinaryData>
+ EventHandlerUtility<T>()
+ EventHandlerUtility<T>(customEventClient : EventGridPublisherClient<BinaryData>)
+ publishEvent(customEvent : T, logger : Logger)
}
class MessageHandlerUtility<T> {
- blobServiceClient : BlobServiceClient
+ MessageHandlerUtility<T>()
+ MessageHandlerUtility<T>(blobServiceClient : BlobServiceClient)
+ dropToPersistantStorage(message : Message<T>, logger : Logger)
+ readFromPersistantStorage(messageReference : MessageReference, logger : Logger) : Message<T>
}
}
package com.iluwatar.claimcheckpattern.consumer.callcostprocessor.functions {
class UsageCostProcessorFunction {
- messageHandlerUtilityForUsageCostDetail : MessageHandlerUtility<UsageCostDetail>
- messageHandlerUtilityForUsageDetail : MessageHandlerUtility<UsageDetail>
+ UsageCostProcessorFunction()
+ UsageCostProcessorFunction(messageHandlerUtilityForUsageDetail : MessageHandlerUtility<UsageDetail>, messageHandlerUtilityForUsageCostDetail : MessageHandlerUtility<UsageCostDetail>)
- calculateUsageCostDetails(usageDetailsList : List<UsageDetail>) : List<UsageCostDetail>
+ run(request : HttpRequestMessage<Optional<String>>, context : ExecutionContext) : HttpResponseMessage
}
}
UsageCostProcessorFunction --> "-messageHandlerUtilityForUsageDetail" MessageHandlerUtility
Message --> "-messageBody" MessageBody
UsageDetailPublisherFunction --> "-eventHandlerUtility" EventHandlerUtility
Builder ..+ HttpResponseMessage
UsageDetailPublisherFunction --> "-messageHandlerUtility" MessageHandlerUtility
Message --> "-messageHeader" MessageHeader
@enduml
7 changes: 7 additions & 0 deletions cloud-claim-check-pattern/call-usage-app/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
Loading

0 comments on commit 9063336

Please sign in to comment.