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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Hybrid Multi-Cloud Event Mesh With Apache Camel and Kubernetes
  • When Feature Flags Become Workflows: Configuring Multi-Tenant State Machines
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • How Much Signal Differential Privacy Takes From Your Dashboards, in Events

Trending

  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI
  • The Art of Idempotency: Preventing Double Charges and Duplicate Actions
  • Picture Perfect: Choosing Your Featured Image
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Event Notifier in Apache Camel Route

Event Notifier in Apache Camel Route

Apache Camel is an open-source, lightweight integration library. In this article, I will demonstrate the concept of events it allows and how to use them.

By 
Rogerio Santos
·
Updated Feb. 19, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

Each step of an Apache Camel Application triggers an event that allows us to read or edit data while the message travels on the "Route." The provided mechanism to access these events is the EventNotifierSupport class.

Apache Camel's route is a sequence of steps, executed in order by Camel, that consumes and processes a message. The initial point of this sequence is represented by the method from(). A route may have many processing components that modify a message or send it to an endpoint. 

To send a message to an endpoint, we use the method to(), so the basic structure of an Apache Camel Route will be:

basic structure of an Apache Camel Route

Between two points, like a from()-> to() or to() -> to(), many other steps occur that we cannot avoid. We call these steps "events."

Events are essential for us because we can interact with them to enrich our application's behavior.

Interaction with events

Interaction With Events

To capture events in Apache Camel, we need to create a class that extends the abstract class EventNotifierSupport. In this class, we will specify a method called isEnabled to activate the event capture.

In the example below, there are two methods: the method isEnable tells Apache Camel that we will capture the events. If the isEnabled method returns true, the notify method receives the event, and then we can interact with them.

Java
 




xxxxxxxxxx
1
17


 
1
@Component
2
public class CamelEvents extends EventNotifierSupport {
3
    
4
    private Integer eventSequence = 1;
5
    
6
    @Override
7
    public void notify(EventObject event) throws Exception {
8
        
9
    }   
10

          
11
    @Override
12
    public boolean isEnabled(EventObject event) {       
13
        return true;
14
    }
15

          
16
}



The notify method receives an object of type EventObject that contains the event that occurred at the moment. We can verify what event was started by checking the instance of the object.

Java
 




xxxxxxxxxx
1


 
1
@Override
2
public void notify(EventObject event) throws Exception {
3
        
4
    if (event instanceof ExchangeCreatedEvent) {    
5
    
6
        ... 
7
    } 
8
    
9
...     



After capturing the event, we can interact with it to read or edit data in the message trafficked on the route. For example, we can implement an automatic log of all content trafficked on the route at each stage, or we can apply rules to validate the data or enrich it.

We can make life easier for developers by creating generic components that remove the need to develop repetitive or common block codes.

I posted an event capture example on GitHub.  Inspect the class com.consulting.fuse.poc.event.CamelEvents.java and notice the notify method capturing a lot of events.

Also notice the below route:

Java
 




xxxxxxxxxx
1


 
1
from("file:trigger/?fileName=test-a.txt&noop=false")
2
.routeId("route-a")     
3
.to("bean:processorA")          
4
.log("### :: The step to(\"bean:processorA\") was executed")



This route reads a file called "test-a.txt" in a specific folder and sends the content to a process named processorA using the to() method.

In this flow, the following events occur in sequence:

  1. ExchangeCreatedEvent
  2. ExchangeSendingEvent
  3. ExchangeSentEvent
  4. ExchangeCompletedEvent

When the route is executing, each event is sent to the Event Notifies implementation in the class CamelEvents.

In this example, we printed each event in the log with the sequence number of execution.

Print of each event in a log with the sequence number of execution

Exchange completed event

In each event, we can manipulate or only get the values of Exchange to use. The following block changes the content received from the file for an upper case value-added the prefix "EDITED:"

So if we include other steps in our route, the trafficked content will always be in capital letters. The programmer doesn't need to worry about it because the event does it for all routes.

Java
 




x
12


 
1
if (event instanceof ExchangeSentEvent) {   
2
            
3
    String body = ((ExchangeSentEvent) event).getExchange().getIn().getBody(String.class);
4
            
5
    body = "EDITED: " + body.toUpperCase();         
6
    ((ExchangeSentEvent) event).getExchange().getIn().setBody(body);
7
            
8
    LOGGER.info("### [EVENT] ExchangeSentEvent executed " + eventSequence + " :: Body content edited to UPPERCASE");
9

          
10
}



Something important to say is that Exceptions also launch events. Take, for example, the route-non-handled-exception route that exists in our sample. When we force an Exception, an event called ExchangeFailedEvent occurs regardless of whether we treat or not the Exception.

Java
 




xxxxxxxxxx
1
12


 
1
from("file:trigger/?fileName=test-b.txt&noop=false&moveFailed=failed")
2
    .routeId("route-non-handled-exception")
3
    .process(new Processor() {
4

          
5
     @Override
6
      public void process(Exchange exchange) throws Exception {         
7
         if (null == exchange.getIn().getHeader("fooHeader")) {             
8
        throw new NonHandledException("Exception throwed to test");                   }         
9
       }
10
                })
11
    .log("NonHandledException occur ")



ExchangeFailedEvent

Conclusion

Events are potent resources on our routes. As shown in our example, several events happen during the execution of our Camel routes. Explore our example application and watch the events occurring from the application's initialization to completing a route.

A complete Camel route

Unfortunately, there isn't extensive documentation on these events, but here we have a good starting point.

Event Apache Camel

Opinions expressed by DZone contributors are their own.

Related

  • Building Hybrid Multi-Cloud Event Mesh With Apache Camel and Kubernetes
  • When Feature Flags Become Workflows: Configuring Multi-Tenant State Machines
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • How Much Signal Differential Privacy Takes From Your Dashboards, in Events

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook