Skip to content

Commit 875de17

Browse files
committed
make the actor builder return a Props
1 parent 13466ed commit 875de17

File tree

4 files changed

+14
-50
lines changed

4 files changed

+14
-50
lines changed
Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package com.arpnetworking.akka;
22

3-
import akka.actor.ActorRef;
4-
import akka.actor.ActorSystem;
53
import akka.actor.Props;
64
import com.arpnetworking.commons.builder.OvalBuilder;
7-
import net.sf.oval.constraint.NotEmpty;
8-
import net.sf.oval.constraint.NotNull;
9-
import org.codehaus.jackson.map.annotate.JacksonInject;
105

116
import java.util.function.Function;
127

@@ -16,34 +11,14 @@
1611
* @param <B> The type of the builder
1712
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
1813
*/
19-
public abstract class ActorBuilder<B extends ActorBuilder<B>> extends OvalBuilder<ActorRef> {
14+
public abstract class ActorBuilder<B extends ActorBuilder<B>> extends OvalBuilder<Props> {
2015
/**
2116
* Protected constructor.
2217
*
2318
* @param createProps method to create a {@link Props} from the {@link ActorBuilder}
2419
*/
2520
protected ActorBuilder(final Function<B, Props> createProps) {
26-
super(B::createActor);
27-
_createProps = createProps;
28-
}
29-
30-
public B setActorName(final String value) {
31-
_actorName = value;
32-
return self();
33-
}
34-
35-
public B setActorSystem(final ActorSystem value) {
36-
_actorSystem = value;
37-
return self();
38-
}
39-
40-
/**
41-
* Creates an actor in the {@link ActorSystem}. DO NOT OVERRIDE.
42-
*
43-
* @return an {@link ActorRef} to a newly launched actor
44-
*/
45-
protected final ActorRef createActor(){
46-
return this._actorSystem.actorOf(_createProps.apply(self()));
21+
super(createProps);
4722
}
4823

4924
/**
@@ -53,14 +28,4 @@ protected final ActorRef createActor(){
5328
* @return instance with correct {@link ActorBuilder} class type.
5429
*/
5530
protected abstract B self();
56-
57-
@NotNull
58-
@JacksonInject
59-
protected ActorSystem _actorSystem;
60-
61-
@NotNull
62-
@NotEmpty
63-
protected String _actorName;
64-
65-
private final Function<B, Props> _createProps;
6631
}

src/main/java/com/arpnetworking/clusteraggregator/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import akka.actor.ActorRef;
1919
import akka.actor.ActorSystem;
20+
import akka.actor.Props;
2021
import ch.qos.logback.classic.LoggerContext;
2122
import com.arpnetworking.clusteraggregator.configuration.ClusterAggregatorConfiguration;
2223
import com.arpnetworking.commons.builder.Builder;
@@ -100,7 +101,7 @@ public static void main(final String[] args) {
100101
configurator = Optional.of(new Configurator<>(Main::new, ClusterAggregatorConfiguration.class));
101102
final ObjectMapper objectMapper = ClusterAggregatorConfiguration.createObjectMapper();
102103
final SimpleModule module = new SimpleModule();
103-
module.addDeserializer(ActorRef.class, new ActorBuilderDeserializer(objectMapper));
104+
module.addDeserializer(Props.class, new ActorBuilderDeserializer(objectMapper));
104105
objectMapper.registerModule(module);
105106
configuration = Optional.of(new DynamicConfiguration.Builder()
106107
.setObjectMapper(objectMapper)

src/main/java/com/arpnetworking/configuration/jackson/akka/ActorBuilderDeserializer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.arpnetworking.configuration.jackson.akka;
22

3-
import akka.actor.ActorRef;
3+
import akka.actor.Props;
44
import com.arpnetworking.commons.builder.Builder;
55
import com.fasterxml.jackson.core.JsonParser;
66
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -17,7 +17,7 @@
1717
* Deserializer that will create an ActorBuilder for the given actor, then create the Actor from Guice
1818
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
1919
*/
20-
public class ActorBuilderDeserializer extends JsonDeserializer<ActorRef> {
20+
public class ActorBuilderDeserializer extends JsonDeserializer<Props> {
2121
/**
2222
* Public constructor.
2323
*
@@ -28,13 +28,13 @@ public ActorBuilderDeserializer(final ObjectMapper mapper) {
2828
}
2929

3030
@Override
31-
public ActorRef deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
31+
public Props deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
3232
final TreeNode treeNode = p.readValueAsTree();
3333
final String type = ((TextNode) treeNode.get("type")).textValue();
3434
try {
3535
final Class<?> clazz = Class.forName(type);
36-
final Class<? extends Builder<? extends ActorRef>> builder = getBuilderForClass(clazz);
37-
final Builder<? extends ActorRef> value = _mapper.readValue(treeNode.toString(), builder);
36+
final Class<? extends Builder<? extends Props>> builder = getBuilderForClass(clazz);
37+
final Builder<? extends Props> value = _mapper.readValue(treeNode.toString(), builder);
3838
return value.build();
3939
} catch (ClassNotFoundException e) {
4040
throw new JsonMappingException(p, String.format("Unable to find class %s referenced by ActorRef type", type));
@@ -44,9 +44,9 @@ public ActorRef deserialize(final JsonParser p, final DeserializationContext ctx
4444
}
4545

4646
@SuppressWarnings("unchecked")
47-
private static Class<? extends Builder<ActorRef>> getBuilderForClass(final Class<?> clazz)
47+
private static Class<? extends Builder<Props>> getBuilderForClass(final Class<?> clazz)
4848
throws ClassNotFoundException {
49-
return (Class<? extends Builder<ActorRef>>) (Class.forName(
49+
return (Class<? extends Builder<Props>>) (Class.forName(
5050
clazz.getName() + "$Builder",
5151
true, // initialize
5252
clazz.getClassLoader()));

src/test/java/com/arpnetworking/clusteraggregator/configuration/ActorBuilderTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.arpnetworking.clusteraggregator.configuration;
22

3-
import akka.actor.ActorRef;
3+
import akka.actor.Props;
44
import com.arpnetworking.akka.NonJoiningClusterJoiner;
55
import com.arpnetworking.commons.jackson.databind.ObjectMapperFactory;
66
import com.arpnetworking.configuration.jackson.akka.ActorBuilderDeserializer;
@@ -19,21 +19,19 @@ public class ActorBuilderTest extends BaseActorTest {
1919
@Test
2020
public void testBuild() {
2121
final NonJoiningClusterJoiner.Builder builder = new NonJoiningClusterJoiner.Builder();
22-
builder.setActorName("foo");
23-
builder.setActorSystem(getSystem());
2422
builder.build();
2523
}
2624

2725
@Test
2826
public void testPolyDeserialize() throws IOException {
2927
final ObjectMapper mapper = ObjectMapperFactory.createInstance();
3028
final SimpleModule module = new SimpleModule();
31-
module.addDeserializer(ActorRef.class, new ActorBuilderDeserializer(mapper));
29+
module.addDeserializer(Props.class, new ActorBuilderDeserializer(mapper));
3230
mapper.registerModule(module);
3331

3432
@Language("JSON") final String data = "{\n" +
3533
" \"type\": \"com.arpnetworking.akka.NonJoiningClusterJoiner\"\n" +
3634
"}";
37-
final ActorRef ref = mapper.readValue(data, ActorRef.class);
35+
final Props props = mapper.readValue(data, Props.class);
3836
}
3937
}

0 commit comments

Comments
 (0)