Search in sources :

Example 16 with Exchange

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

the class LevelDBAggregationRepositoryAlotDataTest method testWithAlotOfDataTwoKesy.

@Test
public void testWithAlotOfDataTwoKesy() {
    LevelDBAggregationRepository repo = new LevelDBAggregationRepository();
    repo.setLevelDBFile(levelDBFile);
    repo.setRepositoryName("repo1");
    for (int i = 0; i < 100; i++) {
        Exchange exchange1 = new DefaultExchange(context);
        exchange1.getIn().setBody("counter:" + i);
        String key = i % 2 == 0 ? "foo" : "bar";
        repo.add(context, key, exchange1);
    }
    // Get it back..
    Exchange actual = repo.get(context, "foo");
    assertEquals("counter:98", actual.getIn().getBody());
    actual = repo.get(context, "bar");
    assertEquals("counter:99", actual.getIn().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Test(org.junit.Test)

Example 17 with Exchange

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

the class LuceneIndexAndQueryProducerTest method testReturnLuceneDocsQueryProducer.

@Test
public void testReturnLuceneDocsQueryProducer() throws Exception {
    MockEndpoint mockSearchEndpoint = getMockEndpoint("mock:searchResult");
    context.stop();
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:start").setHeader("QUERY", constant("Grouc?? Marx")).setHeader("RETURN_LUCENE_DOCS", constant("true")).to("lucene:searchIndex:query?analyzer=#stdAnalyzer&indexDir=#std&maxHits=20").to("direct:next");
            from("direct:next").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Hits hits = exchange.getIn().getBody(Hits.class);
                    try {
                        printResults(hits);
                    } catch (Exception e) {
                        LOG.error(e.getMessage());
                        exchange.getOut().setBody(null);
                    }
                }

                private void printResults(Hits hits) throws Exception {
                    LOG.debug("Number of hits: " + hits.getNumberOfHits());
                    for (int i = 0; i < hits.getNumberOfHits(); i++) {
                        LOG.debug("Hit " + i + " Index Location:" + hits.getHit().get(i).getHitLocation());
                        LOG.debug("Hit " + i + " Score:" + hits.getHit().get(i).getScore());
                        LOG.debug("Hit " + i + " Data:" + hits.getHit().get(i).getData());
                        if (hits.getHit().get(i).getDocument() == null) {
                            throw new Exception("Failed to return lucene documents");
                        }
                    }
                }
            }).to("mock:searchResult").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    Hits hits = exchange.getIn().getBody(Hits.class);
                    if (hits == null) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("NO_LUCENE_DOCS_ERROR", "NO LUCENE DOCS FOUND");
                        exchange.getContext().setProperties(map);
                    }
                    LOG.debug("Number of hits: " + hits.getNumberOfHits());
                }
            });
        }
    });
    context.start();
    LOG.debug("------------Beginning  LuceneQueryProducer Wildcard with Return Lucene Docs Test---------------");
    sendQuery();
    mockSearchEndpoint.assertIsSatisfied();
    Map<String, String> errorMap = mockSearchEndpoint.getCamelContext().getProperties();
    LOG.debug("------------Completed LuceneQueryProducer Wildcard with Return Lucene Docs Test---------------");
    context.stop();
    assertTrue(errorMap.get("NO_LUCENE_DOCS_ERROR") == null);
}
Also used : Exchange(org.apache.camel.Exchange) Hits(org.apache.camel.processor.lucene.support.Hits) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 18 with Exchange

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

the class LumberjackConsumer method onMessageReceived.

private void onMessageReceived(Object payload, LumberjackMessageProcessor.Callback callback) {
    // Create the exchange
    Exchange exchange = getEndpoint().createExchange();
    exchange.getIn().setBody(payload);
    // Process the exchange
    getAsyncProcessor().process(exchange, doneSync -> callback.onComplete(!exchange.isFailed()));
}
Also used : Exchange(org.apache.camel.Exchange)

Example 19 with Exchange

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

the class MailConsumer method createExchanges.

protected Queue<Exchange> createExchanges(List<KeyValueHolder<String, Message>> messages) throws MessagingException {
    Queue<Exchange> answer = new LinkedList<Exchange>();
    int fetchSize = getEndpoint().getConfiguration().getFetchSize();
    int count = fetchSize == -1 ? messages.size() : Math.min(fetchSize, messages.size());
    if (LOG.isDebugEnabled()) {
        LOG.debug("Fetching {} messages. Total {} messages.", count, messages.size());
    }
    for (int i = 0; i < count; i++) {
        try {
            KeyValueHolder<String, Message> holder = messages.get(i);
            String key = holder.getKey();
            Message message = holder.getValue();
            if (LOG.isTraceEnabled()) {
                LOG.trace("Mail #{} is of type: {} - {}", new Object[] { i, ObjectHelper.classCanonicalName(message), message });
            }
            if (!message.getFlags().contains(Flags.Flag.DELETED)) {
                Exchange exchange = getEndpoint().createExchange(message);
                if (getEndpoint().getConfiguration().isMapMailMessage()) {
                    // ensure the mail message is mapped, which can be ensured by touching the body/header/attachment
                    LOG.trace("Mapping #{} from javax.mail.Message to Camel MailMessage", i);
                    exchange.getIn().getBody();
                    exchange.getIn().getHeaders();
                    exchange.getIn().getAttachments();
                }
                // If the protocol is POP3 we need to remember the uid on the exchange
                // so we can find the mail message again later to be able to delete it
                // we also need to remember the UUID for idempotent repository
                exchange.setProperty(MAIL_MESSAGE_UID, key);
                answer.add(exchange);
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Skipping message as it was flagged as deleted: {}", MailUtils.dumpMessage(message));
                }
            }
        } catch (Exception e) {
            if (skipFailedMessage) {
                LOG.debug("Skipping failed message at index " + i + " due " + e.getMessage(), e);
            } else if (handleFailedMessage) {
                handleException(e);
            } else {
                throw e;
            }
        }
    }
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) Message(javax.mail.Message) LinkedList(java.util.LinkedList) MessagingException(javax.mail.MessagingException) FolderNotFoundException(javax.mail.FolderNotFoundException)

Example 20 with Exchange

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

the class MailEndpoint method createExchange.

public Exchange createExchange(Message message) {
    Exchange exchange = super.createExchange();
    exchange.setProperty(Exchange.BINDING, getBinding());
    exchange.setIn(new MailMessage(message, getConfiguration().isMapMailMessage()));
    return exchange;
}
Also used : Exchange(org.apache.camel.Exchange)

Aggregations

Exchange (org.apache.camel.Exchange)3446 Test (org.junit.Test)1735 Processor (org.apache.camel.Processor)1405 RouteBuilder (org.apache.camel.builder.RouteBuilder)666 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)640 DefaultExchange (org.apache.camel.impl.DefaultExchange)473 Message (org.apache.camel.Message)379 Endpoint (org.apache.camel.Endpoint)235 HashMap (java.util.HashMap)190 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)155 Producer (org.apache.camel.Producer)150 File (java.io.File)120 ArrayList (java.util.ArrayList)117 CamelContext (org.apache.camel.CamelContext)117 List (java.util.List)99 Map (java.util.Map)96 ProducerTemplate (org.apache.camel.ProducerTemplate)94 IOException (java.io.IOException)92 Tx (org.nhindirect.common.tx.model.Tx)83 Predicate (org.apache.camel.Predicate)78