From 69472132bc2d3ed08a96dbdf41a024c223d003e9 Mon Sep 17 00:00:00 2001 From: Adriano Santos Date: Wed, 21 Aug 2024 14:49:25 -0300 Subject: [PATCH] fix: some java imports --- .../src/main/java/io/eigr/spawn/java/App.java | 8 +- .../spawn/java/actors/PostalCodeActor.java | 74 ++++++++++--------- .../eigr/spawn/java/actors/RouterActor.java | 6 +- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java index 4b14ec0..e9611d4 100644 --- a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java +++ b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java @@ -5,9 +5,11 @@ import io.eigr.spawn.api.exceptions.SpawnException; import io.eigr.spawn.api.extensions.DependencyInjector; import io.eigr.spawn.api.extensions.SimpleDependencyInjector; + import io.eigr.spawn.java.actors.PostalCodeActor; import io.eigr.spawn.java.actors.RouterActor; import io.eigr.spawn.java.service.PostalCodeService; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,7 +40,7 @@ public static void main(String[] args) throws SpawnException { spawnSystem.start(); - log.info("Actor running and ready to connection at ports [{}] and [{}]", cfg.userFunctionPort, cfg.port); + log.info("Actor running and ready to connection at ports [{}]", cfg.userFunctionPort); } public record Config(String startupDelaySeconds, @@ -55,13 +57,13 @@ public static Config createDefaultConfig() { String userFunctionHost = System.getenv("USER_FUNCTION_HOST") != null ? System.getenv("USER_FUNCTION_HOST") : "localhost"; String userFunctionPort = System.getenv("USER_FUNCTION_PORT") != null ? System.getenv("USER_FUNCTION_PORT") - : "{{ cookiecutter.app_port }}"; + : "8091"; String spawnProxyHost = System.getenv("SPAWN_PROXY_HOST") != null ? System.getenv("SPAWN_PROXY_HOST") : "localhost"; String spawnProxyPort = System.getenv("SPAWN_PROXY_PORT") != null ? System.getenv("SPAWN_PROXY_PORT") : "9001"; String spawnSystemName = System.getenv("SPAWN_SYSTEM_NAME") != null ? System.getenv("SPAWN_SYSTEM_NAME") - : "{{ cookiecutter.spawn_app_spawn_system }}"; + : "spawn-system"; return new Config(startupDelaySeconds, userFunctionHost, userFunctionPort, spawnProxyHost, spawnProxyPort, spawnSystemName); diff --git a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java index fad5c3f..e48cee7 100644 --- a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java +++ b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java @@ -8,18 +8,19 @@ import io.eigr.spawn.api.actors.behaviors.UnNamedActorBehavior; import io.eigr.spawn.internal.ActionBindings; -import io.eigr.spawn.java.domain.Common; +import io.eigr.spawn.java.domain.DomainProto; import io.eigr.spawn.java.service.PostalCodeService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Map; +import java.util.Optional; import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action; import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name; -public class PostalCodeActor implements StatefulActor { +public class PostalCodeActor implements StatefulActor { private static final Logger log = LoggerFactory.getLogger(PostalCodeActor.class); private PostalCodeService postalCodeService; @@ -29,48 +30,53 @@ public ActorBehavior configure(BehaviorCtx behaviorCtx) { this.postalCodeService = behaviorCtx.getInjector().getInstance(PostalCodeService.class); return new UnNamedActorBehavior( name("PostalCode"), - action("GetPostalCodeData", ActionBindings.of(Common.GetRequest.class, this::getPostalCodeData)) + action("GetPostalCodeData", ActionBindings.of(DomainProto.GetRequest.class, this::getPostalCodeData)) ); } - private Value getPostalCodeData(ActorContext context, Common.GetRequest msg) { + private Value getPostalCodeData(ActorContext context, DomainProto.GetRequest msg) { log.debug("Received invocation. Message: '{}'. Context: '{}'.", msg, context); - Common.PostalCodeState.Builder builder = Common.PostalCodeState.newBuilder(); - if (context.getState().isPresent() && !Common.PostalCodeStatus.UNKNOWN.equals(state.getStatus())) - return Value.at() - .state(state) - .response(Common.GetResponse.newBuilder().setPostalCode(state).build()) - .reply(); + Optional currentState = context.getState(); + + // Return current state if available and not unknown + if (currentState.isPresent() && currentState.get().getStatus() != DomainProto.PostalCodeStatus.UNKNOWN) { + return createValueWithStateAndResponse(currentState.get()); } log.trace("State not present."); - Map postalCode = postalCodeService.find(msg.getCode()); - if(!postalCode.isEmpty()) { - Common.PostalCodeState state = builder.setCode(msg.getCode()) - .setCity(postalCode.get("localidade")) - .setState(postalCode.get("uf")) - .setStreet(postalCode.get("logradouro")) - .setCountry(postalCode.get("pais")) - .setStatus(Common.PostalCodeStatus.FOUND) - .build(); - - Common.GetResponse response = Common.GetResponse.newBuilder() - .setPostalCode(state) - .build(); - - return Value.at() - .state(state) - .response(response) - .reply(); + Map postalCodeData = postalCodeService.find(msg.getCode()); + + // If postal code data found, build state and response + if (!postalCodeData.isEmpty()) { + DomainProto.PostalCodeState newState = buildPostalCodeState(msg.getCode(), postalCodeData, DomainProto.PostalCodeStatus.FOUND); + return createValueWithStateAndResponse(newState); } - return Value.at() - .state(builder - .setStatus(Common.PostalCodeStatus.UNKNOWN) - .build()) - .response(Common.GetResponse.newBuilder().build()) - .reply(); + // Default case: return unknown state + DomainProto.PostalCodeState unknownState = DomainProto.PostalCodeState.newBuilder() + .setStatus(DomainProto.PostalCodeStatus.UNKNOWN) + .build(); + return createValueWithStateAndResponse(unknownState); + } + + private Value createValueWithStateAndResponse(DomainProto.PostalCodeState state) { + DomainProto.GetResponse response = DomainProto.GetResponse.newBuilder() + .setPostalCode(state) + .build(); + return Value.at().state(state).response(response).reply(); } + + private DomainProto.PostalCodeState buildPostalCodeState(String code, Map data, DomainProto.PostalCodeStatus status) { + return DomainProto.PostalCodeState.newBuilder() + .setCode(code) + .setCity(data.get("localidade")) + .setState(data.get("uf")) + .setStreet(data.get("logradouro")) + .setCountry(data.get("pais")) + .setStatus(status) + .build(); + } + } diff --git a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/RouterActor.java b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/RouterActor.java index 63580d2..8548e19 100644 --- a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/RouterActor.java +++ b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/RouterActor.java @@ -13,7 +13,7 @@ import io.eigr.spawn.api.exceptions.ActorCreationException; import io.eigr.spawn.internal.ActionBindings; -import io.eigr.spawn.java.domain.Common; +import io.eigr.spawn.java.domain.DomainProto; import io.eigr.spawn.java.service.PostalCodeService; import org.slf4j.Logger; @@ -32,11 +32,11 @@ public ActorBehavior configure(BehaviorCtx behaviorCtx) { this.postalCodeService = behaviorCtx.getInjector().getInstance(PostalCodeService.class); return new NamedActorBehavior( name("Router"), - action("GetPostalCode", ActionBindings.of(Common.GetRequest.class, this::getPostalCodeData)) + action("GetPostalCode", ActionBindings.of(DomainProto.GetRequest.class, this::getPostalCodeData)) ); } - private Value getPostalCodeData(ActorContext context, Common.GetRequest msg) { + private Value getPostalCodeData(ActorContext context, DomainProto.GetRequest msg) { log.debug("Received invocation. Message: '{}'.", msg, context); try { Spawn spawn = context.getSpawnSystem();