in pekko-sample-persistence-dc-java/src/main/java/sample/persistence/res/auction/AuctionEntity.java [248:316]
public CommandHandler<Command, Event, AuctionState> commandHandler() {
CommandHandlerBuilder<Command, Event, AuctionState> builder = newCommandHandlerBuilder();
// running
builder
.forState(state -> state.stillRunning)
.onCommand(
OfferBid.class,
(state, bid) ->
Effect()
.persist(
new BidRegistered(
new Bid(
bid.bidder,
bid.offer,
Instant.ofEpochMilli(
this.getReplicationContext().currentTimeMillis()),
this.getReplicationContext().replicaId()))))
.onCommand(
GetHighestBid.class,
(state, get) -> {
get.replyTo.tell(state.highestBid);
return Effect().none();
})
.onCommand(
Finish.class,
(state, finish) ->
Effect().persist(new AuctionFinished(getReplicationContext().replicaId())))
.onCommand(Close.class, (state, close) -> Effect().unhandled())
.onCommand(
IsClosed.class,
(state, get) -> {
get.replyTo.tell(false);
return Effect().none();
});
// finished
builder
.forAnyState()
.onCommand(OfferBid.class, (state, bid) -> Effect().unhandled())
.onCommand(
GetHighestBid.class,
(state, get) -> {
get.replyTo.tell(state.highestBid);
return Effect().none();
})
.onCommand(
Finish.class,
(state, finish) ->
Effect().persist(new AuctionFinished(getReplicationContext().replicaId())))
.onCommand(
Close.class,
(state, close) ->
Effect()
.persist(
new WinnerDecided(
getReplicationContext().replicaId(),
state.highestBid,
state.highestCounterOffer)))
.onCommand(
IsClosed.class,
(state, get) -> {
get.replyTo.tell(state.isClosed());
return Effect().none();
});
return builder.build();
}