public void start() throws MuleException { // TODO If Service is uninitialised when start is called should we initialise // or throw an exception? if (!initialised.get()) { throw new IllegalStateException("Cannot start an unitialised service."); } if (isStarted()) { logger.info("Service is already started: " + name); } else { if (initialState.equals(AbstractService.INITIAL_STATE_STOPPED)) { logger.info("stopped"); } if (!beyondInitialState.get() && initialState.equals(AbstractService.INITIAL_STATE_STOPPED)) { logger.info("Service " + name + " has not been started (initial state = 'stopped')"); } else if (!beyondInitialState.get() && initialState.equals(AbstractService.INITIAL_STATE_PAUSED)) { start(/*startPaused*/ true); logger.info("Service " + name + " has been started and paused (initial state = 'paused')"); } else { start(/*startPaused*/ false); logger.info("Service " + name + " has been started successfully"); } beyondInitialState.set(true); } }
public void stop() throws MuleException { if (!stopped.get()) { logger.debug("Stopping Service"); stopping.set(true); fireServiceNotification(ServiceNotification.SERVICE_STOPPING); // Unregister Listeners for the service unregisterListeners(); // Resume if paused. (This is required so that stop() doesn't hand and so // message aren't lost in SedaQueue when service is stopped. if (isPaused()) { resume(); } doStop(); // Stop component. We do this here in case there are any queues that need to be consumed // first. component.stop(); stopped.set(true); fireServiceNotification(ServiceNotification.SERVICE_STOPPED); logger.info("Mule Service " + name + " has been stopped successfully"); } }
public void forceStop() throws MuleException { if (!stopped.get()) { logger.debug("Stopping Service"); stopping.set(true); fireServiceNotification(ServiceNotification.SERVICE_STOPPING); doForceStop(); stopped.set(true); stopping.set(false); fireServiceNotification(ServiceNotification.SERVICE_STOPPED); } }
/** * Initialise the service. The service will first create a Mule UMO from the UMODescriptor and * then initialise a pool based on the attributes in the UMODescriptor. * * @throws org.mule.api.lifecycle.InitialisationException if the service fails to initialise */ public final synchronized void initialise() throws InitialisationException { if (initialised.get()) { throw new InitialisationException( CoreMessages.objectAlreadyInitialised("Service '" + name + "'"), this); } // Ensure Component has service instance and is initialised. If the component // was configured with spring and is therefore in the registry it will get // started automatically, if it was set on the service directly then it won't // be started automatically. So to be sure we start it here. component.setService(this); component.initialise(); if (inboundRouter == null) { // Create Default routes that route to the default inbound and // outbound endpoints inboundRouter = new DefaultInboundRouterCollection(); // TODO MULE-2102 This should be configured in the default template. inboundRouter.addRouter(new InboundPassThroughRouter()); } if (outboundRouter == null) { outboundRouter = new DefaultOutboundRouterCollection(); // TODO MULE-2102 This should be configured in the default template. outboundRouter.addRouter(new OutboundPassThroughRouter()); } if (responseRouter == null) { responseRouter = new DefaultResponseRouterCollection(); } if (exceptionListener == null) { // By default us the model Exception Listener exceptionListener = getModel().getExceptionListener(); // // TODO MULE-2102 This should be configured in the default template. // exceptionListener = new DefaultServiceExceptionStrategy(this); // ((MuleContextAware) exceptionListener).setMuleContext(muleContext); // ((Initialisable) exceptionListener).initialise(); } doInitialise(); // initialise statistics stats = createStatistics(); stats.setEnabled(muleContext.getStatistics().isEnabled()); muleContext.getStatistics().add(stats); stats.setOutboundRouterStat(outboundRouter.getStatistics()); stats.setInboundRouterStat(inboundRouter.getStatistics()); stats.setComponentStat(component.getStatistics()); initialised.set(true); fireServiceNotification(ServiceNotification.SERVICE_INITIALISED); }
public final void dispose() { try { if (!stopped.get()) { stop(); } } catch (MuleException e) { // TODO MULE-863: If this is an error, do something! logger.error("Failed to stop service: " + name, e); } doDispose(); component.dispose(); initialised.set(false); fireServiceNotification(ServiceNotification.SERVICE_DISPOSED); muleContext.getStatistics().remove(stats); }
public final synchronized void connect() throws Exception { // This method may be called to ensure transport is connected, if it is // already connected then just return. if (connected.get()) { return; } if (disposed.get()) { throw new IllegalStateException( "Requester/dispatcher has been disposed; cannot connect to resource"); } retryTemplate.execute( new RetryCallback() { public void doWork(RetryContext context) throws Exception { try { doConnect(); connected.set(true); if (startOnConnect) { start(); } } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug(e); } throw e; } } public String getWorkDescription() { return getConnectionDescription(); } }, getWorkManager()); }
public void dispatchEvent(MuleEvent event) throws MuleException { if (stopping.get() || stopped.get()) { throw new ServiceException(CoreMessages.componentIsStopped(name), event.getMessage(), this); } // Dispatching event to an inbound endpoint // in the DefaultMuleSession#dispatchEvent ImmutableEndpoint endpoint = event.getEndpoint(); if (endpoint instanceof OutboundEndpoint) { try { ((OutboundEndpoint) endpoint).dispatch(event); } catch (Exception e) { throw new DispatchException(event.getMessage(), event.getEndpoint(), e); } return; } if (logger.isDebugEnabled()) { logger.debug( "Service: " + name + " has received asynchronous event on: " + event.getEndpoint().getEndpointURI()); } // Dispatching event to the service if (stats.isEnabled()) { stats.incReceivedEventASync(); } doDispatch(event); }
public void stop() throws MuleException { if (!stopped.get()) { logger.debug("Stopping Service"); stopping.set(true); fireServiceNotification(ServiceNotification.SERVICE_STOPPING); // Unregister Listeners for the service unregisterListeners(); component.stop(); doStop(); stopped.set(true); fireServiceNotification(ServiceNotification.SERVICE_STOPPED); logger.info("Mule Service " + name + " has been stopped successfully"); } }
public void start() throws MuleException { lifecycleManager.checkPhase(Startable.PHASE_NAME); if (!beyondInitialState.get() && initialState.equals(AbstractService.INITIAL_STATE_STOPPED)) { logger.info("Service " + name + " has not been started (initial state = 'stopped')"); beyondInitialState.set(true); return; } // Ensure Component is started. If component was configured with spring and // is therefore in the registry it will get started automatically, if it was // set on the service directly then it won't be started automatically. So to // be sure we start it here. component.start(); // Create the receivers for the service but do not start them yet. registerListeners(); // We connect the receivers _before_ starting the service because there may // be // some initialization required for the service which needs to have them // connected. // For example, the org.mule.transport.soap.glue.GlueMessageReceiver adds // InitialisationCallbacks within its doConnect() method (see MULE-804). connectListeners(); if (!beyondInitialState.get() && initialState.equals(AbstractService.INITIAL_STATE_PAUSED)) { lifecycleManager.fireLifecycle(Startable.PHASE_NAME); lifecycleManager.fireLifecycle(Pausable.PHASE_NAME); logger.info("Service " + name + " has been started and paused (initial state = 'paused')"); } else { lifecycleManager.fireLifecycle(Startable.PHASE_NAME); logger.info("Service " + name + " has been started successfully"); } beyondInitialState.set(true); // We start the receivers _after_ starting the service because if a message // gets routed to the service before it is started, // org.mule.model.AbstractComponent.dispatchEvent() will throw a // ServiceException with message COMPONENT_X_IS_STOPPED (see MULE-526). startListeners(); }
/** Template method to destroy any resources held by the Message Dispatcher */ public synchronized void dispose() { if (!disposed.get()) { try { this.disconnect(); this.stop(); } catch (Exception e) { // TODO MULE-863: What should we really do? logger.warn(e.getMessage(), e); } } }
/** * Determines if the expression is valid or not. This method will validate a single expression or * expressions embedded in a string. the expression must be well formed i.e. #[bean:user] * * @param expression the expression to validate * @return true if the expression evaluator is recognised */ public boolean isValidExpression(String expression) { final AtomicBoolean valid = new AtomicBoolean(true); final AtomicBoolean match = new AtomicBoolean(false); final StringBuffer message = new StringBuffer(); parser.parse( new TemplateParser.TemplateCallback() { public Object match(String token) { match.set(true); if (token.indexOf(":") == -1) { if (valid.get()) { valid.compareAndSet(true, false); } message.append(token).append(" is malformed\n"); } return null; } }, expression); if (message.length() > 0) { logger.warn("Expression " + expression + " is malformed: " + message.toString()); } return match.get() && valid.get(); }
/** * Starts a Mule Service. * * @param startPaused - Start service in a "paused" state (messages are received but not * processed). */ protected void start(boolean startPaused) throws MuleException { // Ensure Component is started. If component was configured with spring and // is therefore in the registry it will get started automatically, if it was // set on the service directly then it won't be started automatically. So to // be sure we start it here. component.start(); // Create the receivers for the service but do not start them yet. registerListeners(); // We connect the receivers _before_ starting the service because there may // be // some initialization required for the service which needs to have them // connected. // For example, the org.mule.transport.soap.glue.GlueMessageReceiver adds // InitialisationCallbacks within its doConnect() method (see MULE-804). connectListeners(); // Start (and pause) the service. if (stopped.get()) { stopped.set(false); paused.set(false); doStart(); } fireServiceNotification(ServiceNotification.SERVICE_STARTED); if (startPaused) { pause(); } // We start the receivers _after_ starting the service because if a message // gets routed to the service before it is started, // org.mule.model.AbstractComponent.dispatchEvent() will throw a // ServiceException with message COMPONENT_X_IS_STOPPED (see MULE-526). startListeners(); }
public MuleMessage sendEvent(MuleEvent event) throws MuleException { if (stopping.get() || stopped.get()) { throw new ServiceException(CoreMessages.componentIsStopped(name), event.getMessage(), this); } try { waitIfPaused(event); } catch (InterruptedException e) { throw new ServiceException(event.getMessage(), this, e); } if (stats.isEnabled()) { stats.incReceivedEventSync(); } if (logger.isDebugEnabled()) { logger.debug( "Service: " + name + " has received synchronous event on: " + event.getEndpoint().getEndpointURI()); } event = OptimizedRequestContext.unsafeSetEvent(event); return doSend(event); }
public boolean isStarted() { return !stopped.get(); }
public boolean isStopped() { return stopped.get(); }
public boolean validate() { // by default a dispatcher/requester can be used unless disposed return !disposed.get(); }