Skip to content

Commit 18a22d9

Browse files
committed
PR feedback, move the actor builder closer to desired
1 parent ac364ff commit 18a22d9

File tree

6 files changed

+49
-48
lines changed

6 files changed

+49
-48
lines changed

src/main/java/com/arpnetworking/akka/ActorBuilder.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package com.arpnetworking.akka;
1717

18-
import akka.actor.Props;
18+
import akka.actor.Actor;
19+
import akka.japi.Creator;
1920
import com.arpnetworking.commons.builder.OvalBuilder;
2021

2122
import java.util.function.Function;
@@ -26,14 +27,14 @@
2627
* @param <B> The type of the builder
2728
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
2829
*/
29-
public abstract class ActorBuilder<B extends ActorBuilder<B>> extends OvalBuilder<Props> {
30+
public abstract class ActorBuilder<B extends ActorBuilder<B, S>, S extends Actor> extends OvalBuilder<S> implements Creator<S> {
3031
/**
31-
* Protected constructor.
32+
* Protected constructor for subclasses.
3233
*
33-
* @param createProps method to create a {@link Props} from the {@link ActorBuilder}
34+
* @param targetConstructor The constructor for the concrete type to be created by this builder.
3435
*/
35-
protected ActorBuilder(final Function<B, Props> createProps) {
36-
super(createProps);
36+
protected ActorBuilder(final Function<B, S> targetConstructor) {
37+
super(targetConstructor);
3738
}
3839

3940
/**
@@ -43,4 +44,14 @@ protected ActorBuilder(final Function<B, Props> createProps) {
4344
* @return instance with correct {@link ActorBuilder} class type.
4445
*/
4546
protected abstract B self();
47+
48+
/**
49+
* {@inheritDoc}
50+
*/
51+
@Override
52+
public S create() throws Exception {
53+
return build();
54+
}
55+
56+
private static final long serialVersionUID = 1L;
4657
}

src/main/java/com/arpnetworking/akka/NonJoiningClusterJoiner.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.arpnetworking.akka;
1717

18-
import akka.actor.Props;
1918
import akka.actor.UntypedActor;
2019
import com.arpnetworking.steno.Logger;
2120
import com.arpnetworking.steno.LoggerFactory;
@@ -26,30 +25,10 @@
2625
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
2726
*/
2827
public final class NonJoiningClusterJoiner extends UntypedActor {
29-
/**
30-
* Static factory method for creating a {@link Props} to create a {@link NonJoiningClusterJoiner} actor.
31-
*
32-
* @return a new {@link Props}
33-
*/
34-
public static Props props() {
35-
return Props.create(NonJoiningClusterJoiner.class);
36-
}
37-
38-
/**
39-
* Static factory method for creating a {@link Props} to create a {@link NonJoiningClusterJoiner} actor from a
40-
* {@link Builder}.
41-
*
42-
* @param builder Builder to create the Props from
43-
* @return a new {@link Props}
44-
*/
45-
private static Props props(final Builder builder) {
46-
return props();
47-
}
48-
4928
/**
5029
* Public constructor.
5130
*/
52-
public NonJoiningClusterJoiner() {
31+
public NonJoiningClusterJoiner(final Builder builder) {
5332
LOGGER.info()
5433
.setMessage("NonJoiningClusterJoiner starting up")
5534
.log();
@@ -71,12 +50,12 @@ public void onReceive(final Object message) throws Exception {
7150
*
7251
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
7352
*/
74-
public static class Builder extends ActorBuilder<Builder> {
53+
public static class Builder extends ActorBuilder<Builder, NonJoiningClusterJoiner> {
7554
/**
7655
* Public constructor.
7756
*/
7857
public Builder() {
79-
super(NonJoiningClusterJoiner::props);
58+
super(NonJoiningClusterJoiner::new);
8059
}
8160

8261
/**
@@ -86,5 +65,7 @@ public Builder() {
8665
public Builder self() {
8766
return this;
8867
}
68+
69+
private static final long serialVersionUID = 1L;
8970
}
9071
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private ActorRef provideTcpServer(final Injector injector, final ActorSystem sys
249249
@Named("cluster-joiner")
250250
@SuppressFBWarnings("UPM_UNCALLED_PRIVATE_METHOD") // Invoked reflectively by Guice
251251
private ActorRef provideClusterJoiner(final ActorSystem system, final ClusterAggregatorConfiguration config) {
252-
return system.actorOf(config.getClusterJoinActor(), "cluster-joiner");
252+
return system.actorOf(Props.create(config.getClusterJoinActor()), "cluster-joiner");
253253
}
254254

255255
@Provides

src/main/java/com/arpnetworking/clusteraggregator/configuration/ClusterAggregatorConfiguration.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package com.arpnetworking.clusteraggregator.configuration;
1717

18-
import akka.actor.Props;
18+
import akka.actor.Actor;
19+
import com.arpnetworking.akka.ActorBuilder;
1920
import com.arpnetworking.akka.NonJoiningClusterJoiner;
2021
import com.arpnetworking.commons.builder.OvalBuilder;
2122
import com.arpnetworking.commons.jackson.databind.ObjectMapperFactory;
@@ -49,7 +50,7 @@ public final class ClusterAggregatorConfiguration {
4950
public static ObjectMapper createObjectMapper() {
5051
final ObjectMapper objectMapper = ObjectMapperFactory.createInstance();
5152
final SimpleModule module = new SimpleModule();
52-
module.addDeserializer(Props.class, new ActorBuilderDeserializer(objectMapper));
53+
module.addDeserializer(ActorBuilder.class, new ActorBuilderDeserializer(objectMapper));
5354
objectMapper.registerModule(module);
5455
return objectMapper;
5556
}
@@ -122,7 +123,7 @@ public String getClusterHostSuffix() {
122123
return _clusterHostSuffix;
123124
}
124125

125-
public Props getClusterJoinActor() {
126+
public ActorBuilder<?, ? extends Actor> getClusterJoinActor() {
126127
return _clusterJoinActor;
127128
}
128129

@@ -191,7 +192,7 @@ private ClusterAggregatorConfiguration(final Builder builder) {
191192
private final Period _jvmMetricsCollectionInterval;
192193
private final RebalanceConfiguration _rebalanceConfiguration;
193194
private final String _clusterHostSuffix;
194-
private final Props _clusterJoinActor;
195+
private final ActorBuilder<?, ? extends Actor> _clusterJoinActor;
195196
private final Map<String, DatabaseConfiguration> _databaseConfigurations;
196197

197198
private static final InterfaceDatabase INTERFACE_DATABASE = ReflectionsDatabase.newInstance();
@@ -411,7 +412,7 @@ public Builder setDatabaseConfigurations(final Map<String, DatabaseConfiguration
411412
* @param value The cluster join actor configuration.
412413
* @return This instance of <code>Builder</code>.
413414
*/
414-
public Builder setClusterJoinActor(final Props value) {
415+
public Builder setClusterJoinActor(final ActorBuilder<?, ? extends Actor> value) {
415416
_clusterJoinActor = value;
416417
return this;
417418
}
@@ -456,7 +457,7 @@ public Builder setClusterJoinActor(final Props value) {
456457
@NotNull
457458
private String _clusterHostSuffix = "";
458459
@NotNull
459-
private Props _clusterJoinActor = new NonJoiningClusterJoiner.Builder().build();
460+
private ActorBuilder<?, ? extends Actor> _clusterJoinActor = new NonJoiningClusterJoiner.Builder();
460461
private Map<String, DatabaseConfiguration> _databaseConfigurations;
461462
}
462463
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package com.arpnetworking.configuration.jackson.akka;
1717

18-
import akka.actor.Props;
18+
import akka.actor.Actor;
19+
import com.arpnetworking.akka.ActorBuilder;
1920
import com.arpnetworking.commons.builder.Builder;
2021
import com.fasterxml.jackson.core.JsonParser;
2122
import com.fasterxml.jackson.core.TreeNode;
@@ -32,7 +33,7 @@
3233
*
3334
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
3435
*/
35-
public class ActorBuilderDeserializer extends JsonDeserializer<Props> {
36+
public class ActorBuilderDeserializer extends JsonDeserializer<ActorBuilder<?, ?>> {
3637
/**
3738
* Public constructor.
3839
*
@@ -43,23 +44,23 @@ public ActorBuilderDeserializer(final ObjectMapper mapper) {
4344
}
4445

4546
@Override
46-
public Props deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
47+
public ActorBuilder<? extends ActorBuilder<?, ?>, ? extends Actor> deserialize(final JsonParser p, final DeserializationContext ctxt)
48+
throws IOException {
4749
final TreeNode treeNode = p.readValueAsTree();
4850
final String type = ((TextNode) treeNode.get("type")).textValue();
4951
try {
5052
final Class<?> clazz = Class.forName(type);
51-
final Class<? extends Builder<? extends Props>> builder = getBuilderForClass(clazz);
52-
final Builder<? extends Props> value = _mapper.readValue(treeNode.traverse(), builder);
53-
return value.build();
53+
final Class<? extends ActorBuilder<? extends ActorBuilder<?, ?>, ? extends Actor>> builder = getBuilderForClass(clazz);
54+
return _mapper.readValue(treeNode.traverse(), builder);
5455
} catch (final ClassNotFoundException e) {
5556
throw new JsonMappingException(p, String.format("Unable to find class %s referenced by Props type", type));
5657
}
5758
}
5859

5960
@SuppressWarnings("unchecked")
60-
private static Class<? extends Builder<Props>> getBuilderForClass(final Class<?> clazz)
61+
private static Class<? extends ActorBuilder<? extends ActorBuilder<?, ?>, ? extends Actor>> getBuilderForClass(final Class<?> clazz)
6162
throws ClassNotFoundException {
62-
return (Class<? extends Builder<Props>>) (Class.forName(
63+
return (Class<? extends ActorBuilder<? extends ActorBuilder<?, ?>, ? extends Actor>>) (Class.forName(
6364
clazz.getName() + "$Builder",
6465
true, // initialize
6566
clazz.getClassLoader()));

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package com.arpnetworking.clusteraggregator.configuration;
1717

18+
import akka.actor.ActorRef;
19+
import akka.actor.PoisonPill;
1820
import akka.actor.Props;
21+
import com.arpnetworking.akka.ActorBuilder;
1922
import com.arpnetworking.akka.NonJoiningClusterJoiner;
2023
import com.arpnetworking.commons.jackson.databind.ObjectMapperFactory;
2124
import com.arpnetworking.configuration.jackson.akka.ActorBuilderDeserializer;
@@ -34,19 +37,23 @@ public class ActorBuilderTest extends BaseActorTest {
3437
@Test
3538
public void testBuild() {
3639
final NonJoiningClusterJoiner.Builder builder = new NonJoiningClusterJoiner.Builder();
37-
builder.build();
40+
41+
final ActorRef ref = getSystem().actorOf(Props.create(builder));
42+
ref.tell(PoisonPill.getInstance(), ActorRef.noSender());
3843
}
3944

4045
@Test
4146
public void testPolyDeserialize() throws IOException {
4247
final ObjectMapper mapper = ObjectMapperFactory.createInstance();
4348
final SimpleModule module = new SimpleModule();
44-
module.addDeserializer(Props.class, new ActorBuilderDeserializer(mapper));
49+
module.addDeserializer(ActorBuilder.class, new ActorBuilderDeserializer(mapper));
4550
mapper.registerModule(module);
4651

4752
@Language("JSON") final String data = "{\n"
4853
+ " \"type\": \"com.arpnetworking.akka.NonJoiningClusterJoiner\"\n"
4954
+ "}";
50-
final Props props = mapper.readValue(data, Props.class);
55+
final ActorBuilder<?, ?> builder = mapper.readValue(data, ActorBuilder.class);
56+
final ActorRef ref = getSystem().actorOf(Props.create(builder));
57+
ref.tell(PoisonPill.getInstance(), ActorRef.noSender());
5158
}
5259
}

0 commit comments

Comments
 (0)