Dynamic routing

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dynamic routing

Xybrek
Here's a typical Restlet application:

public class GenericBackendApplication extends SwaggerApplication {
     private static final String ROOT_URI = "/";
     public SampleApplication() {
     }
     public Restlet createInboundRoot() {
         Guice.createInjector(new Module[]{new
GuiceConfigModule(this.getContext()),
                new SelfInjectingServerResourceModule()});
         Router router = new Router(this.getContext());
         router.attach("/", GaeRootServerResource.class);
         router.attach("/ping", GaePingServerResource.class);
         return router;
     }
}

I wonder if its possible to "attach" ServerResources dynamically or at
least through some third-party library. My concern is that we are
building a "generic" backend jar which we want that users of the backend
to extend the same GenericBackendApplication adding resource without
editing or re-building it. Merely by using the backend jar as dependency
and adding Resources and injecting them dynamically to the Router. Is
this possible?

--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamic routing

Niclas Hedhman
The answer is "yes", you can modify Router on-the-fly, and I think(!) that it is even thread-safe to do so, although I guess that code isn't as heavily field tested as most other stuff.


AND if there is something that you want to do, that will break Restlet's existing parts, you can create your own Restlet subtypes and do whatever it takes. Restlet is in effect a big box of Unix Pipes, and up to you to plumb them together.

Note; I am not a Restlet team developer, just a user like you...

Cheers
Niclas


On Thu, Dec 3, 2015 at 4:03 PM, xybrek <[hidden email]> wrote:
Here's a typical Restlet application:

public class GenericBackendApplication extends SwaggerApplication {
    private static final String ROOT_URI = "/";
    public SampleApplication() {
    }
    public Restlet createInboundRoot() {
        Guice.createInjector(new Module[]{new GuiceConfigModule(this.getContext()),
                new SelfInjectingServerResourceModule()});
        Router router = new Router(this.getContext());
        router.attach("/", GaeRootServerResource.class);
        router.attach("/ping", GaePingServerResource.class);
        return router;
    }
}

I wonder if its possible to "attach" ServerResources dynamically or at least through some third-party library. My concern is that we are building a "generic" backend jar which we want that users of the backend to extend the same GenericBackendApplication adding resource without editing or re-building it. Merely by using the backend jar as dependency and adding Resources and injecting them dynamically to the Router. Is this possible?

--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].




--
Niclas Hedhman, Software Developer
http://zest.apache.org - New Energy for Java

--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamic routing

Xybrek
On Thursday, 03 December, 2015 04:18 PM, Niclas Hedhman wrote:

> The answer is "yes", you can modify Router on-the-fly, and I think(!)
> that it is even thread-safe to do so, although I guess that code isn't
> as heavily field tested as most other stuff.
>
>
> AND if there is something that you want to do, that will break Restlet's
> existing parts, you can create your own Restlet subtypes and do whatever
> it takes. Restlet is in effect a big box of Unix Pipes, and up to you to
> plumb them together.
>
> Note; I am not a Restlet team developer, just a user like you...
>
> Cheers
> Niclas
>
>
> On Thu, Dec 3, 2015 at 4:03 PM, xybrek <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Here's a typical Restlet application:
>
>     public class GenericBackendApplication extends SwaggerApplication {
>          private static final String ROOT_URI = "/";
>          public SampleApplication() {
>          }
>          public Restlet createInboundRoot() {
>              Guice.createInjector(new Module[]{new
>     GuiceConfigModule(this.getContext()),
>                      new SelfInjectingServerResourceModule()});
>              Router router = new Router(this.getContext());
>              router.attach("/", GaeRootServerResource.class);
>              router.attach("/ping", GaePingServerResource.class);
>              return router;
>          }
>     }
>
>     I wonder if its possible to "attach" ServerResources dynamically or
>     at least through some third-party library. My concern is that we are
>     building a "generic" backend jar which we want that users of the
>     backend to extend the same GenericBackendApplication adding resource
>     without editing or re-building it. Merely by using the backend jar
>     as dependency and adding Resources and injecting them dynamically to
>     the Router. Is this possible?
>
>     --
>     You received this message because you are subscribed to the Google
>     Groups "Restlet Framework (Discuss)" group.
>     To unsubscribe from this group and stop receiving emails from it,
>     send an email to [hidden email]
>     <mailto:framework-discuss%[hidden email]>.
>
>
>
>
> --
> Niclas Hedhman, Software Developer
> http://zest.apache.org - New Energy for Java
>
> --
> You received this message because you are subscribed to the Google
> Groups "Restlet Framework (Discuss)" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [hidden email]
> <mailto:[hidden email]>.

