Skip to content

Instantly share code, notes, and snippets.

View nsengupta's full-sized avatar

Nirmalya Sengupta nsengupta

View GitHub Profile
@nsengupta
nsengupta / CB_CallWastePreventor_receive_using_callable.java
Last active April 9, 2017 08:33
CircuitBreaker: CallWastePreventor's callable structure
Callable <CompletionStage<ClubDetailsFromXternalSource>> workingCallable =
new Callable<CompletionStage<ClubDetailsFromXternalSource>>() {
@Override
public CompletionStage<ClubDetailsFromXternalSource> call() throws Exception {
return (
CompletableFuture.supplyAsync(
new Supplier<ClubDetailsFromXternalSource>() {
@Override
public ClubDetailsFromXternalSource get() {
@nsengupta
nsengupta / CB_CallWastePreventor_receive_handling.java
Last active April 9, 2017 08:27
CircuitBreaker: CallWastePrevento's receive() basic structure
if (clubID == 0) { // A forced 'fail' timeout situation, for demonstration
PatternsCS
.pipe(
circuitBreaker.callWithCircuitBreakerCS(nonWorkingCallable),
getContext().system().dispatcher()
)
.to(getSender());
}
else {
@nsengupta
nsengupta / CB_SoccerClubInfoGetter_Failure_Handling.java
Last active April 9, 2017 08:24
CircuitBreaker: SoccerClubInfoGetter's handling of failure
{ // Emulating a failed call to the external service
Thread.sleep(2000);
getSender().tell(
new InteractionProtocol.UnavailableClubDetails("timed out"),
getSelf()
);
}
@nsengupta
nsengupta / CB_Requestor_handle_self_messages.java
Created April 9, 2017 08:07
CircuitBreaker: Requestor's receive function handles self-directed messages
if ( arg0 instanceof ClubDetailsFromXternalSource) {
ClubDetailsFromXternalSource m = (ClubDetailsFromXternalSource)arg0;
ActorRef originalSender = m.originallyAskedBy;
String details = m.clubInfoAsJSON;
originalSender.tell (details, getSelf());
} else
if ( arg0 instanceof TimedOutClubDetails ) {
TimedOutClubDetails m = (TimedOutClubDetails) arg0;
m.toBSentTo.tell("Service unresponsive, try again later", getSelf());
@nsengupta
nsengupta / CB_Reqestor_ask_Handling.java
Last active April 9, 2017 08:02
CircuitBreaker: Requestor's handling of ask() responses
PatternsCS.pipe(
PatternsCS.ask(
circuitBreakerJeeves,
new InteractionProtocol.RetrievableClubIDMessageWithFinalDeliveryAddress(
clubID, getSender()
),
ASK_TIMEOUT
)
.handle((messageOK, exceptionReported) -> {
@nsengupta
nsengupta / retrieve-tables-of-player
Created July 18, 2015 02:08
Retrieving the tables (possibly multiple) tables, a player is playing at
def retrieveTablesPlayerIsAt(p: Player) = {
lobby.get(p).outgoing.view
.filter(e => e.label == "IsPlayingAt")
.map(e => e.target)
.map(_.value)
.collect { case t: GameTable => t }
.toList
}
@nsengupta
nsengupta / gist:2a99819895ae0e9a9563
Created July 17, 2015 17:43
Logic to retrieve all players sitting on a given table.
def retrieveAllPlayersOnTable(tableToSearch: GameTable) = {
val innerTableNode = retrieveAllTablesInner
.view
.filter(nextTable => nextTable == tableToSearch)
.head
innerTableNode.outgoing
.view.filter(nextOut => nextOut.label == "seatOccupiedBy")
.view.map(e => e.target)
@nsengupta
nsengupta / add-list-triples
Created July 17, 2015 17:38
Example of adding Node-Edge-Node triples to a graph
def assignPlayersToTable(pairs: List[(Player,GameTable)]) = {
lobby = lobby ++
pairs.foldLeft(IndexedSeq[LDiEdge[LobbyNode]]())((holder,nextPair) => {
(holder) ++ giveSeatToPlayer(nextPair._1,nextPair._2)
}).toList
}
def giveSeatToPlayer(p: Player, t: GameTable) = {
IndexedSeq[LDiEdge[LobbyNode]](
@nsengupta
nsengupta / add-list-triples
Created July 17, 2015 17:36
Example of adding Node-Edge-Node triples to a graph
def assignPlayersToTable(pairs: List[(Player,GameTable)]) = {
lobby = lobby ++
pairs.foldLeft(IndexedSeq[LDiEdge[LobbyNode]]())((holder,nextPair) => {
(holder :+ rootToPlayer :+ playerToRoot) ++ giveSeatToPlayer(nextPair._1,nextPair._2)
}).toList
}
def giveSeatToPlayer(p: Player, t: GameTable) = {
IndexedSeq[LDiEdge[LobbyNode]](
setRelationWithLabel(p,t,"IsPlayingAt"),
@nsengupta
nsengupta / add-list-triples
Created July 17, 2015 17:33
Example of adding Node-Edge-Node triples to a graph
def assignPlayersToTable(pairs: List[(Player,GameTable)]) = {
lobby = lobby ++
pairs.foldLeft(IndexedSeq[LDiEdge[LobbyNode]]())((holder,nextPair) => {
val rootToPlayer = setRelationWithLabel(playerRootNode,nextPair._1,"IsAPlayer")
val playerToRoot = setRelationWithLabel(nextPair._1,playerRootNode,"RootedAt")
(holder :+ rootToPlayer :+ playerToRoot) ++ giveSeatToPlayer(nextPair._1,nextPair._2)
}).toList
}