-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/io/actor4j/core/pods/functions/FunctionPod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2015-2021, David A. Bauer. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.actor4j.core.pods.functions; | ||
|
||
import io.actor4j.core.actors.ActorRef; | ||
import io.actor4j.core.messages.ActorMessage; | ||
import io.actor4j.core.pods.ActorPod; | ||
import io.actor4j.core.pods.PodContext; | ||
import io.actor4j.core.pods.actors.PodActor; | ||
|
||
public abstract class FunctionPod extends ActorPod { | ||
@Override | ||
public PodActor create() { | ||
return new PodActor() { | ||
protected PodFunction podFunction; | ||
|
||
@Override | ||
public void preStart() { | ||
if (getContext().isShard()) | ||
setAlias(domain()+getContext().getShardId()); | ||
else | ||
setAlias(domain()); | ||
|
||
register(); | ||
} | ||
|
||
@Override | ||
public void receive(ActorMessage<?> message) { | ||
podFunction.handle(message); | ||
} | ||
|
||
@Override | ||
public void register() { | ||
podFunction = createFunction(this, getContext()); | ||
} | ||
}; | ||
} | ||
|
||
public abstract PodFunction createFunction(ActorRef host, PodContext context); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/io/actor4j/core/pods/functions/PodFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2015-2020, David A. Bauer. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.actor4j.core.pods.functions; | ||
|
||
import io.actor4j.core.actors.ActorRef; | ||
import io.actor4j.core.messages.ActorMessage; | ||
import io.actor4j.core.pods.PodContext; | ||
|
||
public abstract class PodFunction { | ||
protected ActorRef host; | ||
protected PodContext context; | ||
|
||
public PodFunction(ActorRef host, PodContext context) { | ||
super(); | ||
this.host = host; | ||
this.context = context; | ||
} | ||
|
||
public abstract void handle(ActorMessage<?> message); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/io/actor4j/core/pods/functions/PodRemoteFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2015-2020, David A. Bauer. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.actor4j.core.pods.functions; | ||
|
||
import io.actor4j.core.actors.ActorRef; | ||
import io.actor4j.core.pods.PodContext; | ||
import io.actor4j.core.pods.RemotePodMessage; | ||
import io.actor4j.core.utils.Pair; | ||
|
||
public abstract class PodRemoteFunction extends PodFunction { | ||
public PodRemoteFunction(ActorRef host, PodContext context) { | ||
super(host, context); | ||
} | ||
|
||
public abstract Pair<Object, Integer> handle(RemotePodMessage remoteMessage); | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/io/actor4j/core/pods/functions/RemoteFunctionPod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2015-2021, David A. Bauer. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.actor4j.core.pods.functions; | ||
|
||
import io.actor4j.core.actors.ActorRef; | ||
import io.actor4j.core.messages.ActorMessage; | ||
import io.actor4j.core.pods.PodContext; | ||
import io.actor4j.core.pods.RemotePodMessage; | ||
import io.actor4j.core.pods.actors.PodActor; | ||
import io.actor4j.core.pods.actors.RemoteHandlerPodActor; | ||
import io.actor4j.core.utils.Pair; | ||
|
||
public abstract class RemoteFunctionPod extends FunctionPod { | ||
@Override | ||
public PodActor create() { | ||
return new PodActor() { | ||
protected PodFunction podFunction; | ||
protected PodRemoteFunction podRemoteFunction; | ||
|
||
@Override | ||
public void preStart() { | ||
if (getContext().isShard()) | ||
setAlias(domain()+getContext().getShardId()); | ||
else | ||
setAlias(domain()); | ||
|
||
register(); | ||
} | ||
|
||
@Override | ||
public void receive(ActorMessage<?> message) { | ||
if (message.value!=null && message.value instanceof RemotePodMessage) { | ||
Pair<Object, Integer> result = podRemoteFunction.handle((RemotePodMessage)message.value); | ||
internal_callback((RemotePodMessage)message.value, result); | ||
} | ||
else | ||
podFunction.handle(message); | ||
} | ||
|
||
@Override | ||
public void register() { | ||
podFunction = createFunction(this, getContext()); | ||
podRemoteFunction = createRemoteFunction(this, getContext()); | ||
} | ||
}; | ||
} | ||
|
||
protected void internal_callback(RemotePodMessage remoteMessage, Pair<Object, Integer> result) { | ||
if (remoteMessage.remotePodMessageDTO.reply && RemoteHandlerPodActor.internal_server_callback!=null) | ||
RemoteHandlerPodActor.internal_server_callback.accept(remoteMessage.replyAddress, result.a, result.b); | ||
} | ||
|
||
public abstract PodRemoteFunction createRemoteFunction(ActorRef host, PodContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/io/actor4j/core/features/pod/ExampleReplicationWithFunctionPod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2015-2021, David A. Bauer. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.actor4j.core.features.pod; | ||
|
||
import static io.actor4j.core.logging.user.ActorLogger.logger; | ||
|
||
import io.actor4j.core.actors.ActorRef; | ||
import io.actor4j.core.messages.ActorMessage; | ||
import io.actor4j.core.pods.PodContext; | ||
import io.actor4j.core.pods.functions.FunctionPod; | ||
import io.actor4j.core.pods.functions.PodFunction; | ||
|
||
public class ExampleReplicationWithFunctionPod extends FunctionPod { | ||
@Override | ||
public PodFunction createFunction(ActorRef host, PodContext context) { | ||
return new PodFunction(host, context) { | ||
@Override | ||
public void handle(ActorMessage<?> message) { | ||
logger().debug(message.value.toString()); | ||
host.tell(String.format("Hello %s! [domain:%s, primaryReplica:%s]", | ||
message.value, | ||
context.getDomain(), | ||
context.isPrimaryReplica()) | ||
, 42, message.source, message.interaction); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public String domain() { | ||
return "ExampleReplicationWithFunctionPod"; | ||
} | ||
} |