Apache Camel - How to configure endpoint with attributes in java

762 Views Asked by At

Here is my perfectly working Camel route in XML below

<route id="someId">
            <from id="_from" uri="{{consumer.serviceName}}:queue:{{consumer.notificationQueue}}?{{consumer.queryParams}}"/>
            <log loggerRef="loggerId" message="Messages throttling from Queue"/>
            <throttle prop:timePeriodMillis="{{throttle.timePeriod}}">
                <constant>{{throttle.maximumRequestsPerSecond}}</constant>
                <log loggerRef="logger" message="Consuming notification message from Queue {{consumer.myQueue}} : ${body}"/> 
                <bean id="beanId" method="process" ref="MyProcessor"/>
            </throttle>
        </route>

Now, I want to write a similar camel endpoint in Java. Can you please tell me how to add log and throttle attributes in it?

MyProcessor  messageProcessor;
String Uri = serviceName + ":queue:" + queueName + "?" + queryParams;
Endpoint ep = camelContext.getEndpoint(Uri);
Consumer consumer = ep.createConsumer(messageProcessor);
consumer.start();
1

There are 1 best solutions below

6
On

I hope it will help you:

 from("{{consumer.serviceName}}:queue:{{consumer.notificationQueue}}?{{consumer.queryParams}}").id("from_")
            .log(LoggingLevel.INFO, loggerObject, "Messages throttling from Queue")
            .throttle(constant("{{maximumRequestsPerSecond}}")).timePeriodMillis(1000)//pass throttle.timePeriod parameter from your config here
            .log(LoggingLevel.INFO, loggerObject, "Consuming notification message from Queue {{consumer.myQueue}} : ${body}")
            .bean(beanObject, "process").id("beanId")
            .end();