> from("direct:jde")
>   .setHeader("FMZ57FMR", simple("${body.fuelDistribution.quantity}"))
>   .setHeader("FMZ57ST",
> simple("${body.fuelDistribution.sourceTank.state}"))
>   .setHeader("FMZ57JOB", constant(null))
>   .setHeader("FMZ57MISC1",
> DateUtility.convertDateToKRC("${date:now:yyyy-MM-dd}"))
>   .setHeader("FMZ57MISC2", simple("${date:now:yyyy-MM-dd}"))
>   .to("sql:" + insertStmt.replaceAll("\t", " ") +
> "?dataSource=exampleds");
> 
> I have two questions.
> 
> 1. How do I make DateUtility.convertDateToKRC("${date:now:yyyy-MM-dd}")
> into an Expression as required by the setHeader method.  DateUtility is
> a Java class with static method convertDateToKRC. The method takes one
> string argument.  


Several possibilities
- create your own expression delegating to that class
- for dates use simple("${date:...}")
- for registered beans use simple("${bean:id.field}")
see example later



> 2.  Is there an alternative to the expression:
> simple("${body.fuelDistribution.sourceTank.state}")
> 
> Specifically I am looking for the cleanest way to retrieve items
> several levels deep from a Java object in the body of the message.

Maybe xpath("//state")?



Jan



public class DateUtilTest extends CamelTestSupport {

    @Test
    public void split() throws InterruptedException {
        sendBody("direct:in", null);
        Thread.sleep(100);
        sendBody("direct:in", null);
        List<Exchange> exchanges =
getMockEndpoint("mock:end").getExchanges();
        assertNoDuplicateHeaders(exchanges, "date", String.class);
    }

    private <T> void assertNoDuplicateHeaders(List<Exchange> exchanges,
String headerName, Class<T> headerType) {
        List<T> alreadyCounted = new ArrayList<T>();
        for(Exchange exchange : exchanges) {
            T date = exchange.getIn().getHeader(headerName, headerType);
            assertFalse(alreadyCounted.contains(date));
            alreadyCounted.add(date);
        }
    }
    
    public static class DateSupplier {
        public static String getDate() {
            return new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss:SSS").format(new Date());
        }
    }
    
    public class DateExpression implements Expression {
        @SuppressWarnings("unchecked")
        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            if (type.isInstance("")) {
                return (T)DateSupplier.getDate();
            } else {
                return null;
            }
        }
    }
    
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                bind(context, "dateSupplier", new DateSupplier());
                
                
                from("direct:in")
                    // does not work as constant() is evaluated once at
start up time
                    // .setHeader("date", constant("" +
System.currentTimeMillis()))
                    
                    // works with the overhead of a custom Expression
delegating to the target class
                    //.setHeader("date", new DateExpression())
                
                    // works with named beans
                    .setHeader("date", simple("${bean:dateSupplier.date}"))
                    
                    // works for dates
                    //.setHeader("date", simple("${date:now:yyyy-MM-dd
HH:mm:ss:SSS}"))
                
                    // does not work 
                    //.setHeader("date",
simple("${type:de.materne.camel.test.splitter.DateUtilTest.DateSupplier.date
}"))
                    
                    .to("mock:end");
            }

            // A hack for adding beans to the registry. The registry
interface itself only supports lookups.
            private void bind(CamelContext context, String name,
DateSupplier object) {
                PropertyPlaceholderDelegateRegistry reg =
(PropertyPlaceholderDelegateRegistry)context.getRegistry();
                JndiRegistry registry = (JndiRegistry)reg.getRegistry(); 
                registry.bind(name, object);
            }
        };
    }
}

Reply via email to