0

I am using a SpringClientFactory to create a Ribbon client with a custom com.netflix.niws.client.http.RestClient. This RestClient is wired with a @Bean that is request scoped (that is, @Scope("request")). This is all done as a part of the Brixton release train.

RibbonClientSpecification customSpec = new RibbonClientSpecification("default.custom",
    new Class<?>[]{CustomConfig.class}); // defines custom RestClient
SpringClientFactory scf = new SpringClientFactory();
scf.setConfigurations(Arrays.asList(customSpec));

This SpringClientFactory has an ApplicationContext that it instantiates on its own for a given name, and this context doesn't have any Scopes registered with it.

I have a workaround that extends the SpringClientFactory, but it feels a bit clunky, and it makes assumptions regarding the type of BeanFactory that is available.

class ScopePropagatingSpringClientFactory extends SpringClientFactory {
    protected AnnotationConfigApplicationContext createContext(String name) {
        AnnotationConfigApplicationContext context = super.createContext(name);

        ApplicationContext parentContext = context.getParent();
        if (parentContext != null) {
            ConfigurableListableBeanFactory parentBF =
                (ConfigurableListableBeanFactory) parentContext.getAutowireCapableBeanFactory();
            String[] scopes = parentBF.getRegisteredScopeNames();
            for(String scope : scopes) {
                context.getBeanFactory().registerScope(scope, parentBF.getRegisteredScope(scope));
            }
        }

        return context;
    }
}

Has anyone tried using alternative bean scopes (request, session, globalsession) within a SpringClientFactory?

This one is more directed at the Spring Cloud maintainers: Is there a conscious reason that scopes are not propagated from the parent context to the ones created within the NamedContextFactory? It seems like it might just be an oversight.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • No one had ever tried creating a request scope bean. My question, is why do you want a request scoped `RestClient`? – spencergibb Jun 24 '16 at 02:37
  • Not a request scoped `RestClient`, but a `RestClient` that utilizes a request scoped bean. The primary goal being to get access to parts of the request that came into this service, without having to pass the structure from call to call. Ultimately, info from the request would be used to 'override' the ribbon setup to not call eureka in certain situations, but instead use a provided host. – nicholas.hauschild Jun 24 '16 at 03:40
  • Where are you doing this from, zuul or RestTemplate, or something else? – spencergibb Jun 24 '16 at 03:58
  • The `SpringClientFactory` is being used within a `RestTemplate`. I will be doing something similar for Zuul, but that approach won't need the request scoped bean, as I should be able to get by with the `RequestContext` object Zuul has. – nicholas.hauschild Jun 24 '16 at 04:08
  • Spring has a thread local too. RequestAttributes or something similar. – spencergibb Jun 24 '16 at 04:14
  • Zuul should just route with a different filter. – spencergibb Jun 24 '16 at 04:15

0 Answers0