Last active
February 1, 2019 18:28
-
-
Save sizovs/3feb4de0b41a09079de29d505ff5e2fd to your computer and use it in GitHub Desktop.
CommandsForTietoFolks.java
This file contains hidden or 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
package lightweight4j.app; | |
import net.jodah.failsafe.Failsafe; | |
import net.jodah.failsafe.RetryPolicy; | |
import java.util.Arrays; | |
import java.util.Collection; | |
public class TietoAmazingTeam { | |
public static void main(String[] args) { | |
var informUser = new InformUserCommand("TietoUser", "We'll finish earlier"); | |
var rejectPayment = new RejectPaymentCommand("123", "No money, bro"); | |
// rejectPayment.execute(); | |
new Try( | |
new Multiple(informUser, rejectPayment) | |
).execute(); | |
} | |
} | |
interface Command { | |
void execute(); | |
} | |
class Try implements Command{ | |
private Command origin; | |
public Try(Command origin) { | |
this.origin = origin; | |
} | |
@Override | |
public void execute() { | |
Failsafe.with(new RetryPolicy<>() | |
.onRetry(objectExecutionAttemptedEvent -> System.out.println("Retried!")) | |
.withMaxAttempts(10)).run(() -> origin.execute()); | |
} | |
} | |
class Multiple implements Command { | |
private Collection<Command> commands; | |
public Multiple(Command... commands) { | |
this.commands = Arrays.asList(commands); | |
} | |
@Override | |
public void execute() { | |
commands.forEach(Command::execute); | |
} | |
} | |
class RejectPaymentCommand implements Command { | |
private String paymentId; | |
private String rejectionReason; | |
public RejectPaymentCommand(String paymentId, String rejectionReason) { | |
this.paymentId = paymentId; | |
this.rejectionReason = rejectionReason; | |
} | |
@Override | |
public void execute() { | |
throw new RuntimeException("Ooops, things went wrong"); | |
// System.out.println("Payment " + paymentId + " has been rejected " + rejectionReason); | |
} | |
} | |
class InformUserCommand implements Command { | |
private String userId; | |
private String message; | |
public InformUserCommand(String userId, String message) { | |
this.userId = userId; | |
this.message = message; | |
} | |
@Override | |
public void execute() { | |
System.out.println("Hey " + userId + "! Here is what you should know: " + message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment