JAVA-29231 Move modules inside existing container modules (#15492)

This commit is contained in:
anuragkumawat
2024-01-01 11:27:05 -08:00
committed by GitHub
parent 76bde3ff46
commit 2096eac575
209 changed files with 472 additions and 222 deletions
@@ -0,0 +1,23 @@
package com.baeldung.lagom.helloworld.greeting.api;
import static com.lightbend.lagom.javadsl.api.Service.named;
import static com.lightbend.lagom.javadsl.api.Service.restCall;
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.api.transport.Method;
import akka.NotUsed;
public interface GreetingService extends Service {
public ServiceCall<NotUsed, String> handleGreetFrom(String user);
@Override
default Descriptor descriptor() {
return named("greetingservice").withCalls(
restCall(Method.GET, "/api/greeting/:fromUser", this::handleGreetFrom)
).withAutoAcl(true);
}
}
@@ -0,0 +1,29 @@
package com.baeldung.lagom.helloworld.greeting.impl;
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;
public interface GreetingCommand extends Jsonable {
@SuppressWarnings("serial")
@JsonDeserialize
public final class ReceivedGreetingCommand implements GreetingCommand,
CompressedJsonable, PersistentEntity.ReplyType<String> {
private final String fromUser;
@JsonCreator
public ReceivedGreetingCommand(String fromUser) {
this.fromUser = Preconditions.checkNotNull(fromUser, "fromUser");
}
public String getFromUser() {
return fromUser;
}
}
}
@@ -0,0 +1,32 @@
package com.baeldung.lagom.helloworld.greeting.impl;
import java.util.Optional;
import com.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand;
import com.baeldung.lagom.helloworld.greeting.impl.GreetingEvent.ReceivedGreetingEvent;
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;
public class GreetingEntity extends PersistentEntity<GreetingCommand,
GreetingEvent, GreetingState> {
@Override
public Behavior initialBehavior(Optional<GreetingState> snapshotState) {
BehaviorBuilder b = newBehaviorBuilder(new GreetingState("Hello "));
b.setCommandHandler(ReceivedGreetingCommand.class,
(cmd, ctx) -> {
String fromUser = cmd.getFromUser();
String currentGreeting = state().getMessage();
return ctx.thenPersist(
new ReceivedGreetingEvent(fromUser),
evt -> ctx.reply(currentGreeting + fromUser + "!"));
});
b.setEventHandler(ReceivedGreetingEvent.class,
// We simply update the current state
evt -> state().withMessage("Hello Again "));
return b.build();
}
}
@@ -0,0 +1,23 @@
package com.baeldung.lagom.helloworld.greeting.impl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.lightbend.lagom.serialization.Jsonable;
public interface GreetingEvent extends Jsonable {
class ReceivedGreetingEvent implements GreetingEvent {
private final String fromUser;
@JsonCreator
public ReceivedGreetingEvent(String fromUser) {
this.fromUser = fromUser;
}
public String getFromUser() {
return fromUser;
}
}
}
@@ -0,0 +1,48 @@
package com.baeldung.lagom.helloworld.greeting.impl;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import com.baeldung.lagom.helloworld.greeting.api.GreetingService;
import com.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand;
import com.baeldung.lagom.helloworld.weather.api.WeatherService;
import com.baeldung.lagom.helloworld.weather.api.WeatherStats;
import com.google.inject.Inject;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRef;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry;
import akka.NotUsed;
public class GreetingServiceImpl implements GreetingService {
private final PersistentEntityRegistry persistentEntityRegistry;
private final WeatherService weatherService;
@Inject
public GreetingServiceImpl(PersistentEntityRegistry persistentEntityRegistry, WeatherService weatherService) {
this.persistentEntityRegistry = persistentEntityRegistry;
this.weatherService = weatherService;
persistentEntityRegistry.register(GreetingEntity.class);
}
@Override
public ServiceCall<NotUsed, String> handleGreetFrom(String user) {
return request -> {
// Look up the hello world entity for the given ID.
PersistentEntityRef<GreetingCommand> ref = persistentEntityRegistry.refFor(GreetingEntity.class, user);
CompletableFuture<String> greetingResponse = ref.ask(new ReceivedGreetingCommand(user))
.toCompletableFuture();
CompletableFuture<WeatherStats> todaysWeatherInfo =
(CompletableFuture<WeatherStats>) weatherService.weatherStatsForToday().invoke();
try {
return CompletableFuture.completedFuture(greetingResponse.get() +
" Today's weather stats: " + todaysWeatherInfo.get().getMessage());
} catch (InterruptedException | ExecutionException e) {
return CompletableFuture.completedFuture("Sorry Some Error at our end, working on it");
}
};
}
}
@@ -0,0 +1,18 @@
package com.baeldung.lagom.helloworld.greeting.impl;
import com.baeldung.lagom.helloworld.greeting.api.GreetingService;
import com.baeldung.lagom.helloworld.weather.api.WeatherService;
import com.google.inject.AbstractModule;
import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport;
/**
* The module that binds the GreetingService so that it can be served.
*/
public class GreetingServiceModule extends AbstractModule implements ServiceGuiceSupport {
@Override
protected void configure() {
bindServices(serviceBinding(GreetingService.class, GreetingServiceImpl.class));
bindClient(WeatherService.class);
}
}
@@ -0,0 +1,24 @@
package com.baeldung.lagom.helloworld.greeting.impl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize
public class GreetingState {
private final String message;
@JsonCreator
public GreetingState(String message) {
this.message = message;
}
GreetingState withMessage(String message) {
return new GreetingState(message);
}
public String getMessage() {
return message;
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>