-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
7937f07
commit 0207099
Showing
4 changed files
with
102 additions
and
6 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
37 changes: 37 additions & 0 deletions
37
src/main/java/io/github/aplotnikov/batch/processing/reactor/ClientProcessor.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 io.github.aplotnikov.batch.processing.reactor; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static io.github.aplotnikov.batch.processing.reactor.Response.Status.FAILED; | ||
import static io.github.aplotnikov.batch.processing.reactor.Response.Status.SUCCESS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static java.util.concurrent.locks.LockSupport.parkNanos; | ||
|
||
class ClientProcessor { | ||
|
||
private final AtomicBoolean isLastResponseSuccessful = new AtomicBoolean(); | ||
|
||
private final int pause; | ||
|
||
ClientProcessor(int pause) { | ||
this.pause = pause; | ||
} | ||
|
||
Response process(Client client) { | ||
parkNanos(SECONDS.toNanos(pause)); | ||
return new Response( | ||
client.getId(), | ||
isCurrentResponseSuccessful() ? SUCCESS : FAILED | ||
); | ||
} | ||
|
||
private boolean isCurrentResponseSuccessful() { | ||
boolean isSuccessful; | ||
|
||
do { | ||
isSuccessful = isLastResponseSuccessful.get(); | ||
} while (isLastResponseSuccessful.compareAndExchange(isSuccessful, !isSuccessful)); | ||
|
||
return isSuccessful; | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
src/main/java/io/github/aplotnikov/batch/processing/reactor/Response.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,56 @@ | ||
package io.github.aplotnikov.batch.processing.reactor; | ||
|
||
import java.util.Objects; | ||
|
||
class Response { | ||
|
||
private final long clientId; | ||
|
||
private final Status status; | ||
|
||
Response(long clientId, Status status) { | ||
this.clientId = clientId; | ||
this.status = status; | ||
} | ||
|
||
long getClientId() { | ||
return clientId; | ||
} | ||
|
||
Status getStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
|
||
if (other == null || getClass() != other.getClass()) { | ||
return false; | ||
} | ||
|
||
Response that = (Response) other; | ||
return clientId == that.clientId && | ||
status == that.status; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(clientId, status); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ClientResponse{" + | ||
"clientId=" + clientId + | ||
", status=" + status + | ||
'}'; | ||
} | ||
|
||
enum Status { | ||
SUCCESS, | ||
FAILED | ||
} | ||
} |