Skip to content

Commit c72f08a

Browse files
committed
Merge pull request '合并更新版本到本地服务' (#12) from active_mq into main
Reviewed-on: https://src.isharkfly.com/iSharkFly-Docs/java-tutorials/pulls/12
2 parents 1eafa91 + 17e3ee9 commit c72f08a

28,671 files changed

Lines changed: 1604899 additions & 8766 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.
Lines changed: 32 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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>
Lines changed: 32 additions & 0 deletions
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+
}
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 45 additions & 0 deletions
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+
}
Lines changed: 73 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)