Skip to content

Commit 9a2f8a8

Browse files
authored
Merge branch 'master' into BAEL-10836
2 parents bba9a99 + 6f93d7f commit 9a2f8a8

381 files changed

Lines changed: 1996 additions & 601 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎akka-http/pom.xml‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
6+
7+
<modelVersion>4.0.0</modelVersion>
8+
<artifactId>akka-http</artifactId>
9+
<name>akka-http</name>
10+
11+
<parent>
12+
<artifactId>parent-modules</artifactId>
13+
<groupId>com.baeldung</groupId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.typesafe.akka</groupId>
20+
<artifactId>akka-http_2.12</artifactId>
21+
<version>${akka.http.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.typesafe.akka</groupId>
25+
<artifactId>akka-stream_2.12</artifactId>
26+
<version>2.5.11</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.typesafe.akka</groupId>
30+
<artifactId>akka-http-jackson_2.12</artifactId>
31+
<version>${akka.http.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.typesafe.akka</groupId>
35+
<artifactId>akka-http-testkit_2.12</artifactId>
36+
<version>${akka.http.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<properties>
42+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
44+
<java.version>1.8</java.version>
45+
<akka.http.version>10.0.11</akka.http.version>
46+
<akka.stream.version>2.5.11</akka.stream.version>
47+
</properties>
48+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.akkahttp;
2+
3+
public class User {
4+
5+
private final Long id;
6+
7+
private final String name;
8+
9+
public User() {
10+
this.name = "";
11+
this.id = null;
12+
}
13+
14+
public User(Long id, String name) {
15+
this.name = name;
16+
this.id = id;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.akkahttp;
2+
3+
import akka.actor.AbstractActor;
4+
import akka.actor.Props;
5+
import akka.japi.pf.FI;
6+
import com.baeldung.akkahttp.UserMessages.ActionPerformed;
7+
import com.baeldung.akkahttp.UserMessages.CreateUserMessage;
8+
import com.baeldung.akkahttp.UserMessages.GetUserMessage;
9+
10+
11+
class UserActor extends AbstractActor {
12+
13+
private UserService userService = new UserService();
14+
15+
static Props props() {
16+
return Props.create(UserActor.class);
17+
}
18+
19+
@Override
20+
public Receive createReceive() {
21+
return receiveBuilder()
22+
.match(CreateUserMessage.class, handleCreateUser())
23+
.match(GetUserMessage.class, handleGetUser())
24+
.build();
25+
}
26+
27+
private FI.UnitApply<CreateUserMessage> handleCreateUser() {
28+
return createUserMessageMessage -> {
29+
userService.createUser(createUserMessageMessage.getUser());
30+
sender().tell(new ActionPerformed(String.format("User %s created.", createUserMessageMessage.getUser()
31+
.getName())), getSelf());
32+
};
33+
}
34+
35+
private FI.UnitApply<GetUserMessage> handleGetUser() {
36+
return getUserMessageMessage -> {
37+
sender().tell(userService.getUser(getUserMessageMessage.getUserId()), getSelf());
38+
};
39+
}
40+
41+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.akkahttp;
2+
3+
import java.io.Serializable;
4+
5+
public interface UserMessages {
6+
7+
class ActionPerformed implements Serializable {
8+
9+
private static final long serialVersionUID = 1L;
10+
11+
private final String description;
12+
13+
public ActionPerformed(String description) {
14+
this.description = description;
15+
}
16+
17+
public String getDescription() {
18+
return description;
19+
}
20+
}
21+
22+
class CreateUserMessage implements Serializable {
23+
24+
private static final long serialVersionUID = 1L;
25+
private final User user;
26+
27+
public CreateUserMessage(User user) {
28+
this.user = user;
29+
}
30+
31+
public User getUser() {
32+
return user;
33+
}
34+
}
35+
36+
class GetUserMessage implements Serializable {
37+
private static final long serialVersionUID = 1L;
38+
private final Long userId;
39+
40+
public GetUserMessage(Long userId) {
41+
this.userId = userId;
42+
}
43+
44+
public Long getUserId() {
45+
return userId;
46+
}
47+
}
48+
49+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.akkahttp;
2+
3+
import java.util.Optional;
4+
import java.util.concurrent.CompletionStage;
5+
import java.util.concurrent.TimeUnit;
6+
7+
import akka.actor.ActorRef;
8+
import akka.actor.ActorSystem;
9+
import akka.http.javadsl.marshallers.jackson.Jackson;
10+
import akka.http.javadsl.model.StatusCodes;
11+
import akka.http.javadsl.server.HttpApp;
12+
import akka.http.javadsl.server.Route;
13+
import akka.pattern.PatternsCS;
14+
import akka.util.Timeout;
15+
import com.baeldung.akkahttp.UserMessages.ActionPerformed;
16+
import com.baeldung.akkahttp.UserMessages.CreateUserMessage;
17+
import com.baeldung.akkahttp.UserMessages.GetUserMessage;
18+
import scala.concurrent.duration.Duration;
19+
import static akka.http.javadsl.server.PathMatchers.*;
20+
21+
class UserServer extends HttpApp {
22+
23+
private final ActorRef userActor;
24+
25+
Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS));
26+
27+
UserServer(ActorRef userActor) {
28+
this.userActor = userActor;
29+
}
30+
31+
@Override
32+
public Route routes() {
33+
return path("users", this::postUser)
34+
.orElse(path(segment("users").slash(longSegment()), id ->
35+
route(getUser(id))));
36+
}
37+
38+
private Route getUser(Long id) {
39+
return get(() -> {
40+
CompletionStage<Optional<User>> user = PatternsCS.ask(userActor, new GetUserMessage(id), timeout)
41+
.thenApply(obj -> (Optional<User>) obj);
42+
43+
return onSuccess(() -> user, performed -> {
44+
if (performed.isPresent())
45+
return complete(StatusCodes.OK, performed.get(), Jackson.marshaller());
46+
else
47+
return complete(StatusCodes.NOT_FOUND);
48+
});
49+
});
50+
}
51+
52+
private Route postUser() {
53+
return route(post(() -> entity(Jackson.unmarshaller(User.class), user -> {
54+
CompletionStage<ActionPerformed> userCreated = PatternsCS.ask(userActor, new CreateUserMessage(user), timeout)
55+
.thenApply(obj -> (ActionPerformed) obj);
56+
57+
return onSuccess(() -> userCreated, performed -> {
58+
return complete(StatusCodes.CREATED, performed, Jackson.marshaller());
59+
});
60+
})));
61+
}
62+
63+
public static void main(String[] args) throws Exception {
64+
ActorSystem system = ActorSystem.create("userServer");
65+
ActorRef userActor = system.actorOf(UserActor.props(), "userActor");
66+
UserServer server = new UserServer(userActor);
67+
server.startServer("localhost", 8080, system);
68+
}
69+
70+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.akkahttp;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public class UserService {
8+
9+
private final static List<User> users = new ArrayList<>();
10+
11+
static {
12+
users.add(new User(1l, "Alice"));
13+
users.add(new User(2l, "Bob"));
14+
users.add(new User(3l, "Chris"));
15+
users.add(new User(4l, "Dick"));
16+
users.add(new User(5l, "Eve"));
17+
users.add(new User(6l, "Finn"));
18+
}
19+
20+
public Optional<User> getUser(Long id) {
21+
return users.stream()
22+
.filter(user -> user.getId()
23+
.equals(id))
24+
.findFirst();
25+
}
26+
27+
public void createUser(User user) {
28+
users.add(user);
29+
}
30+
31+
public List<User> getUsers(){
32+
return users;
33+
}
34+
35+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.akkahttp;
2+
3+
import akka.actor.ActorRef;
4+
import akka.actor.ActorSystem;
5+
import akka.http.javadsl.model.ContentTypes;
6+
import akka.http.javadsl.model.HttpEntities;
7+
import akka.http.javadsl.model.HttpRequest;
8+
import akka.http.javadsl.testkit.JUnitRouteTest;
9+
import akka.http.javadsl.testkit.TestRoute;
10+
import org.junit.Test;
11+
12+
public class UserServerUnitTest extends JUnitRouteTest {
13+
14+
ActorSystem system = ActorSystem.create("helloAkkaHttpServer");
15+
16+
ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor");
17+
18+
TestRoute appRoute = testRoute(new UserServer(userActorRef).routes());
19+
20+
@Test
21+
public void whenRequest_thenActorResponds() {
22+
23+
appRoute.run(HttpRequest.GET("/users/1"))
24+
.assertEntity(alice())
25+
.assertStatusCode(200);
26+
27+
appRoute.run(HttpRequest.GET("/users/42"))
28+
.assertStatusCode(404);
29+
30+
appRoute.run(HttpRequest.DELETE("/users/1"))
31+
.assertStatusCode(200);
32+
33+
appRoute.run(HttpRequest.DELETE("/users/42"))
34+
.assertStatusCode(200);
35+
36+
appRoute.run(HttpRequest.POST("/users")
37+
.withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, zaphod())))
38+
.assertStatusCode(201);
39+
40+
}
41+
42+
private String alice() {
43+
return "{\"id\":1,\"name\":\"Alice\"}";
44+
}
45+
46+
private String zaphod() {
47+
return "{\"id\":42,\"name\":\"Zaphod\"}";
48+
}
49+
50+
}

‎apache-curator/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<properties>
6060
<curator.version>4.0.1</curator.version>
6161
<zookeeper.version>3.4.11</zookeeper.version>
62-
<jackson-databind.version>2.9.4</jackson-databind.version>
62+
<jackson-databind.version>2.9.7</jackson-databind.version>
6363
<!-- testing -->
6464
<assertj.version>3.6.1</assertj.version>
6565
<avaitility.version>1.7.0</avaitility.version>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.interfaces;
2+
3+
public interface Electronic {
4+
//Constant variable
5+
public static final String LED = "LED";
6+
7+
//Abstract method
8+
public int getElectricityUse();
9+
10+
// Static method
11+
public static boolean isEnergyEfficient(String electtronicType) {
12+
if (electtronicType.equals(LED)) {
13+
return true;
14+
}
15+
return false;
16+
}
17+
18+
//Default method
19+
public default void printDescription() {
20+
System.out.println("Electronic Description");
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.interfaces;
2+
3+
public class Employee {
4+
5+
private double salary;
6+
7+
public double getSalary() {
8+
return salary;
9+
}
10+
11+
public void setSalary(double salary) {
12+
this.salary = salary;
13+
}
14+
15+
}

0 commit comments

Comments
 (0)