Menu
pushmode

Class PageExecutor

PushMode » API Reference » pushmode

  • All Implemented Interfaces:
    Executor, ExecutorService


    public class PageExecutor
    extends Object
    implements ExecutorService
    Event loop where PushPage executes. Every PushPage instance has one associated event loop that can be obtained from PushPage.executor() method. All page renders (executions of PushPage.document()), DOM events, and DOM bindings are scheduled on PageExecutor. Event loops of different page instances are unrelated. Pages execute concurrently with each other.
    PageExecutor processes one event at a time. Every event might run on a different thread, because events are scheduled on a thread pool, but PageExecutor makes sure no two events execute at the same time. From application's point of view, all page-related code appears to run in a single thread. It is therefore unnecessary to implement any synchronization on page level. Thread-local variables still work, but only within one event. Thread-local variables cannot maintain state across several events.
    PageExecutor implements ExecutorService interface. ExecutorService defines convenient methods that allow scheduling custom application events on the event loop. Any 3rd party code can be forced to run in the event loop as far as it supports configurable ExecutorService. The thread pool is compute-optimized and applications should refrain from running blocking operations in it. All I/O operations should instead go through appropriate non-blocking reactive APIs.
    • Method Detail

      • execute

        public void execute(Runnable runnable)
        Schedules Runnable on this PageExecutor. The supplied Runnable will run in page's event loop. If the Runnable throws an exception, page's event loop will stop and browser connection will be terminated.
        Specified by:
        execute in interface Executor
        Parameters:
        runnable - Runnable to schedule.
        See Also:
        submit(Runnable)
        Thread-safety:
        This method is safe to call from any thread. The supplied Runnable will run in the event loop, which means it doesn't need to synchronize access with other events in the same event loop.
      • invokeAll

        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        Blocks until all tasks complete running on this PageExecutor. All supplied tasks are executed as one big event in the event loop. They are not scheduled separately.
        Specified by:
        invokeAll in interface ExecutorService
        Parameters:
        tasks - Tasks to execute on this PageExecutor.
        Returns:
        List of completed futures holding result or exception for every supplied task.
        Thread-safety:
        This method is safe to call from any thread except the thread used by this PageExecutor where it would result in a deadlock. This method blocks, which means it shouldn't be called from within event on any PageExecutor. The supplied tasks will run in the event loop, which means they don't need to synchronize access with other events in the same event loop.
      • invokeAll

        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                             long timeout,
                                             TimeUnit unit)
        Blocks until all tasks complete running on this PageExecutor or timeout expires. All supplied tasks are executed as one big event in the event loop. They are not scheduled separately. If timeout is reached, this method completes without exception, but some or all of the tasks might be in cancelled state as if CompletableFuture.cancel(boolean) was called.
        Specified by:
        invokeAll in interface ExecutorService
        Parameters:
        tasks - Tasks to execute on this PageExecutor.
        timeout - Timeout to wait for completion of the tasks in units specified.
        unit - Unit of the timeout.
        Returns:
        List of completed futures holding result or exception for every supplied task. If timeout is reached some or all of the tasks might be cancelled instead.
        Thread-safety:
        This method is safe to call from any thread except the thread used by this PageExecutor where it would result in a deadlock. This method blocks, which means it shouldn't be called from within event on any PageExecutor. The supplied tasks will run in the event loop, which means they don't need to synchronize access with other events in the same event loop.
      • shutdown

        public void shutdown()
        Stops the event loop. This method is provided for compatibility with ExecutorService. Application shouldn't call it under any circumstances. Resource management should be left to PushMode.
        PageExecutor will wait for remaining events to execute before stopping. This method however returns immediately. No new events are allowed to be scheduled.
        Specified by:
        shutdown in interface ExecutorService
        Thread-safety:
        This method is safe to call from any thread. It is safe to call multiple times.
      • shutdownNow

        public List<Runnable> shutdownNow()
        Stops the event loop without waiting for remaining events to execute. This method is provided for compatibility with ExecutorService. Application shouldn't call it under any circumstances. Resource management should be left to PushMode.
        This method returns immediately with the list of unfinished events. No new events are allowed to be scheduled after this method is called. Currently running event is allowed to complete.
        Specified by:
        shutdownNow in interface ExecutorService
        Returns:
        List of events that weren't allowed to run on the PageExecutor.
        Thread-safety:
        This method is safe to call from any thread. It is safe to call multiple times.
      • poster

        public boolean poster()
        Checks whether currently rendered frame is a poster frame.
        Poster frame is sent as vanilla HTML to the browser in response to initial HTTP request. Usually only the first frame of the page is a poster frame, but draft frames may cause the poster frame to be rendered several times.
        Application code can call this method from within PushPage.document() method. It is useful for rendering slightly different content in the poster frame, perhaps to deal with bots or to enable events in a more controlled fashion.
        Returns:
        True if rendering poster frame.
        Thread-safety:
        This method is safe to call from any thread.
        Reactive programming:
        This method accesses reactive data. It will cause reactive computations to re-evaluate when the reactive data changes.
      • page

        public PushPage page()
        PushPage that runs in this event loop.
        Returns:
        PushPage that runs in this event loop.
        See Also:
        current()
        Thread-safety:
        This method only reads immutable data.