Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@SessionScoped injection not working with @Push enabled (8.0.3+) #8967

Closed
ricardovm opened this issue Mar 28, 2017 · 11 comments
Closed

@SessionScoped injection not working with @Push enabled (8.0.3+) #8967

ricardovm opened this issue Mar 28, 2017 · 11 comments
Labels
Stale Stale bot label

Comments

@ricardovm
Copy link

ricardovm commented Mar 28, 2017

  • Vaadin version: 8.0.3+ and WildFly 10.1.0

  • Description of the bug

Using CDI, injection of a SessionScoped bean not working on events. Until 8.0.2, using a session bean on listeners works fine. But since 8.0.3 it doesn't work anymore. More precisely, since new Atmosphere (2.4.5.vaadin2) on #8785

Without @Push it works.

14:33:16,090 SEVERE [com.vaadin.server.DefaultErrorHandler] (default task-9) : org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.SessionScoped
	at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:689)
	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:90)
	at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:165)
	at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
	at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
	at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
	at com.linkhos.tests.sessions.UserSession$Proxy$_$$_WeldClientProxy.getHello(Unknown Source)
	at com.linkhos.tests.sessions.MainUI.lambda$init$6de4602$1(MainUI.java:33)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:211)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:174)
	at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1029)
	at com.vaadin.ui.Button.fireClick(Button.java:370)
	at com.vaadin.ui.Button$1.click(Button.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155)
	at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:445)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:410)
	at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274)
	at com.vaadin.server.communication.PushHandler.lambda$new$71(PushHandler.java:145)
	at com.vaadin.server.communication.PushHandler.callWithUi(PushHandler.java:235)
	at com.vaadin.server.communication.PushHandler.onMessage(PushHandler.java:489)
	at com.vaadin.server.communication.PushAtmosphereHandler.onMessage(PushAtmosphereHandler.java:87)
	at com.vaadin.server.communication.PushAtmosphereHandler.onRequest(PushAtmosphereHandler.java:77)
	at org.atmosphere.cpr.AsynchronousProcessor.action(AsynchronousProcessor.java:223)
	at org.atmosphere.cpr.AsynchronousProcessor.suspended(AsynchronousProcessor.java:115)
	at org.atmosphere.container.Servlet30CometSupport.service(Servlet30CometSupport.java:68)
	at org.atmosphere.cpr.AtmosphereFramework.doCometSupport(AtmosphereFramework.java:2284)
	at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:593)
	at org.atmosphere.websocket.DefaultWebSocketProcessor$3.run(DefaultWebSocketProcessor.java:345)
	at org.atmosphere.util.VoidExecutorService.execute(VoidExecutorService.java:101)
	at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:340)
	at org.atmosphere.websocket.DefaultWebSocketProcessor.invokeWebSocketProtocol(DefaultWebSocketProcessor.java:447)
	at org.atmosphere.container.JSR356Endpoint$3.onMessage(JSR356Endpoint.java:260)
	at org.atmosphere.container.JSR356Endpoint$3.onMessage(JSR356Endpoint.java:257)
	at io.undertow.websockets.jsr.FrameHandler$7.run(FrameHandler.java:283)
	at io.undertow.websockets.jsr.ServerWebSocketContainer$1.call(ServerWebSocketContainer.java:162)
	at io.undertow.websockets.jsr.ServerWebSocketContainer$1.call(ServerWebSocketContainer.java:159)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
	at io.undertow.websockets.jsr.ServerWebSocketContainer.invokeEndpointMethod(ServerWebSocketContainer.java:575)
	at io.undertow.websockets.jsr.ServerWebSocketContainer$6.run(ServerWebSocketContainer.java:561)
	at io.undertow.websockets.jsr.OrderedExecutor$ExecutorTask.run(OrderedExecutor.java:67)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
  • If possible, minimal reproducible example
@SessionScoped
public class MySessionScoped {
    private Instant sessionInit = Instant.now();

    public String getHello() {
        return "Hello! Session started at: " + sessionInit;
    }
}

@CDIUI("main")
@Push
public class MyUI extends UI
    @Inject
    MySessionScoped bean;

    protected void init(VaadinRequest request) {
        // This works
        Label helloLabel = bean.getHello();

        // This listener doesn't work
        Button showButton = new Button("Show", e -> helloLabel.setValue(bean.getHello()));

        setContent(new VerticalLayout(helloLabel, showButton));
}

