Dominic Weiser
Dominic Weiser

Reputation: 1456

Vaadin 7.1.0 Push

I am new to Vaadin and just implement a Prototype for testing the functionality. Currently I play around with Vaadin Server-Push feature. I like to do a simple counter which can be increased by Button. After that I like to open this side from different Browser and when I count up I like all Browser to be updated.

For now I've got that the current Browser is updated by a separated Thread.

public class PushTestView extends VerticalLayout implements View {

   private static final long serialVersionUID = -8056849522760207972L;
   private Label counter;

   public void enter(ViewChangeEvent event) {
      removeAllComponents();
      PushTestController controller = PushTestController.getInstance();
      counter = new Label("" + controller.getCounter());
      Button button = new Button("Count up");

      button.addClickListener(new ClickListener() {
         private static final long serialVersionUID = -1437136914245802675L;

         @Override
         public void buttonClick(ClickEvent event) {
            InitializerThread thread = new InitializerThread();
            thread.run();
         }
      });

      addComponent(counter);
      addComponent(button);

   }

   class InitializerThread extends Thread {
      @Override
      public void run() {
         // Init done, update the UI after doing locking
         UI.getCurrent().access(new Runnable() {
            @Override
            public void run() {
               // Here the UI is locked and can be updated
               PushTestController.getInstance().countUp();
               counter.setValue("" + PushTestController.getInstance().getCounter());
               UI.getCurrent().notifyAll();
            }
         });
      }
   }
}

This view I embedded into my UI which is annotated by @Push. And here is my controller:

public class PushTestController {

   private static PushTestController instance = new PushTestController();
   private int counter = 0;

   private PushTestController() {
      //Private Constructor for Singleton 
   }

   public static PushTestController getInstance() {
      return instance;
   }

   public int getCounter() {
      return counter;
   }

   public String getCounterString() {
      return "" + counter;
   }

   public void countUp() {
      counter++;
   }
}

How do I have to extend this example that it works to update the UI in different browsers?

Upvotes: 2

Views: 3592

Answers (2)

Gabor
Gabor

Reputation: 61

Take a look at this one, section 11.16.3: https://vaadin.com/book/vaadin7/-/page/advanced.push.html

Upvotes: 3

Slenkra
Slenkra

Reputation: 830

You have to use servlet 3.0 on your project and edit your ivy.xml(or if you use Maven then pom.xml). After that you have to add the @Push annotation to enable push for the ui. Something like this:

<dependency org="com.vaadin" name="vaadin-push" rev="&vaadin.version;" conf="default->default" />

in Maven:

<dependency>
  <groupId>com.vaadin</groupId>
  <artifactId>vaadin-push</artifactId>
  <version>${vaadin.version}</version>
</dependency>

The servlet configuration:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    <init-param>
        <param-name>ui</param-name>
        <param-value>org.vaadin.example.MyUI</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>

And at last the @Push

@Push 
public class PushTestUI extends UI 

Reference here

Upvotes: 2

Related Questions