@Niclas, any hints on how to do this "on-the-fly"?

--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamic routing

Tim Peierls
First, I strongly recommend creating the injector in the constructor and saving a reference to it in a final field, otherwise there's no guarantee that it won't be garbage collected before you're done with it.

One question is whether the attach methods are thread-safe. I think they are: Router.attach uses CopyOnWriteArrayList.add under the covers, so it should be ok.

The detach methods are not thread-safe; I've created issue #1175 to report this. If they were, you could call attach and detach freely to change the routing structure on the fly.

As things stand, if you really want to be able to attach and detach dynamically, your best bet is to do all route structure manipulation in a dedicated thread. You could use a single-thread Executor for this, perhaps one associated with each Router. You could even write a Router subclass that would manage the details and hide them from users.

Note, however, that the semantics of detach are limited: When you detach by Class, you remove all routes matching that class, and when you detach by Restlet, you remove all routes using that Restlet. There's no way to remove by path.

It doesn't sound as though the original poster was interested in detach. If all you want to do is defer your calls to attach to some later point than the call to createInboundRoot, you should be fine.

But I don't think you need even that much, given the original motivation. If you want a generic backend application that users can extend to do their own routing, how about this:

public abstract class GenericBackendApplication extends SwaggerApplication {
    private final Injector injector;

    public GenericBackendApplication() {
        this.injector = Guice.createInjector(
            new GuiceConfigModule(this.getContext()),
            new SelfInjectingServerResourceModule()
        );
    }

    public Restlet createInboundRoot() {
        Router router = new Router(this.getContext());
        // ... do any routing common to all      ... 
        // ... generic backend applications here ...
        configureRouter(router);
        return router;
    }

    protected abstract void configureRouter(Router router);
}

public class GaeApplication extends GenericBackendApplication {
    // ...
    @Override
    protected void configureRouter(Router router) {
        router.attach("/", GaeRootServerResource.class);
        router.attach("/ping", GaePingServerResource.class);
    }
}


?

--tim


On Thu, Dec 3, 2015 at 8:41 PM, xybrek <[hidden email]> wrote:
On Thursday, 03 December, 2015 04:18 PM, Niclas Hedhman wrote:
The answer is "yes", you can modify Router on-the-fly, and I think(!)
that it is even thread-safe to do so, although I guess that code isn't
as heavily field tested as most other stuff.


AND if there is something that you want to do, that will break Restlet's
existing parts, you can create your own Restlet subtypes and do whatever
it takes. Restlet is in effect a big box of Unix Pipes, and up to you to
plumb them together.

Note; I am not a Restlet team developer, just a user like you...

Cheers
Niclas


On Thu, Dec 3, 2015 at 4:03 PM, xybrek <[hidden email]
<mailto:[hidden email]>> wrote:

    Here's a typical Restlet application:

    public class GenericBackendApplication extends SwaggerApplication {
         private static final String ROOT_URI = "/";
         public SampleApplication() {
         }
         public Restlet createInboundRoot() {
             Guice.createInjector(new Module[]{new
    GuiceConfigModule(this.getContext()),
                     new SelfInjectingServerResourceModule()});
             Router router = new Router(this.getContext());
             router.attach("/", GaeRootServerResource.class);
             router.attach("/ping", GaePingServerResource.class);
             return router;
         }
    }

    I wonder if its possible to "attach" ServerResources dynamically or
    at least through some third-party library. My concern is that we are
    building a "generic" backend jar which we want that users of the
    backend to extend the same GenericBackendApplication adding resource
    without editing or re-building it. Merely by using the backend jar
    as dependency and adding Resources and injecting them dynamically to
    the Router. Is this possible?

    --
    You received this message because you are subscribed to the Google
    Groups "Restlet Framework (Discuss)" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to [hidden email]
    <mailto:[hidden email]>.




--
Niclas Hedhman, Software Developer
http://zest.apache.org - New Energy for Java

--
You received this message because you are subscribed to the Google
Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [hidden email]
<mailto:[hidden email]>.

@Niclas, any hints on how to do this "on-the-fly"?


--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].


--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
Loading...