camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergey Zhemzhitsky (JIRA)" <j...@apache.org>
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
                 URL: https://issues.apache.org/jira/browse/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

{code}
package org.apache.camel.impl;

public class ReplaceEndpointStrategy implements EndpointStrategy {

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

    @Override
    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;
    }

}
{code}

Here is it can be used from spring

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

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

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="timer://testTimer" />
            <to uri="log://timerTick" />
        </route>
    </camelContext>

</beans>
{code}

And the unit test
{code}
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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ReplaceEndpointStrategyTest {

    @Autowired
    private CamelContext camelContext;

    @Autowired
    private ProducerTemplate producer;

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

    @Test
    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!");
        complete.assertIsSatisfied();
    }

}
{code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message