camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hadrian Zbarcea (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CAMEL-4371) Add an ability to replace endpoints
Date Fri, 02 Sep 2011 01:50:09 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-4371?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13095716#comment-13095716
] 

Hadrian Zbarcea commented on CAMEL-4371:
----------------------------------------

Hi Sergey, I do understand your problem, but I don't think your proposal is the right solution.
What you want is already possible in Camel in more than way. Adding this would mean adding
yet another way of achieving the same result, arguably not even in the best way. Hard-coding
an endpoint URIs in code and then replacing it at runtime with another URI is really not a
good practice. Better give them symbolic names that reflect semantics. Until somebody convinces
me otherwise I am -1 on this change.

@Ashwin, one problem with what I think you propose is identifying the boundaries of the route
segment. Another point is that if you have to mutate parts of the route, I would argue that
there's something wrong with the route design.

> 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
>            Assignee: Ashwin Karpe
>
> 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 files 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