This commit is contained in:
Tom Hombergs 2018-12-16 20:42:33 +01:00
parent 4321be4c58
commit 997f1fcb59
5 changed files with 87 additions and 85 deletions

View File

@ -1,30 +1,26 @@
package com.baeldung.akkahttp;
/**
* User Entity
*
*/
public class User {
private final Long id;
private final String name;
private final String address;
public User() {
this.name = "";
this.address = "";
this.id = null;
}
public User(String name, String address) {
public User(Long id, String name) {
this.name = name;
this.address = address;
this.id = id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
public Long getId() {
return id;
}
}

View File

@ -2,71 +2,48 @@ package com.baeldung.akkahttp;
import java.io.Serializable;
/**
* Defines all messages related to User Actor
*
*/
public interface UserRegistryMessages {
public interface UserMessages {
class GetUsers implements Serializable {
class ActionPerformed implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private final String description;
public ActionPerformed(String description) {
this.description = description;
}
class ActionPerformed implements Serializable {
public String getDescription() {
return description;
}
}
private static final long serialVersionUID = 1L;
class CreateUserMessage implements Serializable {
private final String description;
private static final long serialVersionUID = 1L;
private final User user;
public ActionPerformed(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public CreateUserMessage(User user) {
this.user = user;
}
class CreateUser implements Serializable {
public User getUser() {
return user;
}
}
private static final long serialVersionUID = 1L;
private final User user;
class GetUserMessage implements Serializable {
private static final long serialVersionUID = 1L;
private final Long userId;
public CreateUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public GetUserMessage(Long userId) {
this.userId = userId;
}
class GetUser implements Serializable {
private static final long serialVersionUID = 1L;
private final String name;
public GetUser(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Long getUserId() {
return userId;
}
}
class DeleteUser implements Serializable {
private static final long serialVersionUID = 1L;
private final String name;
public DeleteUser(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}

View File

@ -5,37 +5,39 @@ import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.http.javadsl.marshallers.jackson.Jackson;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.AllDirectives;
import akka.http.javadsl.server.HttpApp;
import akka.http.javadsl.server.Route;
import akka.pattern.PatternsCS;
import akka.util.Timeout;
import com.baeldung.akkahttp.UserMessages.ActionPerformed;
import com.baeldung.akkahttp.UserMessages.CreateUser;
import com.baeldung.akkahttp.UserMessages.CreateUserMessage;
import com.baeldung.akkahttp.UserMessages.GetUserMessage;
import scala.concurrent.duration.Duration;
import static akka.http.javadsl.server.PathMatchers.*;
class UserRoutes extends AllDirectives {
class UserServer extends HttpApp {
private final ActorRef userActor;
Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS));
UserRoutes(ActorRef userActor) {
UserServer(ActorRef userActor) {
this.userActor = userActor;
}
Route routes() {
@Override
public Route routes() {
return path("users", this::postUser)
.orElse(path(segment("users").slash(longSegment()), id ->
route(getUser(id),
deleteUser(id))));
route(getUser(id))));
}
private Route getUser(Long id) {
return get(() -> {
CompletionStage<Optional<User>> user = PatternsCS.ask(userActor, new UserMessages.GetUser(id), timeout)
CompletionStage<Optional<User>> user = PatternsCS.ask(userActor, new GetUserMessage(id), timeout)
.thenApply(obj -> (Optional<User>) obj);
return onSuccess(() -> user, performed -> {
@ -47,20 +49,9 @@ class UserRoutes extends AllDirectives {
});
}
private Route deleteUser(Long id) {
return delete(() -> {
CompletionStage<ActionPerformed> userDeleted = PatternsCS.ask(userActor, new UserMessages.DeleteUser(id), timeout)
.thenApply(obj -> (ActionPerformed) obj);
return onSuccess(() -> userDeleted, performed -> {
return complete(StatusCodes.OK, performed, Jackson.marshaller());
});
});
}
private Route postUser() {
return route(post(() -> entity(Jackson.unmarshaller(User.class), user -> {
CompletionStage<ActionPerformed> userCreated = PatternsCS.ask(userActor, new CreateUser(user), timeout)
CompletionStage<ActionPerformed> userCreated = PatternsCS.ask(userActor, new CreateUserMessage(user), timeout)
.thenApply(obj -> (ActionPerformed) obj);
return onSuccess(() -> userCreated, performed -> {
@ -69,4 +60,11 @@ class UserRoutes extends AllDirectives {
})));
}
public static void main(String[] args) throws Exception {
ActorSystem system = ActorSystem.create("userServer");
ActorRef userActor = system.actorOf(UserActor.props(), "userActor");
UserServer server = new UserServer(userActor);
server.startServer("localhost", 8080, system);
}
}

View File

@ -1,4 +1,35 @@
package com.baeldung.akkahttp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class UserService {
private final static List<User> users = new ArrayList<>();
static {
users.add(new User(1l, "Alice"));
users.add(new User(2l, "Bob"));
users.add(new User(3l, "Chris"));
users.add(new User(4l, "Dick"));
users.add(new User(5l, "Eve"));
users.add(new User(6l, "Finn"));
}
public Optional<User> getUser(Long id) {
return users.stream()
.filter(user -> user.getId()
.equals(id))
.findFirst();
}
public void createUser(User user) {
users.add(user);
}
public List<User> getUsers(){
return users;
}
}

View File

@ -9,13 +9,13 @@ import akka.http.javadsl.testkit.JUnitRouteTest;
import akka.http.javadsl.testkit.TestRoute;
import org.junit.Test;
public class UserRoutesUnitTest extends JUnitRouteTest {
public class UserServerUnitTest extends JUnitRouteTest {
ActorSystem system = ActorSystem.create("helloAkkaHttpServer");
ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor");
TestRoute appRoute = testRoute(new UserRoutes(userActorRef).routes());
TestRoute appRoute = testRoute(new UserServer(userActorRef).routes());
@Test
public void whenRequest_thenActorResponds() {