/** * Will dispatch an application event through Mule * * @param applicationEvent the Spring event to be dispatched * @throws ApplicationEventException if the event cannot be dispatched i.e. if the underlying * transport throws an exception */ protected void dispatchEvent(MuleApplicationEvent applicationEvent) throws ApplicationEventException { UMOEndpoint endpoint = null; try { endpoint = MuleEndpoint.getOrCreateEndpointForUri( applicationEvent.getEndpoint(), UMOEndpoint.ENDPOINT_TYPE_SENDER); } catch (UMOException e) { throw new ApplicationEventException( "Failed to get endpoint for endpointUri: " + applicationEvent.getEndpoint(), e); } if (endpoint != null) { try { // if (applicationEvent.getEndpoint() != null) { // endpoint.setEndpointURI(applicationEvent.getEndpoint()); // } MuleMessage message = new MuleMessage(applicationEvent.getSource(), applicationEvent.getProperties()); // has dispatch been triggered using beanFactory.publish() // without a current event if (applicationEvent.getMuleEventContext() != null) { // tell mule not to try and route this event itself applicationEvent.getMuleEventContext().setStopFurtherProcessing(true); applicationEvent.getMuleEventContext().dispatchEvent(message, endpoint); } else { UMOSession session = new MuleSession( message, ((AbstractConnector) endpoint.getConnector()).getSessionHandler(), component); RequestContext.setEvent(new MuleEvent(message, endpoint, session, false)); // transform if necessary if (endpoint.getTransformer() != null) { message = new MuleMessage( endpoint.getTransformer().transform(applicationEvent.getSource()), applicationEvent.getProperties()); } endpoint.dispatch(new MuleEvent(message, endpoint, session, false)); } } catch (Exception e1) { throw new ApplicationEventException("Failed to dispatch event: " + e1.getMessage(), e1); } } else { throw new ApplicationEventException( "Failed endpoint using name: " + applicationEvent.getEndpoint()); } }
/** * Method is used to dispatch events to listeners registered with the EventManager or dispatches * events to Mule depending on the type and state of the event received. If the event is not a * Mule event it will be dispatched to any listeners registered that are NOT MuleEventListeners. * If the event is a Mule event and there is no source event attached to it, it is assumed that * the event was dispatched by an object in the context using context.publishEvent() and will be * dispatched by Mule. If the event does have a source event attached to it, it is assumed that * the event was dispatched by Mule and will be delivered to any listeners subscribed to the * event. * * @param e application event received by the context */ public void multicastEvent(ApplicationEvent e) { MuleApplicationEvent muleEvent = null; // if the context gets refreshed we need to reinitialise if (e instanceof ContextRefreshedEvent) { // If the manager is being initialised from another context // don't try and initialise Mule if (MuleManager.isInstanciated() && !MuleManager.getInstance().isInitialised()) { try { registerMulticasterDescriptor(); } catch (UMOException ex) { throw new MuleRuntimeException(SpringMessages.failedToReinitMule(), ex); } } else { initMule(); } } else if (e instanceof ContextClosedEvent) { MuleManager.getInstance().dispose(); return; } else if (e instanceof MuleApplicationEvent) { muleEvent = (MuleApplicationEvent) e; // If there is no Mule event the event didn't originate from Mule // so its an outbound event if (muleEvent.getMuleEventContext() == null) { try { dispatchEvent(muleEvent); } catch (ApplicationEventException e1) { exceptionListener.exceptionThrown(e1); } return; } } for (Iterator iterator = listeners.iterator(); iterator.hasNext(); ) { ApplicationListener listener = (ApplicationListener) iterator.next(); if (muleEvent != null) { // As the asynchronous listener wraps the real listener we need to // check the // type of the wrapped listener, but invoke the Async listener if (listener instanceof AsynchronousEventListener) { AsynchronousEventListener asyncListener = (AsynchronousEventListener) listener; if (asyncListener.getListener() instanceof MuleSubscriptionEventListener) { if (isSubscriptionMatch( muleEvent.getEndpoint(), ((MuleSubscriptionEventListener) asyncListener.getListener()).getSubscriptions())) { asyncListener.onApplicationEvent(muleEvent); } } else if (asyncListener.getListener() instanceof MuleEventListener) { asyncListener.onApplicationEvent(muleEvent); } else if (!(asyncListener.getListener() instanceof MuleEventListener)) { asyncListener.onApplicationEvent(e); } // Synchronous Event listener Checks } else if (listener instanceof MuleSubscriptionEventListener) { if (isSubscriptionMatch( muleEvent.getEndpoint(), ((MuleSubscriptionEventListener) listener).getSubscriptions())) { listener.onApplicationEvent(muleEvent); } } else if (listener instanceof MuleEventListener) { listener.onApplicationEvent(muleEvent); } } else if (listener instanceof AsynchronousEventListener && !(((AsynchronousEventListener) listener).getListener() instanceof MuleEventListener)) { listener.onApplicationEvent(e); } else if (!(listener instanceof MuleEventListener)) { listener.onApplicationEvent(e); } else { // Finally only propagate the Application event if the // ApplicationEvent interface is explicitly implemented for (int i = 0; i < listener.getClass().getInterfaces().length; i++) { if (listener.getClass().getInterfaces()[i].equals(ApplicationListener.class)) { listener.onApplicationEvent(e); break; } } } } }