Search in sources :

Example 1 with ProcessorDefinition

use of org.apache.camel.model.ProcessorDefinition in project camel by apache.

the class AdviceWithTasks method doRemove.

private static AdviceWithTask doRemove(final RouteDefinition route, final MatchBy matchBy, boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) {
    return new AdviceWithTask() {

        public void task() throws Exception {
            boolean match = false;
            Iterator<ProcessorDefinition<?>> it = AdviceWithTasks.createMatchByIterator(route, matchBy, selectFirst, selectLast, selectFrom, selectTo, maxDeep);
            while (it.hasNext()) {
                ProcessorDefinition<?> output = it.next();
                if (matchBy.match(output)) {
                    List<ProcessorDefinition<?>> outputs = getOutputs(output);
                    if (outputs != null) {
                        int index = outputs.indexOf(output);
                        if (index != -1) {
                            match = true;
                            Object old = outputs.remove(index);
                            LOG.info("AdviceWith (" + matchBy.getId() + ") : [" + old + "] --> remove");
                        }
                    }
                }
            }
            if (!match) {
                throw new IllegalArgumentException("There are no outputs which matches: " + matchBy.getId() + " in the route: " + route);
            }
        }
    };
}
Also used : ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) Endpoint(org.apache.camel.Endpoint)

Example 2 with ProcessorDefinition

use of org.apache.camel.model.ProcessorDefinition in project camel by apache.

the class AdviceWithTasks method doBefore.

private static AdviceWithTask doBefore(final RouteDefinition route, final MatchBy matchBy, final ProcessorDefinition<?> before, boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) {
    return new AdviceWithTask() {

        public void task() throws Exception {
            boolean match = false;
            Iterator<ProcessorDefinition<?>> it = AdviceWithTasks.createMatchByIterator(route, matchBy, selectFirst, selectLast, selectFrom, selectTo, maxDeep);
            while (it.hasNext()) {
                ProcessorDefinition<?> output = it.next();
                if (matchBy.match(output)) {
                    List<ProcessorDefinition<?>> outputs = getOutputs(output);
                    if (outputs != null) {
                        int index = outputs.indexOf(output);
                        if (index != -1) {
                            match = true;
                            Object existing = outputs.get(index);
                            outputs.add(index, before);
                            // must set parent on the node we added in the route
                            before.setParent(output.getParent());
                            LOG.info("AdviceWith (" + matchBy.getId() + ") : [" + existing + "] --> before [" + before + "]");
                        }
                    }
                }
            }
            if (!match) {
                throw new IllegalArgumentException("There are no outputs which matches: " + matchBy.getId() + " in the route: " + route);
            }
        }
    };
}
Also used : ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) Endpoint(org.apache.camel.Endpoint)

Example 3 with ProcessorDefinition

use of org.apache.camel.model.ProcessorDefinition in project camel by apache.

the class SimpleProcessorIdAwareTest method testIdAware.

public void testIdAware() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    List<Processor> matches = context.getRoute("foo").filter("b*");
    assertEquals(2, matches.size());
    Processor bar = matches.get(0);
    Processor baz = matches.get(1);
    assertEquals("bar", ((IdAware) bar).getId());
    assertEquals("baz", ((IdAware) baz).getId());
    bar = context.getProcessor("bar");
    assertNotNull(bar);
    baz = context.getProcessor("baz");
    assertNotNull(baz);
    Processor unknown = context.getProcessor("unknown");
    assertNull(unknown);
    Processor result = context.getProcessor("result");
    assertNotNull(result);
    ProcessorDefinition def = context.getProcessorDefinition("result");
    assertNotNull(def);
    assertEquals("result", def.getId());
    SendDefinition send = assertIsInstanceOf(SendDefinition.class, def);
    assertNotNull(send);
    assertEquals("mock:result", send.getEndpointUri());
}
Also used : Processor(org.apache.camel.Processor) SendDefinition(org.apache.camel.model.SendDefinition) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition)

Example 4 with ProcessorDefinition

use of org.apache.camel.model.ProcessorDefinition in project camel by apache.

the class DefaultCamelContext method getManagedProcessor.

