merge
This commit is contained in:
parent
4321be4c58
commit
997f1fcb59
@ -1,30 +1,26 @@
|
|||||||
package com.baeldung.akkahttp;
|
package com.baeldung.akkahttp;
|
||||||
|
|
||||||
/**
|
|
||||||
* User Entity
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
|
private final Long id;
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String address;
|
|
||||||
|
|
||||||
public User() {
|
public User() {
|
||||||
this.name = "";
|
this.name = "";
|
||||||
this.address = "";
|
this.id = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User(String name, String address) {
|
public User(Long id, String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.address = address;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public Long getId() {
|
||||||
return address;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -2,71 +2,48 @@ package com.baeldung.akkahttp;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
public interface UserMessages {
|
||||||
* Defines all messages related to User Actor
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public interface UserRegistryMessages {
|
|
||||||
|
|
||||||
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) {
|
public CreateUserMessage(User user) {
|
||||||
this.description = description;
|
this.user = user;
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class CreateUser implements Serializable {
|
public User getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
class GetUserMessage implements Serializable {
|
||||||
private final User user;
|
private static final long serialVersionUID = 1L;
|
||||||
|
private final Long userId;
|
||||||
|
|
||||||
public CreateUser(User user) {
|
public GetUserMessage(Long userId) {
|
||||||
this.user = user;
|
this.userId = userId;
|
||||||
}
|
|
||||||
|
|
||||||
public User getUser() {
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class GetUser implements Serializable {
|
public Long getUserId() {
|
||||||
|
return userId;
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
public GetUser(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,37 +5,39 @@ import java.util.concurrent.CompletionStage;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import akka.actor.ActorRef;
|
import akka.actor.ActorRef;
|
||||||
|
import akka.actor.ActorSystem;
|
||||||
import akka.http.javadsl.marshallers.jackson.Jackson;
|
import akka.http.javadsl.marshallers.jackson.Jackson;
|
||||||
import akka.http.javadsl.model.StatusCodes;
|
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.http.javadsl.server.Route;
|
||||||
import akka.pattern.PatternsCS;
|
import akka.pattern.PatternsCS;
|
||||||
import akka.util.Timeout;
|
import akka.util.Timeout;
|
||||||
import com.baeldung.akkahttp.UserMessages.ActionPerformed;
|
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 scala.concurrent.duration.Duration;
|
||||||
import static akka.http.javadsl.server.PathMatchers.*;
|
import static akka.http.javadsl.server.PathMatchers.*;
|
||||||
|
|
||||||
class UserRoutes extends AllDirectives {
|
class UserServer extends HttpApp {
|
||||||
|
|
||||||
private final ActorRef userActor;
|
private final ActorRef userActor;
|
||||||
|
|
||||||
Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS));
|
Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS));
|
||||||
|
|
||||||
UserRoutes(ActorRef userActor) {
|
UserServer(ActorRef userActor) {
|
||||||
this.userActor = userActor;
|
this.userActor = userActor;
|
||||||
}
|
}
|
||||||
|
|
||||||
Route routes() {
|
@Override
|
||||||
|
public Route routes() {
|
||||||
return path("users", this::postUser)
|
return path("users", this::postUser)
|
||||||
.orElse(path(segment("users").slash(longSegment()), id ->
|
.orElse(path(segment("users").slash(longSegment()), id ->
|
||||||
route(getUser(id),
|
route(getUser(id))));
|
||||||
deleteUser(id))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Route getUser(Long id) {
|
private Route getUser(Long id) {
|
||||||
return get(() -> {
|
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);
|
.thenApply(obj -> (Optional<User>) obj);
|
||||||
|
|
||||||
return onSuccess(() -> user, performed -> {
|
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() {
|
private Route postUser() {
|
||||||
return route(post(() -> entity(Jackson.unmarshaller(User.class), user -> {
|
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);
|
.thenApply(obj -> (ActionPerformed) obj);
|
||||||
|
|
||||||
return onSuccess(() -> userCreated, performed -> {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,35 @@
|
|||||||
package com.baeldung.akkahttp;
|
package com.baeldung.akkahttp;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class UserService {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,13 @@ import akka.http.javadsl.testkit.JUnitRouteTest;
|
|||||||
import akka.http.javadsl.testkit.TestRoute;
|
import akka.http.javadsl.testkit.TestRoute;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class UserRoutesUnitTest extends JUnitRouteTest {
|
public class UserServerUnitTest extends JUnitRouteTest {
|
||||||
|
|
||||||
ActorSystem system = ActorSystem.create("helloAkkaHttpServer");
|
ActorSystem system = ActorSystem.create("helloAkkaHttpServer");
|
||||||
|
|
||||||
ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor");
|
ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor");
|
||||||
|
|
||||||
TestRoute appRoute = testRoute(new UserRoutes(userActorRef).routes());
|
TestRoute appRoute = testRoute(new UserServer(userActorRef).routes());
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenRequest_thenActorResponds() {
|
public void whenRequest_thenActorResponds() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user