A complete example: https://github.com/ricardovm/session-problem-vaadin

@Artur-
Copy link
Member

Artur- commented Mar 28, 2017

The example project uses 8.0.2 which suffers from #8734 and therefore websockets do not work. It will fallback to long polling, which makes session scoped beans work because long polling uses normal HTTP requests.

For 8.0.3+ websockets will work as expected, with the drawback that websockets do not enable SessionScope. See vaadin/cdi#88 and the spec issue at https://java.net/jira/browse/WEBSOCKET_SPEC-196

@ricardovm
Copy link
Author

In most situations, @UIScoped should be enough. If it's a CDIxWS spec problem, I'll go with UIScoped until they solve this. Is it possible to display this in a exception message and documentation?

@Artur-
Copy link
Member

Artur- commented Mar 29, 2017

Although I am not sure, I would be surprised if @UIScoped would work as UIs are part of the session. The workaround for this is to use @Push(transport=WEBSOCKET_XHR) or alternatively transport=LONG_POLLING - with these options the client->server requests are sent as HTTP requests and all CDI scopes work as expected

@ricardovm
Copy link
Author

@UIScoped works fine. VaadinService.getCurrentRequest().getWrappedSession() works too. Just @SessionScoped don't.

@mrts
Copy link

mrts commented Apr 16, 2017

@Artur-, in vaadin/cdi#88 @hesara wrote

Known CDI issues with push (including with web sockets) have been resolved. Note that this only concerns @UIScoped and @ViewScoped beans - request and session scopes are not supported with web sockets in CDI 1.0.

So one would presume that @UIScoped should work, but it would be great to get a confirmation for that. It's also used in the official CDI tutorial along with @Push (where UserInfo is @UIScoped).

Simple full @Push + @UIScoped injection example with Vaadin 8.0.4 that seems to work is available here. (There was originally a problem in the example caused by not having bean-discovery-mode="all" in beans.xml, fixed now.)

@mrts
Copy link

mrts commented Aug 21, 2017

Note that there is an ongoing discussion how to get this fixed in Java EE 9 here:
https://javaee.groups.io/g/websocket-spec/topic/websocket_spec_security_and/5842413

@Artur-, could Vaadin representatives also voice their perspective in the discussion?

@mrts
Copy link

mrts commented Jan 22, 2018

Note that you can use @VaadinSessionScoped since CDI 3.0.0 which is compatible with at least @Push(transport = Transport.WEBSOCKETS_XHR) (tested, but maybe even with WEBSOCKET)

@stale
Copy link

stale bot commented Jun 21, 2018

Hello there!

It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.

There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):

  • Check if the issue is still valid for the latest version. There are dozens of duplicates in our issue tracker, so it is possible that the issue is already tackled. If it appears to be fixed, close the issue, otherwise report to the issue that it is still valid.
  • Provide more details how to reproduce the issue.
  • Explain why it is important to get this issue fixed and politely draw others attention to it e.g. via the forum or social media.
  • Add a reduced test case about the issue, so it is easier for somebody to start working on a solution.
  • Try fixing the issue yourself and create a pull request that contains the test case and/or a fix for it. Handling the pull requests is the top priority for the core team.
  • If the issue is clearly a bug, use the Warranty in your Vaadin subscription to raise its priority.

Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!

@stale stale bot added the Stale Stale bot label label Jun 21, 2018
@TatuLund
Copy link
Contributor

I closed this, since CDI add-on 3.0.0 provides workaround via @VaadinSessionScoped and #8734 and vaadin/cdi#88 are closed.

@janhuba
Copy link

janhuba commented Nov 26, 2020

I have never seen the example how to workaround @CDIView in Vaadin 8 with @VaadinSessionScoped. Can you pls send a link?

@TatuLund
Copy link
Contributor

Here is an example app, which uses it, i.e. there is a bean with @VaadinSessionScoped annotation

https://github.com/TatuLund/cdi-demo/blob/master/src/main/java/org/vaadin/cdidemo/data/UserProfileHolderImpl.java#L16

and it is used for example here

https://github.com/TatuLund/cdi-demo/blob/master/src/main/java/org/vaadin/cdidemo/views/login/LoginPresenter.java#L27

I recommend to download the whole app and play with it, it demonstrates the most typical CDI cases with Vaadin app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Stale bot label
Projects
None yet
Development

No branches or pull requests

5 participants