Claus and Willem, thank you for the insights... below I wrote about the
solution I implemented, I appreciate if you could give your comments about
it :)


On Thu, Oct 18, 2012 at 3:37 PM, Claus Ibsen <claus.ib...@gmail.com> wrote:

> On Thu, Oct 18, 2012 at 3:35 PM, Henrique Viecili <henri...@myreks.com>
> wrote:
> > Claus,
> >
> > what I am doing is a Dynamic Recipient List where I must register these
> > 'services', when a service is started it sends its routing configuration
> to
> > the DRL and when it's shut down it tells the DRL the service is offline.
> I
> > believe the most appropriate is the shutdown from the CamelContext.
> >
> > I was looking for something like:
> >
> > <route id="service-shutdown-route" autoStartup="false"
> *onShutdown="true"* >
> >   <from uri="timer:runOnce?delay=0&amp;repeatCount=1" />
> >   ...
> >   <to uri="jms:DRLControlChannel" />
> > </route>
> >
>
> And how should Camel be able to run this route when its being told to
> shutdown itself?
>

It seems a paradox, and I see it can also be a bit dangerous/unsafe to add
this behaviour. But what I ended up doing was to create my own
ShutdownStrategy:

public class StartRoutesOnShutdownStrategy extends DefaultShutdownStrategy {

    private List<String> startRoutesOnShutdown;

    @Override
    protected boolean doShutdown(final CamelContext context,
List<RouteStartupOrder> routes, long timeout, TimeUnit timeUnit, boolean
suspendOnly, boolean abortAfterTimeout) throws Exception {
        List<String> stRoutes = getStartRoutesOnShutdown();

        if (!stRoutes.isEmpty()) {
            ExecutorService threadPool =
Executors.newFixedThreadPool(stRoutes.size());
            for (final String routeId : stRoutes) {
                threadPool.execute(new Runnable() {
                    public void run() {
                        try {
                            ServiceStatus routeStatus =
context.getRouteStatus(routeId);
                            if (routeStatus.isStopped() &&
routeStatus.isStartable()) {
                                context.startRoute(routeId);
                            }
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            }
        }

        return super.doShutdown(context, routes, timeout, timeUnit,
suspendOnly, abortAfterTimeout);
    }

}

and then, configured my camel-context.xml:

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

    <bean id="ShutdownStrategy"
class="com.package.StartRoutesOnShutdownStrategy">
        <property name="startRoutesOnShutdown">
            <list><value
type="java.lang.String">deregistration-route</value></list>
        </property>
    </bean>

    <camel:camelContext >

        <!-- Service Registration -->
        <camel:route id="registration-route">
            <camel:from uri="timer:runOnce?delay=5000&amp;repeatCount=1"/>
            <camel:to
uri="log:send-configuration-message-to-REGISTER-service" />
        </camel:route>
        <!-- Service Deregistration -->
        <camel:route id="deregistration-route" autoStartup="false"
shutdownRoute="Defer">
            <camel:from uri="timer:runOnce?delay=0&amp;repeatCount=1"/>
            <camel:to
uri="log:send-configuration-message-to-DEREGISTER-service" />
        </camel:route>

    </camel:camelContext>
</beans>

And it works the way I intened too.


> With the <bean> you can use the depends-on="myCamel" attribute and
> refer to the <camelContext id="myCamel" ...">.
> Which will tell Spring to invoke destroy on the <bean> before Camel.
> Then you got your shutdown signal.
>
> From the bean you can send a message to a route, or an endpoint or
> whatever you want.
>
> >
> > *Henrique Viecili*
> >
> >
> > On Thu, Oct 18, 2012 at 12:56 AM, Claus Ibsen <claus.ib...@gmail.com>
> wrote:
> >
> >> On Mon, Oct 15, 2012 at 11:27 PM, Henrique Viecili <henri...@myreks.com
> >
> >> wrote:
> >> > Hello guys...
> >> >
> >> > I've created a service that register itself on startup using
> camel-timer
> >> > component and now I want to make it unregister itself on shutdown. I
> >> know I
> >> > could do it registering a Shutdown Hook in the JVM or coding my own
> >> > ShutdownStrategy but I am looking for a simple solution preferably
> using
> >> > Spring DSL. Does anyone have done this before?
> >> >
> >> > Thanks,
> >> > *Henrique Viecili*
> >>
> >> What is your "service". And what do you mean by unreigstering itself
> >> on shutdown?
> >> From what should it unregister, and when you say shutdown, it is the
> >> CamelContext being stopped or the JVM itself etc.?
> >>
> >> And with Spring you can simple just do
> >> <bean id="foo" class="xxx" init-method="startMe"
> destroy-method="killMe"/>
> >>
> >> The names of the attributes, you would need to check the spring docs
> >> to make sure, as I am not sure if init-method etc is the correct name.
> >>
> >>
> >> --
> >> Claus Ibsen
> >> -----------------
> >> Red Hat, Inc.
> >> FuseSource is now part of Red Hat
> >> Email: cib...@redhat.com
> >> Web: http://fusesource.com
> >> Twitter: davsclaus
> >> Blog: http://davsclaus.com
> >> Author of Camel in Action: http://www.manning.com/ibsen
> >>
>
>
>
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Email: cib...@redhat.com
> Web: http://fusesource.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>

Reply via email to