Apache Camel Tutorial - SEDA component Hello World Example
The seda: component provides asynchronous SEDA behavior, so that messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread from the producer.
Apache Camel - Table of Contents
File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Spring Boot + Apache Camel + RabbitMQ - Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example
Need for SEDA component
Consider a scenario where we have a parent or starting route which we are going to call first. This parent route has a message which needs to be processed independently by three other routes.If we use synchronous call then there will be delay and not good performance. For example we use use direct endpoints then it will be a scynchronous call and the parent route will need to wait till the called route gets executed only then it can call the other routes. The direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange.
The immediate solution that comes to mind for such problems is to use asynchronous calls using JMS. Usually when building messaging systems we use something like JMS which provides us high reliability or message persistence. Camel provides a lighter alternative called SEDA component. It helps us utilizr asynchronous in memory messaging with zero. configuration
Let us first look at the example of using direct component. We will then see how the problems gets resolved using the SEDA component.
In resource folder define the log4j.properties as follows-
# Root logger option log4j.rootLogger=INFO, file, console # Direct log messages to stdout log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout #log4j.appender.console.layout.ConversionPattern=%d{HH:mm}| %p | %F %L | %m%n log4j.appender.stdout.layout.ConversionPattern=%d %p %t [%c] - %m%nThe pom.xml is as follows-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainuse</groupId> <artifactId>camel-seda</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> </dependencies> </project>Define the DirectEndPointRouter.java. Here we will see that the parent route calls the direct endpoint. Since this is a synchronous call it does waits for the child direct route to complete and only after that continuous with its own route execution.