Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShoppingCartEntity with Akka Typed #1

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ lagom.cluster.join-self = off
remote.port=2552
akka {

loglevel = DEBUG

remote {
log-remote-lifecycle-events = on
netty.tcp {
Expand Down Expand Up @@ -60,4 +62,4 @@ akka {
#
# auto-down-unreachable-after = 10s
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<configuration>

<conversionRule conversionWord="coloredLevel" converterClass="com.lightbend.lagom.internal.logback.ColoredLevel" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",UTC} %coloredLevel %logger [%mdc] - %msg%n</pattern>
</encoder>
</appender>

<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="STDOUT" />
</appender>

<!-- Set logging for all Play library classes -->
<logger name="play" level="INFO" />
<!-- Set logging for all Akka library classes -->
<logger name="akka" level="INFO" />
<logger name="akka.actor.TimerScheduler" level="INFO" />
<logger name="akka.cluster.ddata.Replicator" level="INFO" />
<!-- Set logging for all Lagom library classes -->
<logger name="com.lightbend.lagom" level="INFO" />

<!-- Cassandra and the datasta driver are used by the Lagom event sourcing modules -->
<logger name="org.apache.cassandra" level="ERROR" />
<logger name="com.datastax.driver" level="ERROR" />
<!-- Turn down Kafka noise -->
<logger name="org.apache.kafka" level="WARN" />

<root level="INFO">
<appender-ref ref="ASYNCSTDOUT" />
</root>

</configuration>
24 changes: 16 additions & 8 deletions shopping-cart-java-node1/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@ version in ThisBuild ~= (_.replace('+', '-'))
dynver in ThisBuild ~= (_.replace('+', '-'))

val hibernateEntityManager = "org.hibernate" % "hibernate-entitymanager" % "5.4.2.Final"
val jpaApi = "org.hibernate.javax.persistence" % "hibernate-jpa-2.1-api" % "1.0.0.Final"
val jpaApi = "org.hibernate.javax.persistence" % "hibernate-jpa-2.1-api" % "1.0.0.Final"
val validationApi = "javax.validation" % "validation-api" % "1.1.0.Final"

val akkaVersion = "2.6.0-M3"

lazy val `shopping-cart-java` = (project in file("."))
.aggregate(`shopping-cart-api`, `shopping-cart`)

lazy val `shopping-cart-api` = (project in file("shopping-cart-api"))
.settings(common)
.settings(
libraryDependencies ++= Seq(
lagomJavadslApi,
lombok
)
)
.settings(libraryDependencies ++= Seq(lagomJavadslApi, lombok))

lazy val `shopping-cart` = (project in file("shopping-cart"))
.enablePlugins(LagomJava)
.settings(common)
.settings(dockerSettings)
.settings(
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor-typed" % akkaVersion,
"com.typesafe.akka" %% "akka-cluster-typed" % akkaVersion,
"com.typesafe.akka" %% "akka-cluster-sharding-typed" % akkaVersion,
"com.typesafe.akka" %% "akka-persistence-typed" % akkaVersion,
// Netty needed for Akka classic remoting
"io.netty" % "netty" % "3.10.6.Final",
lagomJavadslPersistenceJdbc,
lagomJavadslPersistenceJpa,
lagomLogback,
Expand Down Expand Up @@ -58,7 +61,12 @@ val akkaDiscoveryKubernetesApi = "com.lightbend.akka.discovery" %% "akka-discove
val lagomJavadslAkkaDiscovery = "com.lightbend.lagom" %% "lagom-javadsl-akka-discovery-service-locator" % LagomVersion.current

def common = Seq(
javacOptions in (Compile,compile) ++= Seq("-Xlint:unchecked", "-Xlint:deprecation", "-parameters", "-Werror")
javacOptions in (Compile, compile) ++= Seq(
"-Xlint:unchecked",
"-Xlint:deprecation",
"-parameters",
"-Werror"
)
)

def dockerSettings = Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.example.shoppingcart.impl;

import akka.Done;
import com.example.shoppingcart.api.ShoppingCart;
import akka.actor.typed.ActorRef;
import akka.persistence.typed.ExpectingReply;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.Preconditions;
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;
import com.lightbend.lagom.serialization.CompressedJsonable;
import com.lightbend.lagom.serialization.Jsonable;
import lombok.Value;
Expand All @@ -17,24 +16,31 @@
* makes it simple to get a complete picture of what commands an entity
* supports.
*/
public interface ShoppingCartCommand extends Jsonable {
public interface ShoppingCartCommand<ReplyType> extends Jsonable, ExpectingReply<ReplyType> {
/**
* A command to update an item.
*
* It has a reply type of {@link akka.Done}, which is sent back to the caller
* It has a reply type of {@link OperationResult}, which is sent back to the caller
* when all the events emitted by this command are successfully persisted.
*/
@SuppressWarnings("serial")
@Value
@JsonDeserialize
final class UpdateItem implements ShoppingCartCommand, CompressedJsonable, PersistentEntity.ReplyType<Done> {
final class UpdateItem implements ShoppingCartCommand<OperationResult>, CompressedJsonable {
public final String productId;
public final int quantity;
private final ActorRef<OperationResult> replyTo;

@JsonCreator
UpdateItem(String productId, int quantity) {
UpdateItem(String productId, int quantity, ActorRef<OperationResult> replyTo) {
this.productId = Preconditions.checkNotNull(productId, "productId");
this.quantity = quantity;
this.replyTo = replyTo;
}

@Override
public ActorRef<OperationResult> replyTo() {
return replyTo;
}
}

Expand All @@ -43,17 +49,50 @@ final class UpdateItem implements ShoppingCartCommand, CompressedJsonable, Persi
*
* The reply type is the {@link ShoppingCartState}
*/
enum Get implements ShoppingCartCommand, PersistentEntity.ReplyType<ShoppingCartState> {
INSTANCE
final class Get implements ShoppingCartCommand<ShoppingCartState> {
private final ActorRef<ShoppingCartState> replyTo;

@JsonCreator
public Get(ActorRef<ShoppingCartState> replyTo) {
this.replyTo = replyTo;
}

@Override
public ActorRef<ShoppingCartState> replyTo() {
return replyTo;
}
}

/**
* A command to checkout the shopping cart.
*
* The reply type is the Done, which will be returned when the events have been
* The reply type is the {@link OperationResult}, which will be returned when the events have been
* emitted.
*/
enum Checkout implements ShoppingCartCommand, PersistentEntity.ReplyType<Done> {
class Checkout implements ShoppingCartCommand<OperationResult> {
private final ActorRef<OperationResult> replyTo;

public Checkout(ActorRef<OperationResult> replyTo) {
this.replyTo = replyTo;
}

@Override
public ActorRef<OperationResult> replyTo() {
return replyTo;
}
}

interface OperationResult {}

enum Confirmed implements OperationResult {
INSTANCE
}

class Rejected implements OperationResult {
public final String reason;

public Rejected(String reason) {
this.reason = reason;
}
}
}
Loading