public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T> type) {
    // jmx must be enabled
    if (getManagementStrategy().getManagementAgent() == null) {
        return null;
    }
    Processor processor = getProcessor(id);
    ProcessorDefinition def = getProcessorDefinition(id);
    if (processor != null && def != null) {
        try {
            ObjectName on = getManagementStrategy().getManagementNamingStrategy().getObjectNameForProcessor(this, processor, def);
            return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
        } catch (MalformedObjectNameException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    return null;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) Processor(org.apache.camel.Processor) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) ObjectName(javax.management.ObjectName)

Example 5 with ProcessorDefinition

use of org.apache.camel.model.ProcessorDefinition in project camel by apache.

the class DefaultExecutorServiceManager method onThreadPoolCreated.

/**
     * Invoked when a new thread pool is created.
     * This implementation will invoke the {@link LifecycleStrategy#onThreadPoolAdd(org.apache.camel.CamelContext,
     * java.util.concurrent.ThreadPoolExecutor, String, String, String, String) LifecycleStrategy.onThreadPoolAdd} method,
     * which for example will enlist the thread pool in JMX management.
     *
     * @param executorService the thread pool
     * @param source          the source to use the thread pool
     * @param threadPoolProfileId profile id, if the thread pool was created from a thread pool profile
     */
private void onThreadPoolCreated(ExecutorService executorService, Object source, String threadPoolProfileId) {
    // add to internal list of thread pools
    executorServices.add(executorService);
    String id;
    String sourceId = null;
    String routeId = null;
    // extract id from source
    if (source instanceof NamedNode) {
        id = ((OptionalIdentifiedDefinition<?>) source).idOrCreate(this.camelContext.getNodeIdFactory());
        // and let source be the short name of the pattern
        sourceId = ((NamedNode) source).getShortName();
    } else if (source instanceof String) {
        id = (String) source;
    } else if (source != null) {
        if (source instanceof StaticService) {
            // the source is static service so its name would be unique
            id = source.getClass().getSimpleName();
        } else {
            // fallback and use the simple class name with hashcode for the id so its unique for this given source
            id = source.getClass().getSimpleName() + "(" + ObjectHelper.getIdentityHashCode(source) + ")";
        }
    } else {
        // no source, so fallback and use the simple class name from thread pool and its hashcode identity so its unique
        id = executorService.getClass().getSimpleName() + "(" + ObjectHelper.getIdentityHashCode(executorService) + ")";
    }
    // id is mandatory
    ObjectHelper.notEmpty(id, "id for thread pool " + executorService);
    // extract route id if possible
    if (source instanceof ProcessorDefinition) {
        RouteDefinition route = ProcessorDefinitionHelper.getRoute((ProcessorDefinition<?>) source);
        if (route != null) {
            routeId = route.idOrCreate(this.camelContext.getNodeIdFactory());
        }
    }
    // let lifecycle strategy be notified as well which can let it be managed in JMX as well
    ThreadPoolExecutor threadPool = null;
    if (executorService instanceof ThreadPoolExecutor) {
        threadPool = (ThreadPoolExecutor) executorService;
    } else if (executorService instanceof SizedScheduledExecutorService) {
        threadPool = ((SizedScheduledExecutorService) executorService).getScheduledThreadPoolExecutor();
    }
    if (threadPool != null) {
        for (LifecycleStrategy lifecycle : camelContext.getLifecycleStrategies()) {
            lifecycle.onThreadPoolAdd(camelContext, threadPool, id, sourceId, routeId, threadPoolProfileId);
        }
    }
    // now call strategy to allow custom logic
    onNewExecutorService(executorService);
}
Also used : SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService) RouteDefinition(org.apache.camel.model.RouteDefinition) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) NamedNode(org.apache.camel.NamedNode) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) StaticService(org.apache.camel.StaticService)

Aggregations

ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)17 RouteDefinition (org.apache.camel.model.RouteDefinition)5 Endpoint (org.apache.camel.Endpoint)4 HashMap (java.util.HashMap)3 Processor (org.apache.camel.Processor)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 NamedNode (org.apache.camel.NamedNode)2 PerformanceCounter (org.apache.camel.api.management.PerformanceCounter)2 FromDefinition (org.apache.camel.model.FromDefinition)2 SendDefinition (org.apache.camel.model.SendDefinition)2 KeyValueHolder (org.apache.camel.util.KeyValueHolder)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 EventObject (java.util.EventObject)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1