public void publish(Event event) {
        logger.debug("Dispatching event: " + event.getClass().toString());
        final ArrayList<Class<? extends Subscriber>> subscriberClasses = subscriptionMap.get(event.getClass());
        if (subscriberClasses == null) {
            logger.debug("No subscribers to inform from event: " + event.getClass().getName());
            return;
        }
        for (Class<? extends Subscriber> clazz: subscriberClasses) {
            logger.debug("Alerting subscriber " + clazz.getName());

            if (event instanceof ChainableEvent) {
                addTrackedEvent((ChainableEvent)event);
            }

            // Check to see if the Event is Unblocked. If so, use a separate executor pool from normal events
            final ExecutorService executorService = event instanceof UnblockedEvent  ? dynamicExecutor : executor;

            executorService.execute(() -> {
                try (AlpineQueryManager qm = new AlpineQueryManager()) {
                    final EventServiceLog eventServiceLog = qm.createEventServiceLog(clazz);
                    final Subscriber subscriber = clazz.getDeclaredConstructor().newInstance();
                    subscriber.inform(event);
                    qm.updateEventServiceLog(eventServiceLog);
                    if (event instanceof ChainableEvent) {
                        ChainableEvent chainableEvent = (ChainableEvent)event;
                        logger.debug("Calling onSuccess");
                        for (ChainLink chainLink: chainableEvent.onSuccess()) {
                            if (chainLink.getSuccessEventService() != null) {
                                Method method = chainLink.getSuccessEventService().getMethod("getInstance");
                                IEventService es = (IEventService) method.invoke(chainLink.getSuccessEventService(), new Object[0]);
                                es.publish(chainLink.getSuccessEvent());
                            } else {
                                Event.dispatch(chainLink.getSuccessEvent());
                            }
                        }
                    }
                } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException | SecurityException e) {
                    logger.error("An error occurred while informing subscriber: " + e);
                    if (event instanceof ChainableEvent) {
                        ChainableEvent chainableEvent = (ChainableEvent)event;
                        logger.debug("Calling onFailure");
                        for (ChainLink chainLink: chainableEvent.onFailure()) {
                            if (chainLink.getFailureEventService() != null) {
                                try {
                                    Method method = chainLink.getFailureEventService().getMethod("getInstance");
                                    IEventService es = (IEventService) method.invoke(chainLink.getFailureEventService(), new Object[0]);
                                    es.publish(chainLink.getFailureEvent());
                                } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
                                    logger.error("Exception while calling onFailure callback", ex);
                                }
                            } else {
                                Event.dispatch(chainLink.getFailureEvent());
                            }
                        }
                    }
                }

                if (event instanceof ChainableEvent) {
                    removeTrackedEvent((ChainableEvent)event);
                }

            });
        }
    }
public void publish ( Event event ) { logger . debug ( "Dispatching event: " + event . getClass ( ) . toString ( ) ) ; final ArrayList < Class < ? extends Subscriber > > subscriberClasses = subscriptionMap . get ( event . getClass ( ) ) ; if ( subscriberClasses == null ) { logger . debug ( "No subscribers to inform from event: " + event . getClass ( ) . getName ( ) ) ; return ; } for ( Class < ? extends Subscriber > clazz : subscriberClasses ) { logger . debug ( "Alerting subscriber " + clazz . getName ( ) ) ; if ( event instanceof ChainableEvent ) { addTrackedEvent ( ( ChainableEvent ) event ) ; } final ExecutorService executorService = event instanceof UnblockedEvent ? dynamicExecutor : executor ; executorService . execute ( ( ) -> { try ( AlpineQueryManager qm = new AlpineQueryManager ( ) ) { final EventServiceLog eventServiceLog = qm . createEventServiceLog ( clazz ) ; final Subscriber subscriber = clazz . getDeclaredConstructor ( ) . newInstance ( ) ; subscriber . inform ( event ) ; qm . updateEventServiceLog ( eventServiceLog ) ; if ( event instanceof ChainableEvent ) { ChainableEvent chainableEvent = ( ChainableEvent ) event ; logger . debug ( "Calling onSuccess" ) ; for ( ChainLink chainLink : chainableEvent . onSuccess ( ) ) { if ( chainLink . getSuccessEventService ( ) != null ) { Method method = chainLink . getSuccessEventService ( ) . getMethod ( "getInstance" ) ; IEventService es = ( IEventService ) method . invoke ( chainLink . getSuccessEventService ( ) , new Object [ 0 ] ) ; es . publish ( chainLink . getSuccessEvent ( ) ) ; } else { Event . dispatch ( chainLink . getSuccessEvent ( ) ) ; } } } } catch ( NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException | SecurityException e ) { logger . error ( "An error occurred while informing subscriber: " + e ) ; if ( event instanceof ChainableEvent ) { ChainableEvent chainableEvent = ( ChainableEvent ) event ; logger . debug ( "Calling onFailure" ) ; for ( ChainLink chainLink : chainableEvent . onFailure ( ) ) { if ( chainLink . getFailureEventService ( ) != null ) { try { Method method = chainLink . getFailureEventService ( ) . getMethod ( "getInstance" ) ; IEventService es = ( IEventService ) method . invoke ( chainLink . getFailureEventService ( ) , new Object [ 0 ] ) ; es . publish ( chainLink . getFailureEvent ( ) ) ; } catch ( NoSuchMethodException | InvocationTargetException | IllegalAccessException ex ) { logger . error ( "Exception while calling onFailure callback" , ex ) ; } } else { Event . dispatch ( chainLink . getFailureEvent ( ) ) ; } } } } if ( event instanceof ChainableEvent ) { removeTrackedEvent ( ( ChainableEvent ) event ) ; } } ) ; } }
{@inheritDoc}
@since 1.0.0