Skip to content

Commit 5f201b3

Browse files
committed
✨ add module actor
1 parent 12f2104 commit 5f201b3

File tree

13 files changed

+201
-0
lines changed

13 files changed

+201
-0
lines changed

IOT-Guide-Actor/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
<parent>
6+
<artifactId>IOT-Guide</artifactId>
7+
<groupId>iot.technology</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>IOT-Guide-Actor</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
</properties>
18+
19+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package iot.technology.actor;
2+
3+
/**
4+
* @author mushuwei
5+
*/
6+
public interface Actor {
7+
8+
boolean process(ActorMsg msg);
9+
10+
ActorRef getActorRef();
11+
12+
default void init() throws ActorException {
13+
}
14+
15+
default void destroy() throws ActorException {
16+
}
17+
18+
default InitFailureStrategy onInitFailure(int attempt, Throwable t) {
19+
return InitFailureStrategy.retryWithDelay(5000L * attempt);
20+
}
21+
22+
default ProcessFailureStrategy onProcessFailure(Throwable t) {
23+
if (t instanceof Error) {
24+
return ProcessFailureStrategy.stop();
25+
} else {
26+
return ProcessFailureStrategy.resume();
27+
}
28+
}
29+
}
30+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package iot.technology.actor;
2+
3+
/**
4+
* @author mushuwei
5+
*/
6+
public interface ActorCreator {
7+
8+
ActorId createActorId();
9+
10+
Actor createActor();
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package iot.technology.actor;
2+
3+
import java.util.List;
4+
import java.util.function.Predicate;
5+
import java.util.function.Supplier;
6+
7+
/**
8+
* @author mushuwei
9+
*/
10+
public interface ActorCtx extends ActorRef {
11+
12+
ActorId getSelf();
13+
14+
ActorRef getParentRef();
15+
16+
void tell(ActorId target, ActorMsg msg);
17+
18+
void stop(ActorId target);
19+
20+
ActorRef getOrCreateChildActor(ActorId actorId, Supplier<String> dispatcher, Supplier<ActorCreator> creator);
21+
22+
void broadcastToChildren(ActorMsg msg);
23+
24+
void broadcastToChildren(ActorMsg msg, Predicate<ActorId> childFilter);
25+
26+
List<ActorId> filterChildren(Predicate<ActorId> childFilter);
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package iot.technology.actor;
2+
3+
/**
4+
* @author mushuwei
5+
*/
6+
public class ActorException extends Exception {
7+
8+
public static final long serialVersionUID = 8209771144711980882L;
9+
10+
public ActorException(String message, Throwable cause) {
11+
super(message, cause);
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package iot.technology.actor;
2+
3+
/**
4+
* @author mushuwei
5+
*/
6+
public interface ActorId {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package iot.technology.actor;
2+
3+
/**
4+
* @author mushuwei
5+
*/
6+
public interface ActorMsg {
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package iot.technology.actor;
2+
3+
/**
4+
* @author mushuwei
5+
*/
6+
public interface ActorRef {
7+
8+
ActorId getActorId();
9+
10+
void tell(ActorMsg actorMsg);
11+
12+
void tellWithHighPriority(ActorMsg actorMsg);
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package iot.technology.actor;
2+
3+
/**
4+
* @author mushuwei
5+
*/
6+
public interface ActorSystem {
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package iot.technology.actor;
2+
3+
import lombok.Getter;
4+
import lombok.ToString;
5+
6+
/**
7+
* @author mushuwei
8+
*/
9+
@ToString
10+
public class InitFailureStrategy {
11+
@Getter
12+
private boolean stop;
13+
@Getter
14+
private long retryDelay;
15+
16+
private InitFailureStrategy(boolean stop, long retryDelay) {
17+
this.stop = stop;
18+
this.retryDelay = retryDelay;
19+
}
20+
21+
public static InitFailureStrategy retryImmediately() {
22+
return new InitFailureStrategy(false, 0);
23+
}
24+
25+
public static InitFailureStrategy retryWithDelay(long ms) {
26+
return new InitFailureStrategy(false, ms);
27+
}
28+
29+
public static InitFailureStrategy stop() {
30+
return new InitFailureStrategy(true, 0);
31+
}
32+
}

0 commit comments

Comments
 (0)