Skip to content

Commit 7b5e948

Browse files
authored
Merge pull request #2746 from iSharkFly-Docs/active_mq
Merge code for new documents
2 parents 1eafa91 + a51b7b5 commit 7b5e948

File tree

28,688 files changed

+1605302
-8750
lines changed

Some content is hidden

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

28,688 files changed

+1605302
-8750
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: Issue Report
3+
about: Report an issue to help us improve
4+
title: '[ISSUE] '
5+
---
6+
7+
**Article and Module Links**
8+
A link to the affected article and the affected module. You can find the link to the module in the Conclusion section in the "on Github" standard phase.
9+
10+
**Describe the Issue**
11+
A clear and concise description of what the issue is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected Behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Environment (please complete the following information):**
27+
- OS: [e.g. Windows]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Additional Context**
32+
Add any other context about the issue here.

.idea/compiler.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CONTRIBUTING.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contributing to Baeldung Tutorials
2+
First off, thank you for considering contributing to Baeldung Tutorials.
3+
4+
## Reporting Issues
5+
Before you submit an issue, please review the guidelines below:
6+
7+
1. **No Custom Modifications:** If your issue arises from any custom modifications you've made to the code in the repository, we won't be able to assist. We can only help if the issue is reproducible with the untouched codebase from this repo. If you're working with a modified version, consider asking for help on StackOverflow or other relevant forums.
8+
2. **Use a clear and descriptive title** for the issue to identify the problem.
9+
3. **Include a link to the article** you're having issues with.
10+
4. **Describe the exact steps which reproduce the problem** in as many details as possible.
11+
5. **Additional Details:** Offer any other context or descriptions that could be useful. Screenshots, error messages, copy/pasteable snippets, or logs can be immensely helpful.

akka-modules/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Akka
2+
3+
This module contains modules about Akka.

