DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Leveraging Salesforce Using a Client Written In Vue.js
  • Visually Designing Views for Java Web Apps
  • Spring Application Listeners

Trending

  • MCP Client Agent: Architecture and Implementation
  • Multiple Stakeholder Management in Software Engineering
  • Build AI Agents With MCP Server in C# and Run in VS Code
  • Cell-Based Architecture: Comprehensive Guide
  1. DZone
  2. Coding
  3. Frameworks
  4. Measure Elapsed Time with Camel

Measure Elapsed Time with Camel

By 
Charles Moulliard user avatar
Charles Moulliard
·
Jan. 11, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
12.0K Views

Join the DZone community and get the full member experience.

Join For Free

Apache Camel provides an event notifier support class which allows you to keep information about what happened on Exchange, Route and Endpoint. One of the benefits of this class is that you can easily audit messages created in Camel Routes, collect information and report that in log by example.

When developing an application, it is very important to calculate/measure elapsed time on the platform to find which part of your code, processor or system integrated which is the bad duck and must be improved.

In three steps, I would show you How to enable this mechanism to report :
- Time elapsed to call an endpoint (could be another camel route, web service, ...)
- Time elapsed on the route exchange

STEP 1 - Create a Class implementing the EventNotifierSupport

public class AuditEventNotifier extends EventNotifierSupport {

public void notify(EventObject event) throws Exception {
  if (event instanceof ExchangeSentEvent) {
      ExchangeSentEvent sent = (ExchangeSentEvent) event;
      log.info(">>> Took " + sent.getTimeTaken() + " millis to send to external system : " + sent.getEndpoint());
  }

  if (event instanceof ExchangeCompletedEvent) {;
      ExchangeCompletedEvent exchangeCompletedEvent = (ExchangeCompletedEvent) event;
      Exchange exchange = exchangeCompletedEvent.getExchange();
      String routeId = exchange.getFromRouteId();
      Date created = ((ExchangeCompletedEvent) event).getExchange().getProperty(Exchange.CREATED_TIMESTAMP, Date.class);
      // calculate elapsed time
      Date now = new Date();
      long elapsed = now.getTime() - created.getTime();
      log.info(">>> Took " + elapsed + " millis for the exchange on the route : " + routeId);
  }
}

public boolean isEnabled(EventObject event) {
  return true;
}

protected void doStart() throws Exception {
  // filter out unwanted events
  setIgnoreCamelContextEvents(true);
  setIgnoreServiceEvents(true);
  setIgnoreRouteEvents(true);
  setIgnoreExchangeCreatedEvent(true);
  setIgnoreExchangeCompletedEvent(false);
  setIgnoreExchangeFailedEvents(true);
  setIgnoreExchangeRedeliveryEvents(true);
  setIgnoreExchangeSentEvents(false);
}

protected void doStop() throws Exception {
  // noop
}

}

Not really complicated and the code is explicit. Check the doStart() method to enable/disable the events for which you would like to gather information.

This example uses only Exchange.CREATED_TIMESTAMP property but the next version of Camel 2.7.0 will provide you the property exchange.RECEIVED_TIMESTAMP and so you will be able to calculate more easily the time spend by the exchange to call the different processors till it arrives at the end of the route.

This example collects Date information but you can imagine to use this mechanism to check if your route processes the message according to SLA, ....

STEP 2 - Instantiate the bean in Camel Spring XML

<!-- Event Notifier -->
<bean id="auditEventNotifier" class="com.sfr.audit.AuditEventNotifier">
</bean>
By adding this bean definition, Camel will automatically register it to the CamelContext created.

STEP 3 - Collect info into the log
18:10:46,060 | INFO  | tp1238469515-285 | AuditEventNotifier               | ?                                   ? | 68 - org.apache.camel.camel-core - 2.6.0.fuse-00-00 | >>> Took 3 millis for the exchange on the route : mock-HTTP-Server
18:10:46,062 | INFO  | tp2056154542-293 | AuditEventNotifier               | ?                                   ? | 68 - org.apache.camel.camel-core - 2.6.0.fuse-00-00 | >>> Took 25 millis to send to external system : Endpoint[http://localhost:9191/sis]
18:10:46,077 | INFO  | tp2056154542-293 | AuditEventNotifier               | ?                                   ? | 68 - org.apache.camel.camel-core - 2.6.0.fuse-00-00 | >>> Took 103 millis for the exchange on the route : ws-to-sis

Measure (physics) Apache Camel Spring Framework Property (programming) Event Web Service application

Published at DZone with permission of Charles Moulliard. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Leveraging Salesforce Using a Client Written In Vue.js
  • Visually Designing Views for Java Web Apps
  • Spring Application Listeners

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: