e9edd40e25bc049add0a4080860a251ee8e565a4
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.runtime.impl;
22
23 import java.io.ByteArrayInputStream;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.LinkedBlockingQueue;
31
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
34 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
38 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
39 import org.onap.policy.apex.model.basicmodel.service.ModelService;
40 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
42 import org.onap.policy.apex.service.engine.event.ApexEvent;
43 import org.onap.policy.apex.service.engine.event.ApexPeriodicEventGenerator;
44 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
45 import org.onap.policy.apex.service.engine.runtime.EngineService;
46 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
47 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
48 import org.onap.policy.common.parameters.GroupValidationResult;
49 import org.slf4j.ext.XLogger;
50 import org.slf4j.ext.XLoggerFactory;
51
52 /**
53  * The Class EngineServiceImpl controls a thread pool that runs a set of Apex engine workers, each of which is running
54  * on an identical Apex model. This class handles the management of the engine worker instances, their threads, and
55  * event forwarding to and from the engine workers.
56  *
57  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
58  * @author Liam Fallon (liam.fallon@ericsson.com)
59  * @author John Keeney (john.keeney@ericsson.com)
60  */
61 public final class EngineServiceImpl implements EngineService, EngineServiceEventInterface {
62     // Logging static variables
63     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngineServiceImpl.class);
64     private static final boolean DEBUG_ENABLED = LOGGER.isDebugEnabled();
65
66     // Recurring string constants
67     private static final String ENGINE_KEY_PREAMBLE = "engine with key ";
68     private static final String NOT_FOUND_SUFFIX = " not found in engine service";
69     private static final String ENGINE_KEY_NOT_SPECIFIED = "engine key must be specified and may not be null";
70
71     // Constants for timing
72     private static final long MAX_START_WAIT_TIME = 5000; // 5 seconds
73     private static final long MAX_STOP_WAIT_TIME = 5000; // 5 seconds
74     private static final int ENGINE_SERVICE_STOP_START_WAIT_INTERVAL = 200;
75
76     // The ID of this engine
77     private AxArtifactKey engineServiceKey = null;
78
79     // The Apex engine workers this engine service is handling
80     private final Map<AxArtifactKey, EngineService> engineWorkerMap = Collections
81                     .synchronizedMap(new LinkedHashMap<AxArtifactKey, EngineService>());
82
83     // Event queue for events being sent into the Apex engines, it used by all engines within a
84     // group.
85     private final BlockingQueue<ApexEvent> queue = new LinkedBlockingQueue<>();
86
87     // Thread factory for thread management
88     private final ApplicationThreadFactory atFactory = new ApplicationThreadFactory("apex-engine-service", 512);
89
90     // Periodic event generator and its period in milliseconds
91     private ApexPeriodicEventGenerator periodicEventGenerator = null;
92     private long periodicEventPeriod;
93
94     /**
95      * This constructor instantiates engine workers and adds them to the set of engine workers to be managed. The
96      * constructor is private to prevent subclassing.
97      *
98      * @param engineServiceKey the engine service key
99      * @param threadCount the thread count, the number of engine workers to start
100      * @param periodicEventPeriod the period in milliseconds at which periodic events are generated
101      * @throws ApexException on worker instantiation errors
102      */
103     private EngineServiceImpl(final AxArtifactKey engineServiceKey, final int threadCount,
104                     final long periodicEventPeriod) {
105         LOGGER.entry(engineServiceKey, threadCount);
106
107         this.engineServiceKey = engineServiceKey;
108         this.periodicEventPeriod = periodicEventPeriod;
109
110         // Start engine workers
111         for (int engineCounter = 0; engineCounter < threadCount; engineCounter++) {
112             final AxArtifactKey engineWorkerKey = new AxArtifactKey(engineServiceKey.getName() + '-' + engineCounter,
113                             engineServiceKey.getVersion());
114             engineWorkerMap.put(engineWorkerKey, new EngineWorker(engineWorkerKey, queue, atFactory));
115             LOGGER.info("Created apex engine {} .", engineWorkerKey.getId());
116         }
117
118         LOGGER.info("APEX service created.");
119         LOGGER.exit();
120     }
121
122     /**
123      * Create an Apex Engine Service instance. This method does not load the policy so
124      * {@link #updateModel(AxArtifactKey, AxPolicyModel, boolean)} or
125      * {@link #updateModel(AxArtifactKey, AxPolicyModel, boolean)} must be used to load a model. This method does not
126      * start the Engine Service so {@link #start(AxArtifactKey)} or {@link #startAll()} must be used.
127      *
128      * @param config the configuration for this Apex Engine Service.
129      * @return the Engine Service instance
130      * @throws ApexException on worker instantiation errors
131      */
132     public static EngineServiceImpl create(final EngineServiceParameters config) throws ApexException {
133         if (config == null) {
134             LOGGER.warn("Engine service configuration parameters is null");
135             throw new ApexException("engine service configuration parameters are null");
136         }
137         
138         final GroupValidationResult validation = config.validate();
139         if (!validation.isValid()) {
140             LOGGER.warn("Invalid engine service configuration parameters: {}" + validation.getResult());
141             throw new ApexException("Invalid engine service configuration parameters: " + validation);
142         }
143         
144         final AxArtifactKey engineServiceKey = config.getEngineKey();
145         final int threadCount = config.getInstanceCount();
146
147         return new EngineServiceImpl(engineServiceKey, threadCount, config.getPeriodicEventPeriod());
148     }
149
150     /**
151      * {@inheritDoc}.
152      */
153     @Override
154     public void registerActionListener(final String listenerName, final ApexEventListener apexEventListener) {
155         LOGGER.entry(apexEventListener);
156
157         if (listenerName == null) {
158             String message = "listener name must be specified and may not be null";
159             LOGGER.warn(message);
160             return;
161         }
162
163         if (apexEventListener == null) {
164             String message = "apex event listener must be specified and may not be null";
165             LOGGER.warn(message);
166             return;
167         }
168
169         // Register the Apex event listener on all engine workers, each worker will return Apex
170         // events to the listening application
171         for (final EngineService engineWorker : engineWorkerMap.values()) {
172             engineWorker.registerActionListener(listenerName, apexEventListener);
173         }
174
175         LOGGER.info("Added the action listener to the engine");
176         LOGGER.exit();
177     }
178
179     /**
180      * {@inheritDoc}.
181      */
182     @Override
183     public void deregisterActionListener(final String listenerName) {
184         LOGGER.entry(listenerName);
185
186         // Register the Apex event listener on all engine workers, each worker will return Apex
187         // events to the listening application
188         for (final EngineService engineWorker : engineWorkerMap.values()) {
189             engineWorker.deregisterActionListener(listenerName);
190         }
191
192         LOGGER.info("Removed the action listener from the engine");
193         LOGGER.exit();
194     }
195
196     /**
197      * {@inheritDoc}.
198      */
199     @Override
200     public EngineServiceEventInterface getEngineServiceEventInterface() {
201         return this;
202     }
203
204     /**
205      * {@inheritDoc}.
206      */
207     @Override
208     public AxArtifactKey getKey() {
209         return engineServiceKey;
210     }
211
212     /**
213      * {@inheritDoc}.
214      */
215     @Override
216     public Collection<AxArtifactKey> getEngineKeys() {
217         return engineWorkerMap.keySet();
218     }
219
220     /**
221      * {@inheritDoc}.
222      */
223     @Override
224     public AxArtifactKey getApexModelKey() {
225         if (engineWorkerMap.size() == 0) {
226             return null;
227         }
228
229         return engineWorkerMap.entrySet().iterator().next().getValue().getApexModelKey();
230     }
231
232     /**
233      * {@inheritDoc}.
234      */
235     @Override
236     public void updateModel(final AxArtifactKey incomingEngineServiceKey, final String apexModelString,
237                     final boolean forceFlag) throws ApexException {
238         // Check if the engine service key specified is sane
239         if (incomingEngineServiceKey == null) {
240             String message = ENGINE_KEY_NOT_SPECIFIED;
241             LOGGER.warn(message);
242             throw new ApexException(message);
243         }
244
245         // Check if the Apex model specified is sane
246         if (apexModelString == null || apexModelString.trim().length() == 0) {
247             String emptyModelMessage = "model for updating engine service with key "
248                             + incomingEngineServiceKey.getId() + " is empty";
249             LOGGER.warn(emptyModelMessage);
250             throw new ApexException(emptyModelMessage);
251         }
252
253         // Read the Apex model into memory using the Apex Model Reader
254         AxPolicyModel apexPolicyModel = null;
255         try {
256             final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
257             apexPolicyModel = modelReader.read(new ByteArrayInputStream(apexModelString.getBytes()));
258         } catch (final ApexModelException e) {
259             String message = "failed to unmarshal the apex model on engine service " + incomingEngineServiceKey.getId();
260             LOGGER.error(message, e);
261             throw new ApexException(message, e);
262         }
263
264         // Update the model
265         updateModel(incomingEngineServiceKey, apexPolicyModel, forceFlag);
266
267         LOGGER.exit();
268     }
269
270     /**
271      * {@inheritDoc}.
272      */
273     @Override
274     public void updateModel(final AxArtifactKey incomingEngineServiceKey, final AxPolicyModel apexModel,
275                     final boolean forceFlag) throws ApexException {
276         LOGGER.entry(incomingEngineServiceKey);
277
278         // Check if the engine service key specified is sane
279         if (incomingEngineServiceKey == null) {
280             String message = ENGINE_KEY_NOT_SPECIFIED;
281             LOGGER.warn(message);
282             throw new ApexException(message);
283         }
284
285         // Check if the Apex model specified is sane
286         if (apexModel == null) {
287             LOGGER.warn("model for updating on engine service with key " + incomingEngineServiceKey.getId()
288                             + " is null");
289             throw new ApexException("model for updating on engine service with key " + incomingEngineServiceKey.getId()
290                             + " is null");
291         }
292
293         // Check if the key on the update request is correct
294         if (!this.engineServiceKey.equals(incomingEngineServiceKey)) {
295             LOGGER.warn("engine service key " + incomingEngineServiceKey.getId() + " does not match the key"
296                             + engineServiceKey.getId() + " of this engine service");
297             throw new ApexException("engine service key " + incomingEngineServiceKey.getId() + " does not match the key"
298                             + engineServiceKey.getId() + " of this engine service");
299         }
300
301         // Check model compatibility
302         if (ModelService.existsModel(AxPolicyModel.class)) {
303             // The current policy model may or may not be defined
304             final AxPolicyModel currentModel = ModelService.getModel(AxPolicyModel.class);
305             if (!currentModel.getKey().isCompatible(apexModel.getKey())) {
306                 handleIncompatibility(apexModel, forceFlag, currentModel);
307             }
308         }
309
310         executeModelUpdate(incomingEngineServiceKey, apexModel, forceFlag);
311
312         LOGGER.exit();
313     }
314
315     /**
316      * Execute the model update on the engine instances.
317      * 
318      * @param incomingEngineServiceKey the engine service key to update
319      * @param apexModel the model to update the engines with
320      * @param forceFlag if true, ignore compatibility problems
321      * @throws ApexException on model update errors
322      */
323     private void executeModelUpdate(final AxArtifactKey incomingEngineServiceKey, final AxPolicyModel apexModel,
324                     final boolean forceFlag) throws ApexException {
325         
326         if (!isStopped()) {
327             stopEngines(incomingEngineServiceKey);
328         }
329
330         // Update the engines
331         for (final Entry<AxArtifactKey, EngineService> engineWorkerEntry : engineWorkerMap.entrySet()) {
332             LOGGER.info("Registering apex model on engine {}", engineWorkerEntry.getKey().getId());
333             engineWorkerEntry.getValue().updateModel(engineWorkerEntry.getKey(), apexModel, forceFlag);
334         }
335
336         // start all engines on this engine service if it was not stopped before the update
337         startAll();
338         final long starttime = System.currentTimeMillis();
339         while (!isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT_TIME) {
340             ThreadUtilities.sleep(ENGINE_SERVICE_STOP_START_WAIT_INTERVAL);
341         }
342         // Check if all engines are running
343         final StringBuilder notRunningEngineIdBuilder = new StringBuilder();
344         for (final Entry<AxArtifactKey, EngineService> engineWorkerEntry : engineWorkerMap.entrySet()) {
345             if (engineWorkerEntry.getValue().getState() != AxEngineState.READY
346                             && engineWorkerEntry.getValue().getState() != AxEngineState.EXECUTING) {
347                 notRunningEngineIdBuilder.append(engineWorkerEntry.getKey().getId());
348                 notRunningEngineIdBuilder.append('(');
349                 notRunningEngineIdBuilder.append(engineWorkerEntry.getValue().getState());
350                 notRunningEngineIdBuilder.append(") ");
351             }
352         }
353         if (notRunningEngineIdBuilder.length() > 0) {
354             final String errorString = "engine start error on model update on engine service with key "
355                             + incomingEngineServiceKey.getId() + ", engines not running are: "
356                             + notRunningEngineIdBuilder.toString().trim();
357             LOGGER.warn(errorString);
358             throw new ApexException(errorString);
359         }
360     }
361
362     /**
363      * Stop engines for a model update.
364      * @param incomingEngineServiceKey the engine service key for the engines that are to be stopped
365      * @throws ApexException on errors stopping engines
366      */
367     private void stopEngines(final AxArtifactKey incomingEngineServiceKey) throws ApexException {
368         // Stop all engines on this engine service
369         stop();
370         final long stoptime = System.currentTimeMillis();
371         while (!isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT_TIME) {
372             ThreadUtilities.sleep(ENGINE_SERVICE_STOP_START_WAIT_INTERVAL);
373         }
374         // Check if all engines are stopped
375         final StringBuilder notStoppedEngineIdBuilder = new StringBuilder();
376         for (final Entry<AxArtifactKey, EngineService> engineWorkerEntry : engineWorkerMap.entrySet()) {
377             if (engineWorkerEntry.getValue().getState() != AxEngineState.STOPPED) {
378                 notStoppedEngineIdBuilder.append(engineWorkerEntry.getKey().getId());
379                 notStoppedEngineIdBuilder.append('(');
380                 notStoppedEngineIdBuilder.append(engineWorkerEntry.getValue().getState());
381                 notStoppedEngineIdBuilder.append(") ");
382             }
383         }
384         if (notStoppedEngineIdBuilder.length() > 0) {
385             final String errorString = "cannot update model on engine service with key "
386                             + incomingEngineServiceKey.getId() + ", engines not stopped after " + MAX_STOP_WAIT_TIME
387                             + "ms are: " + notStoppedEngineIdBuilder.toString().trim();
388             LOGGER.warn(errorString);
389             throw new ApexException(errorString);
390         }
391     }
392
393     /**
394      * Issue compatibility warning or error message.
395      * @param apexModel The model name
396      * @param forceFlag true if we are forcing the update
397      * @param currentModel the existing model that is loaded
398      * @throws ContextException on compatibility errors
399      */
400     private void handleIncompatibility(final AxPolicyModel apexModel, final boolean forceFlag,
401                     final AxPolicyModel currentModel) throws ContextException {
402         if (forceFlag) {
403             LOGGER.warn("apex model update forced, supplied model with key \"" + apexModel.getKey().getId()
404                             + "\" is not a compatible model update from the existing engine model with key \""
405                             + currentModel.getKey().getId() + "\"");
406         } else {
407             throw new ContextException("apex model update failed, supplied model with key \""
408                             + apexModel.getKey().getId()
409                             + "\" is not a compatible model update from the existing engine model with key \""
410                             + currentModel.getKey().getId() + "\"");
411         }
412     }
413
414     /**
415      * {@inheritDoc}.
416      */
417     @Override
418     public AxEngineState getState() {
419         // If one worker is running then we are running, otherwise we are stopped
420         for (final EngineService engine : engineWorkerMap.values()) {
421             if (engine.getState() != AxEngineState.STOPPED) {
422                 return AxEngineState.EXECUTING;
423             }
424         }
425
426         return AxEngineState.STOPPED;
427     }
428
429     /**
430      * {@inheritDoc}.
431      */
432     @Override
433     public void startAll() throws ApexException {
434         for (final EngineService engine : engineWorkerMap.values()) {
435             start(engine.getKey());
436         }
437     }
438
439     /**
440      * {@inheritDoc}.
441      */
442     @Override
443     public void start(final AxArtifactKey engineKey) throws ApexException {
444         LOGGER.entry(engineKey);
445
446         if (engineKey == null) {
447             String message = ENGINE_KEY_NOT_SPECIFIED;
448             LOGGER.warn(message);
449             throw new ApexException(message);
450         }
451
452         // Check if we have this key on our map
453         if (!engineWorkerMap.containsKey(engineKey)) {
454             String message = ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX;
455             LOGGER.warn(message);
456             throw new ApexException(message);
457         }
458
459         // Start the engine
460         engineWorkerMap.get(engineKey).start(engineKey);
461         
462         // Check if periodic events should be turned on
463         if (periodicEventPeriod > 0) {
464             startPeriodicEvents(periodicEventPeriod);
465         }
466
467         LOGGER.exit(engineKey);
468     }
469
470     /**
471      * {@inheritDoc}.
472      */
473     @Override
474     public void stop() throws ApexException {
475         LOGGER.entry();
476
477         if (periodicEventGenerator != null) {
478             periodicEventGenerator.cancel();
479             periodicEventGenerator = null;
480         }
481         
482         // Stop each engine
483         for (final EngineService engine : engineWorkerMap.values()) {
484             if (engine.getState() != AxEngineState.STOPPED) {
485                 engine.stop();
486             }
487         }
488
489         LOGGER.exit();
490     }
491
492     /**
493      * {@inheritDoc}.
494      */
495     @Override
496     public void stop(final AxArtifactKey engineKey) throws ApexException {
497         LOGGER.entry(engineKey);
498
499         if (engineKey == null) {
500             String message = ENGINE_KEY_NOT_SPECIFIED;
501             LOGGER.warn(message);
502             throw new ApexException(message);
503         }
504
505         // Check if we have this key on our map
506         if (!engineWorkerMap.containsKey(engineKey)) {
507             LOGGER.warn(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
508             throw new ApexException(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
509         }
510
511         // Stop the engine
512         engineWorkerMap.get(engineKey).stop(engineKey);
513
514         LOGGER.exit(engineKey);
515     }
516
517     /**
518      * {@inheritDoc}.
519      */
520     @Override
521     public void clear() throws ApexException {
522         LOGGER.entry();
523
524         // Stop each engine
525         for (final EngineService engine : engineWorkerMap.values()) {
526             if (engine.getState() == AxEngineState.STOPPED) {
527                 engine.clear();
528             }
529         }
530
531         LOGGER.exit();
532     }
533
534     /**
535      * {@inheritDoc}.
536      */
537     @Override
538     public void clear(final AxArtifactKey engineKey) throws ApexException {
539         LOGGER.entry(engineKey);
540
541         if (engineKey == null) {
542             String message = ENGINE_KEY_NOT_SPECIFIED;
543             LOGGER.warn(message);
544             throw new ApexException(message);
545         }
546
547         // Check if we have this key on our map
548         if (!engineWorkerMap.containsKey(engineKey)) {
549             LOGGER.warn(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
550             throw new ApexException(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
551         }
552
553         // Clear the engine
554         if (engineWorkerMap.get(engineKey).getState() == AxEngineState.STOPPED) {
555             engineWorkerMap.get(engineKey).stop(engineKey);
556         }
557
558         LOGGER.exit(engineKey);
559     }
560
561     /**
562      * Check all engines are started.
563      *
564      * @return true if <i>all</i> engines are started
565      * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStarted()
566      */
567     @Override
568     public boolean isStarted() {
569         for (final EngineService engine : engineWorkerMap.values()) {
570             if (!engine.isStarted()) {
571                 return false;
572             }
573         }
574         return true;
575     }
576
577     /**
578      * {@inheritDoc}.
579      */
580     @Override
581     public boolean isStarted(final AxArtifactKey engineKey) {
582         if (engineKey == null) {
583             String message = ENGINE_KEY_NOT_SPECIFIED;
584             LOGGER.warn(message);
585             return false;
586         }
587
588         // Check if we have this key on our map
589         if (!engineWorkerMap.containsKey(engineKey)) {
590             LOGGER.warn(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
591             return false;
592         }
593         return engineWorkerMap.get(engineKey).isStarted();
594     }
595
596     /**
597      * Check all engines are stopped.
598      *
599      * @return true if <i>all</i> engines are stopped
600      * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStopped()
601      */
602     @Override
603     public boolean isStopped() {
604         for (final EngineService engine : engineWorkerMap.values()) {
605             if (!engine.isStopped()) {
606                 return false;
607             }
608         }
609         return true;
610     }
611
612     /**
613      * {@inheritDoc}.
614      */
615     @Override
616     public boolean isStopped(final AxArtifactKey engineKey) {
617         if (engineKey == null) {
618             String message = ENGINE_KEY_NOT_SPECIFIED;
619             LOGGER.warn(message);
620             return true;
621         }
622
623         // Check if we have this key on our map
624         if (!engineWorkerMap.containsKey(engineKey)) {
625             LOGGER.warn(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
626             return true;
627         }
628         return engineWorkerMap.get(engineKey).isStopped();
629     }
630
631     /**
632      * {@inheritDoc}.
633      */
634     @Override
635     public void startPeriodicEvents(final long period) throws ApexException {
636         // Check if periodic events are already started
637         if (periodicEventGenerator != null) {
638             String message = "Peiodic event geneation already running on engine " + engineServiceKey.getId() + ", "
639                             + periodicEventGenerator.toString();
640             LOGGER.warn(message);
641             throw new ApexException(message);
642         }
643
644         // Set up periodic event execution, its a Java Timer/TimerTask
645         periodicEventGenerator = new ApexPeriodicEventGenerator(this.getEngineServiceEventInterface(), period);
646
647         // Record the periodic event period because it may have been set over the Web Socket admin
648         // interface
649         this.periodicEventPeriod = period;
650     }
651
652     /**
653      * {@inheritDoc}.
654      */
655     @Override
656     public void stopPeriodicEvents() throws ApexException {
657         // Check if periodic events are already started
658         if (periodicEventGenerator == null) {
659             LOGGER.warn("Peiodic event geneation not running on engine " + engineServiceKey.getId());
660             throw new ApexException("Peiodic event geneation not running on engine " + engineServiceKey.getId());
661         }
662
663         // Stop periodic events
664         periodicEventGenerator.cancel();
665         periodicEventGenerator = null;
666         periodicEventPeriod = 0;
667     }
668
669     /**
670      * {@inheritDoc}.
671      */
672     @Override
673     public String getStatus(final AxArtifactKey engineKey) throws ApexException {
674         if (engineKey == null) {
675             String message = ENGINE_KEY_NOT_SPECIFIED;
676             LOGGER.warn(message);
677             throw new ApexException(message);
678         }
679
680         // Check if we have this key on our map
681         if (!engineWorkerMap.containsKey(engineKey)) {
682             LOGGER.warn(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
683             throw new ApexException(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
684         }
685
686         // Return the information for this worker
687         return engineWorkerMap.get(engineKey).getStatus(engineKey);
688     }
689
690     /**
691      * {@inheritDoc}.
692      */
693     @Override
694     public String getRuntimeInfo(final AxArtifactKey engineKey) throws ApexException {
695         if (engineKey == null) {
696             String message = ENGINE_KEY_NOT_SPECIFIED;
697             LOGGER.warn(message);
698             throw new ApexException(message);
699         }
700
701         // Check if we have this key on our map
702         if (!engineWorkerMap.containsKey(engineKey)) {
703             LOGGER.warn(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
704             throw new ApexException(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX);
705         }
706
707         // Return the information for this worker
708         return engineWorkerMap.get(engineKey).getRuntimeInfo(engineKey);
709     }
710
711     /**
712      * {@inheritDoc}.
713      */
714     @Override
715     public void sendEvent(final ApexEvent event) {
716         if (event == null) {
717             LOGGER.warn("Null events cannot be processed, in engine service " + engineServiceKey.getId());
718             return;
719         }
720
721         // Check if we have this key on our map
722         if (getState() == AxEngineState.STOPPED) {
723             LOGGER.warn("event " + event.getName() + " not processed, no engines on engine service "
724                             + engineServiceKey.getId() + " are running");
725             return;
726         }
727
728         if (DEBUG_ENABLED) {
729             LOGGER.debug("Forwarding Apex Event {} to the processing engine", event);
730         }
731
732         // Add the incoming event to the queue, the next available worker will process it
733         queue.add(event);
734     }
735 }