Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Adding camel consumer routes dynamically to camel context

Overview

In this article we are going to learn how to add consumer routes dynamically. Consider a situation where your requirement is to process some files from different FTP servers. And details of those FTP servers keeps changing.

Use Case:

Lets assume you want to achieve something below:

1. Process should be started itself in timely manner.

2. Fetch the FTP server details from the database.

3. Add the camel consumer routes for above FTP servers.


How To DO

Step#1 and Step#2 already explained in other articles so we will mainly focus on step#3 i.e. how to add consumer routes dynamically, if we have SDT data FTPDetails pre-populated.

EIPDynamicRouteProcess.PNG

POJO which will iterate ftpDetails object and add routes:

package com.test;
 
import java.util.ArrayList;
import java.util.Map;
 
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.eclipse.stardust.common.config.Parameters;
import org.eclipse.stardust.engine.extensions.camel.RouteHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
 
public class CustodianRoutesHelper {
	private static final String PRP_APPLICATION_CONTEXT = "org.eclipse.stardust.engine.api.spring.applicationContext";
	private ApplicationContext springContext;
	private CamelContext camelContext;
 
	public CustodianRoutesHelper() {
		this.springContext = (AbstractApplicationContext) Parameters.instance()
				.get(PRP_APPLICATION_CONTEXT);
		this.camelContext = (DefaultCamelContext) this.springContext
				.getBean("defaultCamelContext");
	}
 
	public void addFtpRoutes(Object ftpDetails) {
		Map<String, Object> map = (Map<String, Object>) ftpDetails;
		ArrayList<Map> ftpDetailsList = (ArrayList<Map>) map.get("FtpDetails");
 
		for (int i = 0; i < ftpDetailsList.size(); i++) {
			Map<String, String> ftpRecord = ftpDetailsList.get(i);
			StringBuilder routeDefinition = new StringBuilder(
					"<routes xmlns=\"http://camel.apache.org/schema/spring\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
							+ "<route id=\"CustodianFTPConsumer"
							+ i
							+ "\""
							+ " autoStartup=\"false\""
							+ ">"
							+ "<description>This is Cutodian Route listening to FTP Server: "
							+ ftpRecord.get("FTPHostName")
							+ "</description>"
							+ "<from uri=\"ftp://");
 
			routeDefinition.append(ftpRecord.get("FTPUserName") + "@"
					+ ftpRecord.get("FTPHostName") + ":"
					+ ftpRecord.get("FTPPortNo") + "/"
					+ ftpRecord.get("FTPSourceDir") + "?" + "password="
					+ ftpRecord.get("FTPPassword")
					+ "&amp;delay=1000&amp;delete=true&amp;disconnect=true\""
					+ "/>");
			routeDefinition
					.append("<transform><simple>$simple{bodyAs(String)}</simple></transform>");
			routeDefinition
					.append("<to uri=\"ipp:authenticate:setCurrent?user=motu&amp;password=motu\"/>");
			routeDefinition
					.append("<to uri=\"ipp:process:start?processId=FTPProcess\"/>");
			routeDefinition
					.append("<to uri=\"ipp:authenticate:removeCurrent\"/>");
			routeDefinition.append("</route>");
			routeDefinition.append("</routes>");
 
			try {
				System.out.println("Adding below route definition to bean: "
						+ routeDefinition);
				RouteHelper.loadRouteDefinition(routeDefinition.toString(),
						this.camelContext);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
 
		}
 
	}
 
}

Deploy the attached model and artifacts and run the process

DynamicEIPFTPDetails.PNG

Routes will be listed under Admin-->Integration-runtime from there you can start and stop the routes.

EIPDynamicallyAddedRoute.PNG

Artifacts

All the artifacts dicussed in this article could be found here File:EIPDynamicRoutes.zip

Copyright © Eclipse Foundation, Inc. All Rights Reserved.