user_loser
user_loser

Reputation: 871

Apache Camel Resequence Program Not Working

I am trying to run a simple re-sequence program using Apache Camel. This program uses the Java DSL to re-sequence incoming Java messages. When I run this program the messages are written to the folder but do not appear to be in any particular order based on the header value or the alphabetical order of the single word in the message body. The files Camel creates are out of order still as if the resequence DSL function did nothing.

How can I get this program to actually order the messages like the Arrays.sort() method would do? Also, how can I get this program to resequence and then aggregate the messages in the correct sort order to a single file?

Here is the program... I call the main Camel route via the other class that has the main method.

import org.apache.camel.builder.RouteBuilder;

public class SortThoseMessages extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("direct:pointA")
                .resequence(header("grocery"))

                .to("file:target/pointB");
    }

}

The class below has main and produces the messages into the queue, pointA.

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.ProducerTemplate;

public class NewSequenceMain {

    public static void main(String[] args) { 
        CamelContext c = new DefaultCamelContext();

        try { 

         c.addRoutes(new SortThoseMessages());



        ProducerTemplate template = c.createProducerTemplate();

        c.start();

        template.sendBodyAndHeader("direct:pointA", "apple", "grocery", 1);
        template.sendBodyAndHeader("direct:pointA", "orange", "grocery", 3);
        template.sendBodyAndHeader("direct:pointA", "bannanna", "grocery", 2);

        Thread.sleep(5000);
        c.stop();

        } catch(Exception ex) { 
            System.err.println("Exception thrown -> " + ex);
            System.err.println("Now printing stacktrace...");
            ex.printStackTrace();
        }

    }

}

Upvotes: 0

Views: 888

Answers (1)

user_loser
user_loser

Reputation: 871

The messages maybe re-sequenced inside the Camel route but not when written to file. To see the re-sequence call the aggregator Java DSL to see the message bodies in the sequence order specified. The messages in the program posted are ordered according to the number of the header. The method call on the ProducerTemplate object reference sets the header as the integer in the last argument to the sendBodyAndHeader() method call.

To see the re-sequencing take effect in a single file as the destination of the Camel route please check out the example below:

import org.apache.camel.builder.RouteBuilder;


public class ReorganizingMessages extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("direct:pointA")
                .resequence(header("grocery"))
                .to("log://org.apache.camel.howto?showAll=true&multiline=true")

                .aggregate().constant(true).completionTimeout(100L).
                aggregationStrategy(new StringAggregator())
                .to("file:target/pointB");
    }

}

The code above uses a custom aggregator Java bean that can be seen below.

import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;

public class StringAggregator implements AggregationStrategy {

    @Override
    public Exchange aggregate(Exchange old, Exchange new1) {
        if (old == null) { 
            return new1;
        }

        String oldBody = old.getIn().getBody(String.class);
        String newBody = new1.getIn().getBody(String.class);
        old.getIn().setBody(oldBody + " " + newBody);
        return old;

    }

}

Upvotes: 0

Related Questions