use of org.apache.camel.Processor in project camel by apache.
the class ChoiceProcessor method process.
public boolean process(final Exchange exchange, final AsyncCallback callback) {
Iterator<Processor> processors = next().iterator();
// callback to restore existing FILTER_MATCHED property on the Exchange
final Object existing = exchange.getProperty(Exchange.FILTER_MATCHED);
final AsyncCallback choiceCallback = new AsyncCallback() {
@Override
public void done(boolean doneSync) {
if (existing != null) {
exchange.setProperty(Exchange.FILTER_MATCHED, existing);
} else {
exchange.removeProperty(Exchange.FILTER_MATCHED);
}
callback.done(doneSync);
}
};
// and if not, we just continue without using any processor
while (processors.hasNext()) {
// get the next processor
Processor processor = processors.next();
// evaluate the predicate on filter predicate early to be faster
// and avoid issues when having nested choices
// as we should only pick one processor
boolean matches = false;
if (processor instanceof FilterProcessor) {
FilterProcessor filter = (FilterProcessor) processor;
try {
matches = filter.matches(exchange);
// as we have pre evaluated the predicate then use its processor directly when routing
processor = filter.getProcessor();
} catch (Throwable e) {
exchange.setException(e);
}
} else {
// its the otherwise processor, so its a match
notFiltered++;
matches = true;
}
// check for error if so we should break out
if (!continueProcessing(exchange, "so breaking out of choice", LOG)) {
break;
}
// if we did not match then continue to next filter
if (!matches) {
continue;
}
// okay we found a filter or its the otherwise we are processing
AsyncProcessor async = AsyncProcessorConverterHelper.convert(processor);
return async.process(exchange, choiceCallback);
}
// when no filter matches and there is no otherwise, then just continue
choiceCallback.done(true);
return true;
}
use of org.apache.camel.Processor in project camel by apache.
the class BeanMethodWithExchangeTest method testBeanWithAnnotationAndExchangeTest.
public void testBeanWithAnnotationAndExchangeTest() throws Exception {
Exchange result = template.request("direct:start1", new Processor() {
public void process(Exchange exchange) throws Exception {
Message m = exchange.getIn();
m.addAttachment("attachment", new DataHandler(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java")));
}
});
assertTrue(result.getOut().getAttachmentObjects().containsKey("attachment2"));
assertTrue(result.getOut().getAttachments().containsKey("attachment1"));
assertEquals("attachmentValue1", result.getOut().getAttachmentObjects().get("attachment1").getHeader("attachmentHeader1"));
assertFalse(result.getOut().getAttachments().containsKey("attachment"));
}
use of org.apache.camel.Processor in project camel by apache.
the class BeanInfoSelectMethodTest method testFailure.
public void testFailure() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("Failure");
template.send("direct:b", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("Hello");
exchange.setException(new IllegalArgumentException("Forced by unit test"));
}
});
assertMockEndpointsSatisfied();
}
use of org.apache.camel.Processor in project camel by apache.
the class DirectoryCreateIssueTest method testFileCreatedAsDir.
public void testFileCreatedAsDir() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(numFiles);
template.send("seda:testFileCreatedAsDir", new Processor() {
public void process(Exchange exchange) {
Message in = exchange.getIn();
in.setBody("Contents of test file");
}
});
assertMockEndpointsSatisfied();
// wait a little while for the files to settle down
Thread.sleep(200);
for (int i = 0; i < numFiles; i++) {
assertTrue((new File(path + "/file" + i + ".txt")).isFile());
}
}
use of org.apache.camel.Processor in project camel by apache.
the class FileConsumerAutoCreateDirectoryTest method testDoNotCreateDirectory.
public void testDoNotCreateDirectory() throws Exception {
deleteDirectory("target/file/foo");
Endpoint endpoint = context.getEndpoint("file://target/file/foo?autoCreate=false");
Consumer consumer = endpoint.createConsumer(new Processor() {
public void process(Exchange exchange) throws Exception {
// noop
}
});
consumer.start();
consumer.stop();
// the directory should NOT exists
File dir = new File("target/file/foo");
assertFalse("Directory should NOT be created", dir.exists());
}
Aggregations