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

Spring @EventListener support in component #272

Open
Tracked by #4379
denis-anisimov opened this issue Apr 18, 2018 · 5 comments
Open
Tracked by #4379

Spring @EventListener support in component #272

denis-anisimov opened this issue Apr 18, 2018 · 5 comments

Comments

@denis-anisimov
Copy link

@heruan commented on Wed Jan 03 2018

When building a PWA with Flow and Spring, I'd like my components able to react on server events using Spring's @EventListener and @TransactionalEventListener. For example:

@Route("users")
public class UserList extends Grid<User> {

    // ...

    @EventListener
    public void onNewUser(NewUserEvent event) {
        UI.getCurrent().access(getDataProvider()::refreshAll);
    }

}

At the moment it seems like @EventListener-annotated methods in @Route-annotated components are not invoked when the observed event is published.

@zach-herridge
Copy link

Is there a work around to make this work currently?

@heruan
Copy link
Member

heruan commented Jul 23, 2018

The problem here is that component instances are created at each page load, while event listeners are singletons. I still can't imagine how components might be also event listeners, and now I feel it could be an anti-pattern.

More than a workaround, I think it would be more correct to delegate the listener method to a singleton service (i.e. a Spring @Service bean) and "subscribe" the component to the service.

I've currently implemented this using the Java Flow (not Vaadin's Flow 🙂) API, i.e. java.util.concurrent.Flow.Publisher and java.util.concurrent.Flow.Subscriber.

@d2k2-git
Copy link

The problem here is that component instances are created at each page load, while event listeners are singletons. I still can't imagine how components might be also event listeners, and now I feel it could be an anti-pattern.

More than a workaround, I think it would be more correct to delegate the listener method to a singleton service (i.e. a Spring @Service bean) and "subscribe" the component to the service.

I've currently implemented this using the Java Flow (not Vaadin's Flow 🙂) API, i.e. java.util.concurrent.Flow.Publisher and java.util.concurrent.Flow.Subscriber.

i am currently using googles EventBus, which does the job. But it's not recommended to use by the google devs. so i was looking for a simple alternative. do you have some code examples how to implement the "Flow.Subscriber" in a vaadin route?

@petersj-ess
Copy link

Is there actually a workaround for this? It's still an issue today, what's the best practice for sending events to a view?

Seems like the Spring event listeners would be perfect to use with Vaadin

@TatuLund
Copy link

The problem here is that component instances are created at each page load, while event listeners are singletons.

@petersj-ess I think the ticket should be closed, as this is fundamentally just how Spring works. The solution is to use some EventBus instead of Spring events. Naturally use that EventBus as a proxy. I.e. listen Spring events elsewhere, and post another event to EventBus there and listen to EventBus in the view.

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

No branches or pull requests

7 participants