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);
}
}
};
}
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);
}
}
};
}
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());
}
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;
}
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);
}
Aggregations