Skip to content

Commit

Permalink
fix: some java imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Santos committed Aug 21, 2024
1 parent e97c6ed commit 6947213
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Common.PostalCodeState> {
public class PostalCodeActor implements StatefulActor<DomainProto.PostalCodeState> {
private static final Logger log = LoggerFactory.getLogger(PostalCodeActor.class);

private PostalCodeService postalCodeService;
Expand All @@ -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<Common.PostalCodeState> context, Common.GetRequest msg) {
private Value getPostalCodeData(ActorContext<DomainProto.PostalCodeState> 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<DomainProto.PostalCodeState> 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<String, String> 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<String, String> 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<String, String> 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();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 6947213

Please sign in to comment.