Cataclysm
Cataclysm

Reputation: 8618

Custom Listeners with detach() method error

I would like to know about Vaadin's detach() method. How can I understand below definition from API ?

Called before the UI is removed from the session.

I got a problem when creating custom listener such as BroadCaster .

MyCustomListener.java

public interface MyCustomListener {
void fireEvent(MyCustomEvent event);
}

MyCustomEvent.java

public class MyCustomEvent {
private String message;

public MyCustomEvent(final String message) {
    this.message = message;
}

public final String getMessage() {
    return message;
}
}

MyCustomDispatcher.java

public final class MyCustomDispatcher {
private static LinkedList<MyCustomListener> customListeners = new LinkedList<MyCustomListener>();
private static ExecutorService executorService = Executors.newSingleThreadExecutor();

private MyCustomDispatcher() {

}

public static synchronized void register(final MyCustomListener listener) {
    customListeners.add(listener);
}

public static synchronized void unregister(final MyCustomListener listener) {
    customListeners.remove(listener);
}

public static synchronized void invokeMyCustomEvent(final String message) {
    if (message == null || message.trim().length() <= 0) {
        return;
    }

    for (final MyCustomListener listener : customListeners) {
        executorService.execute(new Runnable() {
            public void run() {
                listener.fireEvent(new MyCustomEvent(message));
            }
        });
    }
}
}

I call this listener from my UI class as ...

public class HelloWorldUI extends UI implements MyCustomListener {

@Override
protected void init(VaadinRequest request) {
    System.out.println("Getting initialized !");
    MyCustomDispatcher.register(this);
    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);
    setSizeFull();
    layout.addComponent(new Label("Hello World !"));
}

@Override
public void detach() {
    System.out.println("Listener was Unregister !");
    MyCustomDispatcher.unregister(this);
    super.detach();
}

@Override
public void fireEvent(MyCustomEvent event) {
    // Do Something
}
}

I call unregister() method of my custom listener inside detach() method for

  1. from some examples for custom listener
  2. to avoid receiving messages for UIs no longer in use (and ensuring that the detached UI can be garbage collected).
  3. Cleaning up resources in a UI

My problem was due to detach() method because when I refreshed my browser , my listener instance was deleted (from detach() method). So , I can't get fireEvent() anymore. I debugged , detach() method was called after init() method of my UI when refreshing browser. But if I remove calling unregister(MyCustomListener listener) from detach() method , that may cause nesting events (previous listeners were still alive).

What am I wrong ? How can I fix it ? Any suggestions ?

Upvotes: 1

Views: 1272

Answers (1)

Cataclysm
Cataclysm

Reputation: 8618

Sorry ! this is stupid question . Vaadin's component were server-side codes and I should avoid using static as I much as I can. When I am using my custom listeners as static-resources , these events were share all others. If someone invokes one event , every users will get same.

Static collection of listeners (sharing events) may only suitable for server-push.

I shouldn't create custom listeners as like this. Thanks @HenriKerola for explanation of using static fields in vaadin and about the creating new UI instance when browser was refresh.

Upvotes: 1

Related Questions