Search in sources :

Example 31 with Service

use of org.apache.camel.Service in project camel by apache.

the class DefaultCamelContext method doAddService.

private void doAddService(Object object, boolean stopOnShutdown, boolean forceStart) throws Exception {
    // inject CamelContext
    if (object instanceof CamelContextAware) {
        CamelContextAware aware = (CamelContextAware) object;
        aware.setCamelContext(this);
    }
    if (object instanceof Service) {
        Service service = (Service) object;
        for (LifecycleStrategy strategy : lifecycleStrategies) {
            if (service instanceof Endpoint) {
                // use specialized endpoint add
                strategy.onEndpointAdd((Endpoint) service);
            } else {
                strategy.onServiceAdd(this, service, null);
            }
        }
        if (!forceStart) {
            // now start the service (and defer starting if CamelContext is starting up itself)
            deferStartService(object, stopOnShutdown);
        } else {
            // only add to services to close if its a singleton
            // otherwise we could for example end up with a lot of prototype scope endpoints
            // assume singleton by default
            boolean singleton = true;
            if (object instanceof IsSingleton) {
                singleton = ((IsSingleton) service).isSingleton();
            }
            // do not add endpoints as they have their own list
            if (singleton && !(service instanceof Endpoint)) {
                // only add to list of services to stop if its not already there
                if (stopOnShutdown && !hasService(service)) {
                    servicesToStop.add(service);
                }
            }
            ServiceHelper.startService(service);
        }
    }
}
Also used : CamelContextAware(org.apache.camel.CamelContextAware) IsSingleton(org.apache.camel.IsSingleton) Endpoint(org.apache.camel.Endpoint) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service) StatefulService(org.apache.camel.StatefulService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SuspendableService(org.apache.camel.SuspendableService)

Example 32 with Service

use of org.apache.camel.Service in project camel by apache.

the class CamelJaxbNoNamespaceSchemaLocationSpringTest method createCamelContext.

protected CamelContext createCamelContext() throws Exception {
    setUseRouteBuilder(false);
    final AbstractXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("org/apache/camel/jaxb/CamelJaxbNoNamespaceSchemaLocationTest.xml");
    setCamelContextService(new Service() {

        public void start() throws Exception {
            applicationContext.start();
        }

        public void stop() throws Exception {
            applicationContext.stop();
        }
    });
    return SpringCamelContext.springCamelContext(applicationContext);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Service(org.apache.camel.Service) AbstractXmlApplicationContext(org.springframework.context.support.AbstractXmlApplicationContext)

Aggregations

Service (org.apache.camel.Service)32 SuspendableService (org.apache.camel.SuspendableService)8 LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)8 AbstractXmlApplicationContext (org.springframework.context.support.AbstractXmlApplicationContext)8 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 StatefulService (org.apache.camel.StatefulService)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 ArrayList (java.util.ArrayList)5 Processor (org.apache.camel.Processor)5 Route (org.apache.camel.Route)5 IOException (java.io.IOException)4 Endpoint (org.apache.camel.Endpoint)4 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 CamelContextAware (org.apache.camel.CamelContextAware)3 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)3 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)3 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)3 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 VetoCamelContextStartException (org.apache.camel.VetoCamelContextStartException)3