camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Gargan <steve.gar...@gmail.com>
Subject Re: How to update/remove camel route
Date Fri, 29 Jan 2010 19:05:41 GMT
Ricardo,

What you're doing is very possible but you'll need to modify how you
are doing it. Routes are not really meant to be modified and restarted
like that, the setters are mostly for camel to initialize them in the
first place. Typically you use RouteBuilders to create and initialize
new Routes. Using the old RouteDefinition in a RouteBuilder to create
a new Route should give you what you need.

You are supplying each Route with is own id so, as Claus suggests, you
can use the methods on the CamelContext to stop the existing route.
Then use a RouteBuilder to create the new Route and add this back to
the context. The following test illustrates a basic solution.
Depending on how complex your route is, you'll need to do more work
when you create the new route from the old one. I suggest that you try
and keep this as simple as possible.

Give this a try and see how it works for you

rgds,

ste

public class DynamicRoutesTest extends CamelTestSupport {

    @Test
    public void routesCanBeCreatedDynamically() throws Exception {
        MockEndpoint mock = setExpectedMessagesToMock("mock:out-endpoint", 1);

        context.addRoutes(createDynamicRoute("dynamicRouteId",
"direct:in", "mock:out-endpoint"));

        sendBody("direct:in", "payload to dynamic endpoint");

        assertSatisfied(mock);
    }

    @Test
    public void routesCanBeUpdatedDynamicallyToo() throws Exception {
        // we're going to change the destination endpoint so the old
one should receive no exchanges and the new on one.
        MockEndpoint oldDestination =
setExpectedMessagesToMock("mock:out-endpoint", 0);
        MockEndpoint newDestination =
setExpectedMessagesToMock("mock:new-out-endpoint", 1);

        context.addRoutes(createDynamicRoute("dynamicRouteId",
"direct:in", "mock:out-endpoint"));

        //stop the route by its id
        context.stopRoute("dynamicRouteId");

        context.addRoutes(redirectTrafficToDifferentEndpoint("dynamicRouteId",
"mock:new-out-endpoint"));

        sendBody("direct:in", "payload to dynamic endpoint");

        assertSatisfied(oldDestination, newDestination);

    }

    public RouteBuilder createDynamicRoute(final String id, final
String uri, final String touri) {
        return new RouteBuilder() {
            public void configure() throws Exception {
                from(uri).id(id).to(touri);
            }
        };
    }

    public RouteBuilder redirectTrafficToDifferentEndpoint(final
String id, final String newDestination) {
        return new RouteBuilder() {
            public void configure() throws Exception {
                RouteDefinition old = getContext().getRouteDefinition(id);

                for (FromDefinition from : old.getInputs()) {
                    from(from.getUri()).id(old.getId()).to(newDestination);
                }
            }
        };
    }

    private MockEndpoint setExpectedMessagesToMock(String mockUri, int
expectedMessages) {
        MockEndpoint mock = getMockEndpoint(mockUri);
        mock.expectedMessageCount(expectedMessages);
        return mock;
    }

    private void assertSatisfied(MockEndpoint... mocks) throws
InterruptedException {
        for (MockEndpoint mock : mocks) {
            mock.await(1, TimeUnit.SECONDS);
            mock.assertIsSatisfied();
        }
    }
}

On Fri, Jan 29, 2010 at 3:52 AM, Ricardo Melo <ricardo@cflex.com.br> wrote:
> Hi ste,
>
> Thank you for the response.
>
> I'm developing a system where routes destination endpoint can be
> added/updated/removed dynamically. A received XML message specifies the
> operation, a route identifier name and the route destination endpoint
> address. After this, the camel route from the camel context is
> updated/removed. The routes are also present in a database, which have its
> routes added/updated/removed from.
>
> That's why I need to remove or update the route. Adding is pretty easy, but
> for updating/removing I need to first find the route using the previously
> specified address.
>
> I've tried something like the code below, which is not working (the route is
> not being updated for some reason. I'm using the setURI() method):
>
> [CODE]
>
> List<RouteDefinition> routes = camelContext.getRouteDefinitions();
>
> // Searches for the route to be updated
> for( int i = 0 ; i < routes.size() ; i++ ) {
>
> RouteDefinition routeToUpdate = routes.get(i);
>
> /*
> * Look for the correct route to update.
> * The route to be updated is
> * [FROM=activemq:queue:out_<routeId>] -> [TO:<newRouteDestination>]
> */
>
> // Look for the route that starts with : [FROM=activemq:queue:out_<routeId>]
> if ( SearchUtil.find(
> ((FromDefinition) routeToUpdate.getInputs().get(0)).getLabel() ,
> "out_" + newCommunicationRoute.getId() ) == true ) {
>
> // Found! Update the destination. First stops the route, update it, then
> restarts it
> camelContext.stopRoute( routeToUpdate );
>
> // Update destination [TO:<newRouteDestination>]
> ((SendDefinition<ProcessorDefinition>)
> routeToUpdate.getOutputs().get(0)).setUri(
> newConfiguration.getTransp() + newConfiguration.getParam());
>
> // restart it
> camelContext.startRoute( routeToUpdate );
>
> [/CODE]
>
> Strangely, the printed result of
>
> (SendDefinition<ProcessorDefinition>) routeToUpdate.getOutputs().get(0)
>
> is differente from
>
> ((SendDefinition<ProcessorDefinition>) routeToUpdate.getOutputs().get(0))
>                        .getUri()
> .
>
> The getUri() returns something that seems not to be used, and the route is
> not updated.
>
> About the route removal, I didn't tried yet.
>
> Thank you,
> Ricardo
>
>
>
>
>
>
> On Thu, Jan 28, 2010 at 6:32 PM, Stephen Gargan <steve.gargan@gmail.com>wrote:
>
>> Ricardo,
>>
>> What are you trying to do? Are you trying to programmatically stop a
>> route from processing by removing it? If so try specifying a
>> RoutePolicy instead. If you can explain your intention a little more
>> we can definitely suggest options.
>>
>> rgds,
>>
>> ste
>>
>> On Thu, Jan 28, 2010 at 10:37 AM, Ricardo Melo <ricardo@cflex.com.br>
>> wrote:
>> > Hi,
>> >
>> > I'm new to camel API and I need to update or remove camel routes that
>> have
>> > been created.
>> >
>> > I've found in an example that to find current routes, I need to use the
>> > code:
>> > List<RouteDefinition> routes = camelContext.getRouteDefinitions();
>> >
>> > I can iterate through the routes definition, however how can I see the
>> "to"
>> > (destination) endpoint of a route definition, to update or remove the
>> route
>> > I'm looking for? Where is it?
>> >
>> > Thanks in advance!
>> > --
>> > Ricardo Britto Melo
>> >
>> > CFlex - Empower your Decisions
>> > Tel: (+55 19) 3251-5211
>> > Rua Barão de Paranapanema, 401A
>> > Campinas - SP
>> > www.cflex.com.br
>> >
>>
>
>
>
> --
> Ricardo Britto Melo
>
> CFlex - Empower your Decisions
> Tel: (+55 19) 3251-5211
> Rua Barão de Paranapanema, 401A
> Campinas - SP
> www.cflex.com.br
>

Mime
View raw message