Package: ActorActivator
ActorActivator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ActorActivator(Class, int) |
|
|
|
|
|
||||||||||||||||||||
activatorFor(Class) |
|
|
|
|
|
||||||||||||||||||||
dispose() |
|
|
|
|
|
||||||||||||||||||||
initialize() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
validate(Actor) |
|
|
|
|
|
||||||||||||||||||||
withPoolSize(int) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.actor.spi;
28:
29: import java.lang.reflect.InvocationTargetException;
30: import javax.annotation.Nonnegative;
31: import javax.annotation.Nonnull;
32: import it.tidalwave.actor.annotation.Actor;
33: import it.tidalwave.actor.impl.CollaborationAwareMessageBusAdapter;
34: import it.tidalwave.actor.impl.ExecutorWithPriority;
35: import it.tidalwave.actor.impl.MBeansManager;
36: import it.tidalwave.actor.impl.PostConstructInvoker;
37: import it.tidalwave.actor.impl.PreDestroyInvoker;
38: import lombok.Getter;
39: import lombok.extern.slf4j.Slf4j;
40: import static it.tidalwave.messagebus.spi.ReflectionUtils.*;
41:
42: /***********************************************************************************************************************
43: *
44: * This class is used to activate and deactivate an actor.
45: *
46: * @author Fabrizio Giudici
47: *
48: **********************************************************************************************************************/
49: @Slf4j
50: public class ActorActivator
51: {
52: @Nonnull
53: private final Class<?> actorClass;
54:
55: @Nonnegative @Getter
56: private final int poolSize;
57:
58: @Getter
59: private Object actorObject;
60:
61: private ExecutorWithPriority executor;
62:
63: private CollaborationAwareMessageBusAdapter messageBusAdapter;
64:
65: private MBeansManager mBeansManager;
66:
67: /*******************************************************************************************************************
68: *
69: * Creates an instance for the given actor class.
70: *
71: * @param actorClass the actor class
72: * @return the instance
73: *
74: ******************************************************************************************************************/
75: public static ActorActivator activatorFor (@Nonnull final Class<?> actorClass)
76: {
77: return new ActorActivator(actorClass, 1);
78: }
79:
80: /*******************************************************************************************************************
81: *
82: * Specifies the pool size for this activator.
83: *
84: * @param poolSize the pool size
85: * @return the activator
86: *
87: ******************************************************************************************************************/
88: @Nonnull
89: public ActorActivator withPoolSize (@Nonnegative final int poolSize)
90: {
91: return new ActorActivator(actorClass, poolSize);
92: }
93:
94: /*******************************************************************************************************************
95: *
96: *
97: *
98: ******************************************************************************************************************/
99: private ActorActivator (@Nonnull final Class<?> actorClass, @Nonnegative final int poolSize)
100: {
101: this.actorClass = actorClass;
102: this.poolSize = poolSize;
103: }
104:
105: /*******************************************************************************************************************
106: *
107: * Activates the managed actor.
108: *
109: ******************************************************************************************************************/
110: public void initialize()
111: {
112: try
113: {
114: final var actor = actorClass.getAnnotation(Actor.class);
115: validate(actor);
116: actorObject = actorClass.getDeclaredConstructor().newInstance();
117: executor = new ExecutorWithPriority(poolSize, actorClass.getSimpleName(), actor.initialPriority());
118: mBeansManager = new MBeansManager(actorObject, poolSize);
119: messageBusAdapter = new CollaborationAwareMessageBusAdapter(actorObject, executor, mBeansManager.getStats());
120: }
121: catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e)
122: {
123: throw new RuntimeException(e);
124: }
125:
126: forEachMethodInTopDownHierarchy(actorObject, messageBusAdapter);
127: forEachMethodInTopDownHierarchy(actorObject, new PostConstructInvoker(actorObject));
128: mBeansManager.register();
129: }
130:
131: /*******************************************************************************************************************
132: *
133: * Deactivates the managed actor and releases resources.
134: *
135: ******************************************************************************************************************/
136: public void dispose()
137: {
138: mBeansManager.unregister();
139: forEachMethodInBottomUpHierarchy(actorObject, new PreDestroyInvoker(actorObject));
140: messageBusAdapter.unsubscribe();
141: }
142:
143: /*******************************************************************************************************************
144: *
145: *
146: *
147: ******************************************************************************************************************/
148: private void validate (@Nonnull final Actor actor)
149: {
150: //noinspection ConstantConditions
151:• if (actor == null)
152: {
153: throw new IllegalArgumentException("Actor class must be annotated with @Actor: " + actorClass);
154: }
155:
156:• if (!actor.threadSafe() && (poolSize != 1))
157: {
158: throw new IllegalArgumentException("Actors that aren't thread safe can't have pool size > 1");
159: }
160: }
161: }