camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergey Zhemzhitsky (JIRA)" <>
Subject [jira] [Created] (CAMEL-4371) Add an ability to replace endpoints
Date Tue, 23 Aug 2011 13:46:29 GMT
Add an ability to replace endpoints

                 Key: CAMEL-4371
             Project: Camel
          Issue Type: Improvement
          Components: camel-core
    Affects Versions: 2.8.0
            Reporter: Sergey Zhemzhitsky

Sometimes it can be useful to replace endpoints in the camel context. For example, in unit
tests it will not be necessary to define multiple properties file for different environments
with placeholders.

Here is the endpoint strategy to replace endpoints

package org.apache.camel.impl;

public class ReplaceEndpointStrategy implements EndpointStrategy {

    private Map<String, String> replacements = Collections.emptyMap();

    public Endpoint registerEndpoint(String uri, Endpoint endpoint) {
        CamelContext context = endpoint.getCamelContext();

        for(Entry<String, String> entry : replacements.entrySet()) {
            if(EndpointHelper.matchEndpoint(uri, entry.getKey())) {
                Endpoint newEndpoint = context.getEndpoint(entry.getValue());
                return newEndpoint;

        return endpoint;

    public void setReplacements(Map<String, String> replacements) {
        this.replacements = replacements;


Here is it can be used from spring

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""

    <bean class="org.apache.camel.impl.ReplaceEndpointStrategy">
        <property name="replacements">
                <entry key="timer://test*" value="direct://start" />
                <entry key="log://timer*" value="mock://tick" />

    <camelContext xmlns="">
            <from uri="timer://testTimer" />
            <to uri="log://timerTick" />


And the unit test
package org.apache.camel.impl;

import static org.junit.Assert.assertNotNull;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

public class ReplaceEndpointStrategyTest {

    private CamelContext camelContext;

    private ProducerTemplate producer;

    public void registerEndpoint() throws Exception {
        assertNotNull("direct:start is null", camelContext.hasEndpoint("direct:start"));
        assertNotNull("mock:tick is null", camelContext.hasEndpoint("mock:tick"));

    public void route() throws Exception {
        Endpoint start = camelContext.hasEndpoint("direct:start");
        MockEndpoint complete = (MockEndpoint) camelContext.hasEndpoint("mock:tick");

        complete.expectedBodiesReceived("Hello World!");
        producer.sendBody(start, "Hello World!");


This message is automatically generated by JIRA.
For more information on JIRA, see:


View raw message