com.vaadin.flow.component.UI.access()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(5.9k)|赞(0)|评价(0)|浏览(189)

本文整理了Java中com.vaadin.flow.component.UI.access()方法的一些代码示例,展示了UI.access()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。UI.access()方法的具体详情如下:
包路径:com.vaadin.flow.component.UI
类名称:UI
方法名:access

UI.access介绍

[英]Provides exclusive access to this UI from outside a request handling thread.

The given command is executed while holding the session lock to ensure exclusive access to this UI. If the session is not locked, the lock will be acquired and the command is run right away. If the session is currently locked, the command will be run before that lock is released.

RPC handlers for components inside this UI do not need to use this method as the session is automatically locked by the framework during RPC handling.

Please note that the command might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the command is executed. UI#getCurrent(), VaadinSession#getCurrent() and VaadinService#getCurrent() are set according to this UI before executing the command. Other standard CurrentInstance values such as VaadinService#getCurrentRequest() and VaadinService#getCurrentResponse() will not be defined.

The returned future can be used to check for task completion and to cancel the task.
[中]提供从请求处理线程外部对此UI的独占访问。
在保持会话锁的同时执行给定的命令,以确保以独占方式访问此UI。如果会话未锁定,将获取锁并立即运行命令。如果会话当前已锁定,则在释放该锁定之前将运行该命令。
此UI内组件的RPC处理程序不需要使用此方法,因为在RPC处理期间,会话会被框架自动锁定。
请注意,该命令可能会在其他线程上调用,或者稍后在当前线程上调用,这意味着在执行该命令时,自定义线程局部变量可能没有预期的值。UI#getCurrent()、VaadinSession#getCurrent()和VaadinService#getCurrent()在执行命令之前根据此UI进行设置。将不定义其他标准CurrentInstance值,例如VaadinService#getCurrentRequest()和VaadinService#getCurrentResponse()。
返回的future可用于检查任务完成情况和取消任务。

代码示例

代码示例来源:origin: com.vaadin/flow-server

return access(command, null);

代码示例来源:origin: com.vaadin/flow-server

/**
 * Wraps the given access task as a runnable that runs the given task with
 * this UI locked. The wrapped task may be run synchronously or
 * asynchronously. If the UI is detached when the returned runnable is run,
 * the provided detach handler is run instead. If the provided detach
 * handler is <code>null</code>, the returned runnable may throw an
 * {@link UIDetachedException}.
 * <p>
 * This method can be used to create a callback that can be passed to an
 * external notifier that isn't aware of the synchronization needed to
 * update a UI instance.
 *
 * @param accessTask
 *            the task that updates this UI, not <code>null</code>
 * @param detachHandler
 *            the callback that will be invoked if the UI is detached, or
 *            <code>null</code> as described above
 * @return a runnable that will run either the access task or the detach
 *         handler, possibly asynchronously
 */
public SerializableRunnable accessLater(SerializableRunnable accessTask,
    SerializableRunnable detachHandler) {
  Objects.requireNonNull(accessTask, "Access task cannot be null");
  return () -> access(accessTask::run, detachHandler);
}

代码示例来源:origin: appreciated/vaadin-app-layout

public void refreshNotifications(NotificationHolder notificationHolder) {
  getUI().ifPresent(ui -> ui.access(() -> {
    notificationsView.initView();
    //setContent(new NotificationsView(notificationHolder));
  }));
}

代码示例来源:origin: com.vaadin/flow-server

/**
 * Wraps the given access task as a consumer that passes a value to the
 * given task with this UI locked. The wrapped task may be run synchronously
 * or asynchronously. If the UI is detached when the returned consumer is
 * run, the provided detach handler is run instead. If the provided detach
 * handler is <code>null</code>, the returned runnable may throw an
 * {@link UIDetachedException}.
 * <p>
 * This method can be used to create a callback that can be passed to an
 * external notifier that isn't aware of the synchronization needed to
 * update a UI instance.
 *
 * @param accessTask
 *            the task that updates this UI, not <code>null</code>
 * @param detachHandler
 *            the callback that will be invoked if the UI is detached, or
 *            <code>null</code> as described above
 * @return a consumer that will run either the access task or the detach
 *         handler, possibly asynchronously
 */
public <T> SerializableConsumer<T> accessLater(
    SerializableConsumer<T> accessTask,
    SerializableRunnable detachHandler) {
  Objects.requireNonNull(accessTask, "Access task cannot be null");
  return value -> access(() -> accessTask.accept(value), detachHandler);
}

代码示例来源:origin: vaadin/spring

@Override
public void run() {
  // We can acquire the lock after the request started this thread is processed
  // Needed to make sure that this is sent as a push message
  Lock lockInstance = ui.getSession().getLockInstance();
  lockInstance.lock();
  lockInstance.unlock();
  ui.access(() -> execute(ui));
}

代码示例来源:origin: appreciated/vaadin-app-layout

private void reloadNotifications() {
  if (currentThread != null && !currentThread.isInterrupted()) {
    currentThread.interrupt();
  }
  badgeHolder.clearCount();
  notificationHolder.clearNotifications();
  currentThread = new Thread(() -> {
    try {
      Thread.sleep(1000);
      for (int i = 0; i < 3; i++) {
        getUI().ifPresent(ui -> ui.access(() -> {
          addNotification(MEDIUM);
          badgeHolder.increase();
          badgeHolder.increase();
        }));
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  });
  currentThread.start();
}

代码示例来源:origin: appreciated/vaadin-app-layout

public void reloadNotifications() {
  if (currentThread != null && !currentThread.isInterrupted()) {
    currentThread.interrupt();
  }
  badgeHolder.clearCount();
  notificationHolder.clearNotifications();
  currentThread = new Thread(() -> {
    try {
      Thread.sleep(1000);
      for (int i = 0; i < 3; i++) {
        //Thread.sleep(5000);
        getUI().ifPresent(ui -> ui.access(() -> {
          addNotification(MEDIUM);
          badgeHolder.increase();
          badgeHolder.increase();
        }));
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  });
  currentThread.start();
}

代码示例来源:origin: vaadin/spring

@Override
  public void run() {
    // We can acquire the lock after the request started this thread is processed
    // Needed to make sure that this is sent as a push message
    Lock lockInstance = ui.getSession().getLockInstance();
    lockInstance.lock();
    lockInstance.unlock();
    ui.access(() -> {
      Paragraph world = new Paragraph("World");
      world.setId("world");
      add(world);
    });
  }
}

代码示例来源:origin: appreciated/vaadin-app-layout

holder.addClickListener(newStatus -> getUI().ifPresent(ui -> ui.access(AppBarNotificationButton.this::close)));
holder.bind(((IconBadgeButton) getTriggerComponent()).getBadge());

相关文章