Hello, possibility to push in grid archetype-application-example ??

I’m trying to push with this example application, but I do not repaint the graph, it’s very strange the push does not work for me.

Neither with a simple label

https://github.com/vaadin/archetype-application-example/blob/master/mockapp-ui/src/main/java/org/vaadin/mockapp/samples/MainScreen.java

The strange thing is that I have not been able to include the vaadinCharts in the example of
https://demo.vaadin.com/charts/#SplineUpdatingEachSecond



public  class Hilo extends Thread {
        
        @Override
        public void run() {
            while(true) {
                try {
                    Thread.sleep(2000);
                    
                    UI.getCurrent().access(new Runnable() {
                        @Override
                        public void run() {
                            labelHora.setValue("Ahora: "+Instant.now());
                        }
                        
                    });
                    
                }catch(InterruptedException ez) {
                    ez.printStackTrace();
                }
            }
        }
            
    }

And this other method works for me in other projects, very well

but in this example of application no.

 runWhileAttached(chart, new Runnable() {

            @Override
            public void run() {
                final long x = System.currentTimeMillis();
                final double y = random.nextDouble();
                series.add(new DataSeriesItem(x, y), true, true);
            }
        }, 1000, 1000);

        configuration.setSeries(series);

        chart.drawChart(configuration);
        return chart;
    }

    public static synchronized void runWhileAttached(final Component component, final Runnable task, final int interval,
            final int initialPause) {
        // Until reliable push available in our demo servers
        GoliatUI.get().setPollInterval(interval);

        final Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(initialPause);
                    while (component.getUI() != null) {
                        Future<Void> future = component.getUI().access(task);
                        future.get();
                        Thread.sleep(interval);
                    }
                } catch (InterruptedException e) {
                } catch (ExecutionException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING,
                            "Stopping repeating command due to an exception", e);
                } catch (com.vaadin.ui.UIDetachedException e) {
                } catch (Exception e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING,
                            "Unexpected exception while running scheduled update", e);
                }
                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Thread stopped");

            };
        };
        thread.start();

    }




abr 24, 2017 1:45:09 AM com.vaadin.server.DefaultDeploymentConfiguration checkProductionMode
ADVERTENCIA: 
=================================================================
Vaadin is running in DEBUG MODE.
Add productionMode=true to web.xml to disable debug features.
To show debug window, add ?debug to your application URL.
=================================================================
abr 24, 2017 1:45:09 AM com.vaadin.server.communication.PushAtmosphereHandler onStateChange
ADVERTENCIA: AtmosphereHandler.onStateChange called before PushHandler has been set. This should really not happen
abr 24, 2017 1:45:09 AM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Cluster created with settings {hosts=[localhost:27017]
, mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
abr 24, 2017 1:45:09 AM org.mongodb.morphia.logging.MorphiaLoggerFactory chooseLoggerFactory
INFORMACIÓN: LoggerImplFactory set to org.mongodb.morphia.logging.jdk.JDKLoggerFactory
abr 24, 2017 1:45:09 AM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Opened connection [connectionId{localValue:1, serverValue:47}]
 to localhost:27017
abr 24, 2017 1:45:09 AM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 12]
}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=503015}
abr 24, 2017 1:45:10 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.1.0.CR1
abr 24, 2017 1:45:10 AM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Opened connection [connectionId{localValue:2, serverValue:48}]
 to localhost:27017
abr 24, 2017 1:45:11 AM com.guayoyolabs.goliat.MyChart$2 run
INFORMACIÓN: Thread stopped

Hi,

Push should work.There are few things that could be good to check:

  • Do you have @Push annotation on the UI?
  • Then you also need asyncSupported true for the VaadinServlet (is true by default)
  • UI.getCurrent() should only be called from an UI thread. It would be better to pass the desired UI instance to class Hilo.
  • You should not set polling interval when using push (it is redundant)
  • getUI() is not thread safe and it might return null if the component is detached. It would be better to pass the correct UI instance to the thread before it is started.

Hope this helps,
Pontus

Hi Pontus Boström,

A simple label does not act like it should, before it did in spite of that error of UIDetachedException, there are other things that I forgot to limit, is that archetype-application-example, it works with push but with the version that is in the repository github, with vaadin 8 does not work

  • I have @Push
  • My WebServlet contains asyncSupported = true

The above method runWhileAttached () works fine by repainting the chart, but in vaadin 8.0.5 it does not work for me

The problem is indeed in the Hilo class. The UI.getCurrent() returns null (since that thread is not an UI thread) and thus produces a NullPointerException. Yet, that exception is happily ignored and eaten by Java as the thread terminates, and it’s not even displayed in the console. This is a horrible JVM decision, but one we have to live with. As a rule of thumb, I always write Thread.run methods as follows:

public void run() {
  try {
     ... do your stuff
  } catch (Throwable t) {
    logger.log(t);
    // or t.printStackTrace();
  }
}

So, instead of using UI.getCurrent() in the thread, pass the UI instance via a constructor parameter to the Hilo class; I assume you will construct the Hilo class in an UI thread which will then return non-null for UI.getCurrent().

Thank you very much, it works