akka-modules/akka-actors/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Akka HTTP
2+
3+
This module contains articles about Akka actors.
4+
5+
### Relevant articles:
6+
7+
- [Introduction to Akka Actors in Java](https://www.baeldung.com/akka-actors-java)

akka-modules/akka-actors/pom.xml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>akka-actors</artifactId>
7+
<name>akka-actors</name>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>akka-modules</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.typesafe.akka</groupId>
18+
<artifactId>akka-actor_${scala.version}</artifactId>
19+
<version>${typesafe-akka.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.typesafe.akka</groupId>
23+
<artifactId>akka-testkit_${scala.version}</artifactId>
24+
<version>${typesafe-akka.version}</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
<properties>
30+
<typesafe-akka.version>2.5.11</typesafe-akka.version>
31+
</properties>
32+
33+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.akkaactors;
2+
3+
import akka.actor.AbstractActor;
4+
import akka.actor.Props;
5+
import akka.event.Logging;
6+
import akka.event.LoggingAdapter;
7+
8+
public class FirstActor extends AbstractActor {
9+
10+
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
11+
12+
public static Props props() {
13+
return Props.create(FirstActor.class);
14+
}
15+
16+
@Override
17+
public void preStart() {
18+
log.info("Actor started");
19+
}
20+
21+
@Override
22+
public void postStop() {
23+
log.info("Actor stopped");
24+
}
25+
26+
// Messages will not be handled
27+
@Override
28+
public Receive createReceive() {
29+
return receiveBuilder()
30+
.build();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.akkaactors;
2+
3+
import akka.actor.AbstractActor;
4+
import akka.event.Logging;
5+
import akka.event.LoggingAdapter;
6+
7+
public class MyActor extends AbstractActor {
8+
9+
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
10+
11+
@Override
12+
public void postStop() {
13+
log.info("Stopping actor {}", this);
14+
}
15+
16+
public Receive createReceive() {
17+
return receiveBuilder()
18+
.matchEquals("printit", p -> {
19+
System.out.println("The address of this actor is: " + getSelf());
20+
getSender().tell("Got Message", getSelf());
21+
})
22+
.build();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.akkaactors;
2+
3+
import akka.actor.AbstractActor;
4+
import akka.actor.Props;
5+
import akka.event.Logging;
6+
import akka.event.LoggingAdapter;
7+
8+
public class PrinterActor extends AbstractActor {
9+
10+
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
11+
12+
public static Props props(String text) {
13+
return Props.create(PrinterActor.class, text);
14+
}
15+
16+
public static final class PrintFinalResult {
17+
Integer totalNumberOfWords;
18+
19+
public PrintFinalResult(Integer totalNumberOfWords) {
20+
this.totalNumberOfWords = totalNumberOfWords;
21+
}
22+
}
23+
24+
@Override
25+
public void preStart() {
26+
log.info("Starting PrinterActor {}", this);
27+
}
28+
29+
@Override
30+
public void postStop() {
31+
log.info("Stopping PrinterActor {}", this);
32+
}
33+
34+
35+
@Override
36+
public Receive createReceive() {
37+
return receiveBuilder()
38+
.match(PrinterActor.PrintFinalResult.class,
39+
r -> {
40+
log.info("Received PrintFinalResult message from " + getSender());
41+
log.info("The text has a total number of {} words", r.totalNumberOfWords);
42+
})
43+
.build();
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.akkaactors;
2+
3+
import akka.actor.AbstractActor;
4+
import akka.actor.ActorRef;
5+
import akka.actor.Props;
6+
import akka.event.Logging;
7+
import akka.event.LoggingAdapter;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
import static akka.pattern.PatternsCS.ask;
14+
15+
public class ReadingActor extends AbstractActor {
16+
17+
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
18+
19+
private String text;
20+
21+
public ReadingActor(String text) {
22+
this.text = text;
23+
}
24+
25+
public static Props props(String text) {
26+
return Props.create(ReadingActor.class, text);
27+
}
28+
29+
public static final class ReadLines {
30+
}
31+
32+
@Override
33+
public void preStart() {
34+
log.info("Starting ReadingActor {}", this);
35+
}
36+
37+
@Override
38+
public void postStop() {
39+
log.info("Stopping ReadingActor {}", this);
40+
}
41+
42+
@Override
43+
public Receive createReceive() {
44+
return receiveBuilder()
45+
.match(ReadLines.class, r -> {
46+
47+
log.info("Received ReadLines message from " + getSender());
48+
49+
String[] lines = text.split("\n");
50+
List<CompletableFuture> futures = new ArrayList<>();
51+
52+
for (int i = 0; i < lines.length; i++) {
53+
String line = lines[i];
54+
ActorRef wordCounterActorRef = getContext().actorOf(Props.create(WordCounterActor.class), "word-counter-" + i);
55+
56+
CompletableFuture<Object> future =
57+
ask(wordCounterActorRef, new WordCounterActor.CountWords(line), 1000).toCompletableFuture();
58+
futures.add(future);
59+
}
60+
61+
Integer totalNumberOfWords = futures.stream()
62+
.map(CompletableFuture::join)
63+
.mapToInt(n -> (Integer) n)
64+
.sum();
65+
66+
ActorRef printerActorRef = getContext().actorOf(Props.create(PrinterActor.class), "Printer-Actor");
67+
printerActorRef.forward(new PrinterActor.PrintFinalResult(totalNumberOfWords), getContext());
68+
// printerActorRef.tell(new PrinterActor.PrintFinalResult(totalNumberOfWords), getSelf());
69+
70+
})
71+
.build();
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.akkaactors;
2+
3+
import akka.actor.AbstractActor;
4+
import akka.event.Logging;
5+
import akka.event.LoggingAdapter;
6+
7+
public class WordCounterActor extends AbstractActor {
8+
9+
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
10+
11+
public static final class CountWords {
12+
String line;
13+
14+
public CountWords(String line) {
15+
this.line = line;
16+
}
17+
}
18+
19+
@Override
20+
public void preStart() {
21+
log.info("Starting WordCounterActor {}", this);
22+
}
23+
24+
@Override
25+
public Receive createReceive() {
26+
return receiveBuilder()
27+
.match(CountWords.class, r -> {
28+
try {
29+
log.info("Received CountWords message from " + getSender());
30+
int numberOfWords = countWordsFromLine(r.line);
31+
getSender().tell(numberOfWords, getSelf());
32+
} catch (Exception ex) {
33+
getSender().tell(new akka.actor.Status.Failure(ex), getSelf());
34+
throw ex;
35+
}
36+
})
37+
.build();
38+
}
39+
40+
private int countWordsFromLine(String line) throws Exception {
41+
42+
if (line == null) {
43+
throw new IllegalArgumentException("The text to process can't be null!");
44+
}
45+
46+
int numberOfWords = 0;
47+
String[] words = line.split(" ");
48+
for (String possibleWord : words) {
49+
if (possibleWord.trim().length() > 0) {
50+
numberOfWords++;
51+
}
52+
}
53+
return numberOfWords;
54+
}
55+
}

0 commit comments

Comments
 (0)