Search Tutorials


Apache Camel Tutorial - SEDA component Hello World Example | JavaInUse



Apache Camel Tutorial - SEDA component Hello World Example

SEDA is an acronym that stands for Staged Event Driven Architecture it is designed as a mechanism to regulate the flow between different phases of message processing.
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

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.
apache camel direct component
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
apache camel seda component
Let us first look at the example of using direct component. We will then see how the problems gets resolved using the SEDA component.
The Maven project will be as follows-
camel seda maven
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%n
The 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.
apache camel direct component route
package com.javainuse;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DirectEndPointRouter extends RouteBuilder {

	static Logger LOG = LoggerFactory.getLogger(DirectEndPointRouter.class);

	public static final String DIRECT_END_ROUTE = "direct:end";
	public static final String DIRECT_START_ROUTE = "direct:start";

	@Override
	public void configure() throws Exception {
		from(DIRECT_START_ROUTE).routeId("StartRouteId").setBody().simple("Start Message").to(DIRECT_END_ROUTE)
				.process(new Processor() {

					public void process(Exchange exchange) throws Exception {
						LOG.info("Message at start route completion");
					}
				});

		from(DIRECT_END_ROUTE).routeId("EndRouteId").delay(10000).setBody().simple("End Message")
				.process(new Processor() {
					public void process(Exchange exchange) throws Exception {
						LOG.info("message after end-route completion");

					}
				});
	}
}
Define the RunDirectApp.java
package com.javainuse;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.PropertyConfigurator;

public class RunDirectApp {

	public static void main(String[] args) throws Exception {
		PropertyConfigurator.configure("C:/Users/SONY/Desktop/camel-seda/src/main/resources/log4j.properties");
		CamelContext camelContext = new DefaultCamelContext();
		camelContext.addRoutes(new DirectEndPointRouter());
		camelContext.start();

		ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
		producerTemplate.sendBody(DirectEndPointRouter.DIRECT_START_ROUTE, "start Message");
	}
}
Run the above file as a java application. We get the output as follows-
apache camel direct component output

Define the SedaEndPointRouter.java. Here we will see that the parent route calls the SEDA endpoint. Since this is an asynchronous call it does not wait for the SEDA route to complete but continuous with its own route execution.
apache camel seda component route

package com.javainuse;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SedaEndPointRouter extends RouteBuilder {

	static Logger LOG = LoggerFactory.getLogger(SedaEndPointRouter.class);

	public static final String DIRECT_START_ROUTE = "direct:start";
	public static final String SEDA_END_ROUTE = "seda:end";

	@Override
	public void configure() throws Exception {
		from(DIRECT_START_ROUTE).routeId("SedaStartRouteId").setBody().simple("Seda Message").to(SEDA_END_ROUTE)
				.process(new Processor() {
					public void process(Exchange exchange) throws Exception {
						LOG.info("Message at Parent route completion");
					}
				});

		from(SEDA_END_ROUTE).routeId("EndRouteId").setBody().simple("End Message").process(new Processor() {
			public void process(Exchange exchange) throws Exception {
				LOG.info("message after seda end route completion");
			}
		});
	}
}
Define the RunSedaApp.java
package com.javainuse;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.PropertyConfigurator;

public class RunSedaApp {

	public static void main(String[] args) throws Exception {
		PropertyConfigurator.configure("C:/Users/SONY/Desktop/camel-seda/src/main/resources/log4j.properties");
		CamelContext camelContext = new DefaultCamelContext();
		camelContext.addRoutes(new SedaEndPointRouter());
		camelContext.start();

		ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
		producerTemplate.sendBody(SedaEndPointRouter.DIRECT_START_ROUTE, "start Message");

		Thread.sleep(2000);
	}
}
Run the above file as a java application. We get the output as follows-
apache camel seda component output

Download Source Code

Download it - Apache Camel + SEDA Hello World Example Application

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions