Skip to content

Commit 3cc4086

Browse files
committed
PR feedback, move the actor builder closer to desired
1 parent d63735a commit 3cc4086

File tree

6 files changed

+56
-56
lines changed

6 files changed

+56
-56
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@
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;
21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2022

2123
import java.util.function.Function;
2224

2325
/**
2426
* Builder for actors.
2527
*
2628
* @param <B> The type of the builder
29+
* @param <S> type of the object to be built
2730
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
2831
*/
29-
public abstract class ActorBuilder<B extends ActorBuilder<B>> extends OvalBuilder<Props> {
32+
@SuppressFBWarnings("SE_NO_SUITABLE_CONSTRUCTOR")
33+
public abstract class ActorBuilder<B extends ActorBuilder<B, S>, S extends Actor> extends OvalBuilder<S> implements Creator<S> {
3034
/**
31-
* Protected constructor.
35+
* Protected constructor for subclasses.
3236
*
33-
* @param createProps method to create a {@link Props} from the {@link ActorBuilder}
37+
* @param targetConstructor The constructor for the concrete type to be created by this builder.
3438
*/
35-
protected ActorBuilder(final Function<B, Props> createProps) {
36-
super(createProps);
39+
protected ActorBuilder(final Function<B, S> targetConstructor) {
40+
super(targetConstructor);
3741
}
3842

3943
/**
@@ -43,4 +47,14 @@ protected ActorBuilder(final Function<B, Props> createProps) {
4347
* @return instance with correct {@link ActorBuilder} class type.
4448
*/
4549
protected abstract B self();
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
@Override
55+
public S create() throws Exception {
56+
return build();
57+
}
58+
59+
private static final long serialVersionUID = 1L;
4660
}

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

Lines changed: 9 additions & 32 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;
@@ -27,56 +26,32 @@
2726
*/
2827
public final class NonJoiningClusterJoiner extends UntypedActor {
2928
/**
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}
29+
* {@inheritDoc}
4430
*/
45-
private static Props props(final Builder builder) {
46-
return props();
31+
@Override
32+
public void onReceive(final Object message) throws Exception {
33+
unhandled(message);
4734
}
4835

49-
/**
50-
* Public constructor.
51-
*/
52-
public NonJoiningClusterJoiner() {
36+
private NonJoiningClusterJoiner(final Builder builder) {
5337
LOGGER.info()
5438
.setMessage("NonJoiningClusterJoiner starting up")
5539
.log();
5640
}
5741

58-
59-
/**
60-
* {@inheritDoc}
61-
*/
62-
@Override
63-
public void onReceive(final Object message) throws Exception {
64-
unhandled(message);
65-
}
66-
6742
private static final Logger LOGGER = LoggerFactory.getLogger(NonJoiningClusterJoiner.class);
6843

6944
/**
7045
* Implementation of the {@link com.arpnetworking.commons.builder.Builder} pattern for a {@link NonJoiningClusterJoiner}.
7146
*
7247
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
7348
*/
74-
public static class Builder extends ActorBuilder<Builder> {
49+
public static class Builder extends ActorBuilder<Builder, NonJoiningClusterJoiner> {
7550
/**
7651
* Public constructor.
7752
*/
7853
public Builder() {
79-
super(NonJoiningClusterJoiner::props);
54+
super(NonJoiningClusterJoiner::new);
8055
}
8156

8257
/**
@@ -86,5 +61,7 @@ public Builder() {
8661
public Builder self() {
8762
return this;
8863
}
64+
65+
private static final long serialVersionUID = 1L;
8966
}
9067
}

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)