Antonio Foglia
Antonio Foglia

Reputation: 87

Call controller form another bean. No thread-bound request found

I'm developing a spring mvc and i want my controller to listen application events via @controller I send a jms message from a web page and I'm trying to notify the controller when receiving a message jms, in order to push some data to a web page

First I've tried with the observer pattern, implementing the controller as an ApplicationListener

@ Controller ("MyController") 
@ Scope (value = "session" = proxyMode ScopedProxyMode.TARGET_CLASS) 
public class MyController implements Serializable, ApplicationListener <CustomEvent>

Then I've tried to call a controller method from another bean when receiving a jms message

@Resource(name="MyController")
private MyController c;
c.update(String data);

But I'm getting always the same error

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.

Is there a way to call a controller from another bean? Thanks

Upvotes: 1

Views: 189

Answers (1)

axtavt
axtavt

Reputation: 242716

The problem is that your controller is session-scoped, and you try to call its method from a context where no HTTP session is available.

So, I assume that your controller combines functionality that depends on HTTP session (thus session scope), and functionality that need to be called outside of a session (such as your update() method).

If so, you can solve this problem by moving functionality that doesn't require HTTP session into separate bean (without session scope).

Upvotes: 1

Related Questions