Skip to content

Commit

Permalink
Compile under sbt again, simplify API further
Browse files Browse the repository at this point in the history
  • Loading branch information
raboof committed Jul 6, 2021
1 parent d21a262 commit 65efcfb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ lazy val samples = project
.aggregate(
// FIXME include samples again
// `java-eventing-shopping-cart`,
// `java-customer-registry`
`java-customer-registry`
)

lazy val `java-eventing-shopping-cart` = project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@

package customer;

import com.akkaserverless.javasdk.Effect;
import com.akkaserverless.javasdk.valueentity.CommandContext;
import com.google.protobuf.Empty;
import customer.api.CustomerApi;
import customer.domain.CustomerDomain;

import java.util.Optional;

/**
* This is where the user will implement his business logic.
*
* We might generate an initial version, but after that
* re-generation should update just the interface and the users'
* build tooling should indicate what needs changing.
* <p>We might generate an initial version, but after that re-generation should update just the
* interface and the users' build tooling should indicate what needs changing.
*/
public class CustomerValueEntity extends CustomerValueEntityInterface {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package customer;

import com.akkaserverless.javasdk.Effect;
import com.akkaserverless.javasdk.Reply;
import com.akkaserverless.javasdk.impl.*;
import com.akkaserverless.javasdk.impl.valueentity.AdaptedCommandContext;
import com.akkaserverless.javasdk.lowlevel.ValueEntityHandler;
import com.akkaserverless.javasdk.valueentity.CommandContext;
import com.akkaserverless.javasdk.valueentity.ValueEntityBase;
import com.google.protobuf.Any;
import com.google.protobuf.Descriptors;
import com.google.protobuf.GeneratedMessageV3;
import customer.api.CustomerApi;
import customer.domain.CustomerDomain;
Expand All @@ -33,29 +34,39 @@
import java.lang.reflect.InvocationTargetException;

/**
* This class would be generated by codegen and should be
* treated mostly as an implementation detail by the user.
* This class would be generated by codegen and should be treated mostly as an implementation detail
* by the user.
*/
public class CustomerValueEntityHandler implements ValueEntityHandler {
final CustomerValueEntity entity;
final AnySupport anySupport;

CustomerValueEntityHandler(CustomerValueEntity entity, AnySupport anySupport) {
public static final Descriptors.ServiceDescriptor serviceDescriptor =
CustomerApi.getDescriptor().findServiceByName("CustomerService");
public static final String entityType = "customers";

CustomerValueEntityHandler(CustomerValueEntity entity) {
this.entity = entity;
this.anySupport = anySupport;
this.anySupport =
new AnySupport(
new Descriptors.FileDescriptor[] {CustomerDomain.getDescriptor()},
CustomerValueEntityHandler.class.getClassLoader(),
AnySupport.DefaultTypeUrlPrefix(),
AnySupport.PREFER_JAVA());
}

@Override
public Reply<Any> handleCommand(Any command, Any state, CommandContext<Any> context)
throws Throwable {
public ValueEntityBase.Effect<Any> handleCommand(
Any command, Any state, CommandContext<Any> context) throws Throwable {
AdaptedCommandContext<CustomerDomain.CustomerState> adaptedContext =
new AdaptedCommandContext(context, anySupport);
try {
CustomerDomain.CustomerState parsedState =
CustomerDomain.CustomerState.parseFrom(state.getValue());
// TODO we used to support passing in the command as Jackson-parsed model object
// or as ScalaPB class as well. With this change we tie ourselves to Java protobuf.
Effect<? extends GeneratedMessageV3> effect = invoke(command, parsedState, adaptedContext);
ValueEntityBase.Effect<? extends GeneratedMessageV3> effect =
invoke(command, parsedState, adaptedContext);
// TODO we used to support accepting ScalaPB and Jackson objects as responses as well.
// Are we OK with losing that? I guess we could have separate methods for that on the
// builder, though it's not obvious how to achieve type-safety for those then.
Expand Down Expand Up @@ -88,7 +99,7 @@ public com.google.protobuf.any.Any emptyState() {
UnknownFieldSet.empty());
}

public Effect<? extends GeneratedMessageV3> invoke(
public ValueEntityBase.Effect<? extends GeneratedMessageV3> invoke(
Any command,
CustomerDomain.CustomerState state,
CommandContext<CustomerDomain.CustomerState> context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@

package customer;

import com.akkaserverless.javasdk.Effect;
import com.akkaserverless.javasdk.valueentity.CommandContext;
import com.akkaserverless.javasdk.valueentity.ValueEntityBase;
import com.google.protobuf.Empty;
import customer.api.CustomerApi;
import customer.domain.CustomerDomain;

import java.util.Optional;

/**
* This file would be managed by the codegen and provide
* the interface the user implements.
*/
/** This file would be managed by the codegen and provide the interface the user implements. */
public abstract class CustomerValueEntityInterface
extends ValueEntityBase<CustomerDomain.CustomerState> {

Expand Down
27 changes: 4 additions & 23 deletions samples/java-customer-registry/src/main/java/customer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public final class Main {

public static void main(String[] args) throws Exception {
LOG.info("started");
ClassLoader classLoader = Main.class.getClassLoader();
String typeUrlPrefix = AnySupport.DefaultTypeUrlPrefix();
AnySupport.Prefer prefer = AnySupport.PREFER_JAVA();

if (args.length == 0) {
// This is for value entity
Expand All @@ -55,26 +52,10 @@ public static void main(String[] args) throws Exception {
// end::register[]
.lowLevel()
.registerValueEntity(
// TODO this factory probably can disappear, we can pass in the handler directly?
context ->
new CustomerValueEntityHandler(
// TODO I guess construction the Entity should somehow remain part of the
// customer API, so they can pass
// in anything they like in the constructor, including the context?
new CustomerValueEntity(),
new AnySupport(
new Descriptors.FileDescriptor[] {CustomerDomain.getDescriptor()},
classLoader,
typeUrlPrefix,
prefer)),
CustomerApi.getDescriptor().findServiceByName("CustomerService"),
"customers",
ValueEntityOptions.defaults(),
CustomerDomain.getDescriptor())
.registerValueEntity(
CustomerValueEntity.class,
CustomerApi.getDescriptor().findServiceByName("CustomerService"),
CustomerDomain.getDescriptor())
context -> new CustomerValueEntityHandler(new CustomerValueEntity()),
CustomerValueEntityHandler.serviceDescriptor,
CustomerValueEntityHandler.entityType,
ValueEntityOptions.defaults())
.start()
.toCompletableFuture()
.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,13 @@ public AkkaServerless registerAction(
* @param descriptor The descriptor for the service that this entity implements.
* @param entityType The entity type name
* @param entityOptions The options for this entity.
* @param additionalDescriptors Any additional descriptors that should be used to look up
* protobuf types when needed.
* @return This stateful service builder.
*/
public AkkaServerless registerValueEntity(
ValueEntityFactory factory,
Descriptors.ServiceDescriptor descriptor,
String entityType,
ValueEntityOptions entityOptions,
Descriptors.FileDescriptor... additionalDescriptors) {
ValueEntityOptions entityOptions) {

services.put(
descriptor.getFullName(),
Expand Down

0 comments on commit 65efcfb

Please sign in to comment.