-
-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Besok
committed
Nov 2, 2019
1 parent
e382794
commit 768e647
Showing
12 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
layout: pattern | ||
title: Saga | ||
folder: Communication | ||
permalink: /patterns/saga/ | ||
categories: Behavioral | ||
tags: | ||
- Java | ||
- Difficulty-Expert | ||
- Idiom | ||
- Distributed communication | ||
--- | ||
|
||
## Also known as | ||
This pattern has a similar goal with two-phase commit (XA transaction) | ||
|
||
## Intent | ||
This pattern is used in distributed services to perform a group of operations atomically. | ||
This is an analog of transaction in a database but in terms of microservices architecture this is performed in a distributed environment | ||
|
||
## Explanation | ||
A saga is a sequence of local transactions in a certain context. If one transaction fails for some reason, | ||
the saga executes compensating transactions(rollbacks) to undo the impact of the preceding transactions. | ||
There are two types of Saga: | ||
|
||
- Choreography-Based Saga. | ||
In this approach, there is no central orchestrator. | ||
Each service participating in the Saga performs their transaction and publish events. | ||
The other services act upon those events and perform their transactions. | ||
Also, they may or not publish other events based on the situation. | ||
|
||
- Orchestration-Based Saga | ||
In this approach, there is a Saga orchestrator that manages all the transactions and directs | ||
the participant services to execute local transactions based on events. | ||
This orchestrator can also be though of as a Saga Manager. | ||
|
||
## Applicability | ||
Use the Saga pattern, if: | ||
- you need to perform a group of operations related to different microservices atomically | ||
- you need to rollback changes in different places in case of failure one of the operation | ||
- you need to take care of data consistency in different places including different databases | ||
- you can not use 2PC(two phase commit) | ||
|
||
## Credits | ||
- [pattern description](https://microservices.io/patterns/data/saga.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
The MIT License | ||
Copyright © 2014-2019 Ilkka Seppälä | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
--> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.iluwatar</groupId> | ||
<artifactId>java-design-patterns</artifactId> | ||
<version>1.22.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>saga</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>ie.home.besok</groupId> | ||
<artifactId>move-clicker</artifactId> | ||
<version>1.0</version> | ||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.10</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
15 changes: 15 additions & 0 deletions
15
stirring-clicker/src/main/java/ie/home/besok/stirrings/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ie.home.besok.stirrings; | ||
|
||
import java.awt.*; | ||
import java.io.IOException; | ||
|
||
public class Application { | ||
public static void main(String[] args) throws IOException { | ||
FileStorage storage = new FileStorage(); | ||
|
||
EventQueue.invokeLater(() -> { | ||
Gui gui = new Gui(storage); | ||
gui.setVisible(true); | ||
}); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
stirring-clicker/src/main/java/ie/home/besok/stirrings/Counter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ie.home.besok.stirrings; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class Counter { | ||
public Map<LocalDate,Long> count(List<String> dates){ | ||
return null; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
stirring-clicker/src/main/java/ie/home/besok/stirrings/FileStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ie.home.besok.stirrings; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class FileStorage { | ||
|
||
private Path file; | ||
|
||
public FileStorage() throws IOException { | ||
this.file = Paths.get("data.log"); | ||
if(!Files.exists(file)){ | ||
Files.createFile(file); | ||
} | ||
} | ||
|
||
public void plus() { | ||
String line = LocalDateTime.now().toString()+System.lineSeparator(); | ||
try { | ||
Files.write(file, line.getBytes(), StandardOpenOption.APPEND); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public List<String> get() throws IOException { | ||
return Files.readAllLines(file); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
stirring-clicker/src/main/java/ie/home/besok/stirrings/Gui.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ie.home.besok.stirrings; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
public class Gui extends JFrame { | ||
|
||
private FileStorage storage; | ||
|
||
public Gui(FileStorage storage) { | ||
this.storage = storage; | ||
try { | ||
createUI(storage); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void createUI(FileStorage storage) throws IOException { | ||
setTitle("Stirring counter"); | ||
setSize(300, 300); | ||
setLocationRelativeTo(null); | ||
|
||
|
||
JButton button = createButton(storage); | ||
JButton graphick = new JButton(); | ||
graphick.setIcon(getIcon("3.jpg")); | ||
|
||
|
||
Container pane = getContentPane(); | ||
GroupLayout gl = new GroupLayout(pane); | ||
pane.setLayout(gl); | ||
|
||
gl.setAutoCreateContainerGaps(true); | ||
|
||
gl.setHorizontalGroup( | ||
gl.createSequentialGroup().addComponent(button).addComponent(graphick) | ||
); | ||
|
||
gl.setVerticalGroup(gl.createSequentialGroup().addComponent(button).addComponent(graphick)); | ||
|
||
|
||
button.addActionListener((event) -> { | ||
storage.plus(); | ||
try { | ||
JOptionPane.showMessageDialog(null,"","",JOptionPane.INFORMATION_MESSAGE, getIcon("2.jpg")); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
|
||
setDefaultCloseOperation(EXIT_ON_CLOSE); | ||
} | ||
|
||
private JButton createButton(FileStorage storage) throws IOException { | ||
ImageIcon babyIcon = getIcon("1.png"); | ||
|
||
JButton button = new JButton(); | ||
|
||
button.setIcon(babyIcon); | ||
return button; | ||
} | ||
|
||
private ImageIcon getIcon(String name) throws IOException { | ||
URL file = this.getClass().getClassLoader().getResource(name); | ||
return new ImageIcon(ImageIO.read(file)); | ||
} | ||
|
||
|
||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions
25
stirring-clicker/src/test/java/ie/home/besok/stirrings/FileStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ie.home.besok.stirrings; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class FileStorageTest { | ||
|
||
@Test | ||
public void fsTest() throws IOException { | ||
FileStorage fs = new FileStorage(); | ||
List<String> arrs = fs.get(); | ||
int oldSize = arrs.size(); | ||
fs.plus(); | ||
fs.plus(); | ||
fs.plus(); | ||
fs.plus(); | ||
arrs = fs.get(); | ||
int newSize = arrs.size(); | ||
assertEquals(4, newSize - oldSize); | ||
} | ||
} |