Dispatcher Servlet and Vaadin Servlet

Hello,

I’m trying to integrate Spring Dispatcher Servlet with Vaadin Servlet but I cannot find any information how to do it. So I decided to ask you guys for help. Any idea’s how to do it ?

Regards, Satrix

Hi,

It’s really not possible to use the Spring Dispatcher servlet to run a Vaadin application; they are chalk-and-cheese.

However, you can (and we are) use the
org.springframework.web.context.request.RequestContextListener
in order to use session scoped beans.

Cheers,

Charles.

Hi Charles,

Yes Im using this but the problem is when you have Spring Session Bean and you try to use it in background thread. It ends up with an exception.

Regards, Satrix

Ok,

what Exception is thrown? How are you trying to access the session-scoped bean?

Could you please give us a little more background information?

Cheers,

Christoph

The exception:
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

However I have RequestContextListener in my web.xml.

Let’s say I have example like this:


@Configurable(preConstruction = true)
class Example {
     @Autowired
     private ProductCarryBean carryBean;

     public void test(){
          new Thread(new Runnable() {

                public void run() {
                     carryBean.doSmth();
                }
          }).start();
     }
}

Everything works like a charm when I use this bean in main thread. But whenever I try to use it in background thread it throws me above exception.

Regards, Satrix

Ok,

because you provided so less, I have to guess.

Maybe you are working according to this:

https://vaadin.com/de/wiki/-/wiki/Main/Spring%20Integration

So your
ExampleBean
ist scoped
prototype
( Default for AspectJ based @Configurable bean creation), so I’m guessing your
carryBean
is scoped
session
with propably aop:scoped-proxy?

I have java based spring configuration:


@Configuration
public class CommonBeanConfiguration {
    @Bean
    @Scope(value = "session")
    public ProductCarryBean productCarryBean(){
        return new ProductCarryBean();
    }
}

My project is based on java config. My xml configuration:


    <tx:annotation-driven/>
    
    <context:spring-configured />

    <context:component-scan base-package="example" />
    
    <task:annotation-driven />

Regards, Satrix

Ok, the problem is that in your case spring automatically uses an scoped-proxy to access the carryBean.
But because your thread isn’t session-bound the proxy can’t find the implementing carryBean.

You have to options:

1.) Get rid of the asynchronous thread
or
2.) use the carryBean only as a wrapper


@Configurable(preConstruction = true)
class Example {
     @Autowired
     private ProductCarryBean carryBean;

     @Autowired
     private SomeSingletonLogicBean logicBean;

     public void test(){
          final Payload payload = carryBean.getPayload();
          new Thread(new Runnable() {

                public void run() {
                     logicBean.doSmth(payload);
                }
          }).start();
     }
}

Thank you very much Christoph.

Hi,

Christoph is right; essentially, if you start a background thread, you don’t have an HttpSession, so Spring can’t access the session to retrieve the object. You should pass the session bean into the Runnable, or declare as final as Christoph suggests.

You should also be aware that any changes you make to the state of the session bean in the “background thead” may not be stored in the session for reasons that may be a little complex to explain here.

Cheers,

Charles.