如何在预加载器中处理java Web启动(jnlp)下载进度?
问题
我的应用程序有一个预加载器,用于处理特定于应用程序的初始化。现在,我正在尝试扩展它,以便预加载器也显示下载的应用程序 JAR 的进度。
TL;DR
为什么预加载器在阶段2期间没有加载,因为这应该处理跟踪我认为的JAR的下载?
PreloaderFx::handleProgressNotification();
2016 年 3 月 14 日更新:使用 DownloadServiceListener 是解决此问题的方法吗?如何将其连接到JavaFX阶段?
文档
根据Oracle的说法,应用程序启动时有4个阶段:
- 阶段 1:初始化:Java 运行时的初始化和初始检查确定在启动应用程序之前必须加载和执行的组件。在此阶段,将显示初始屏幕。默认为:
阶段 2:加载和准备:从网络或磁盘缓存加载所需的资源,并执行验证过程。所有执行模式都可以看到默认或自定义预加载器。在此阶段,应显示我的自定义预加载器。
阶段 3:特定于应用程序的初始化:应用程序已启动,但可能需要加载其他资源或执行其他冗长的准备工作才能完全正常运行。目前,我的自定义预加载器显示:
- 阶段 4:应用程序执行:显示应用程序并可供使用。在我的情况下,将显示一个登录窗口,用户可以继续。
我的案例
我注意到的第一件事是,在第 2 阶段中,没有显示处理应用程序 JAR 下载的默认 JavaFX 预加载器。因此,用户会感觉到程序没有过早启动或终止,使他们多次打开JNLP文件。下载 JAR 后,我们进入阶段 3,并显示预加载器。
但是,我希望我的自定义预加载器也能处理进度条中的下载进度(阶段2)。我使一切尽可能简单,以跟踪在应用程序启动期间发生的事件。这是基于Jewelsea的一个例子和Oracle的例子:
预加载机:
public class PreloaderFX extends Preloader {
Stage stage;
//boolean noLoadingProgress = true;
public static final String APPLICATION_ICON
= "http://cdn1.iconfinder.com/data/icons/Copenhagen/PNG/32/people.png";
public static final String SPLASH_IMAGE
= "http://fxexperience.com/wp-content/uploads/2010/06/logo.png";
private Pane splashLayout;
private ProgressBar loadProgress;
private Label progressText;
private static final int SPLASH_WIDTH = 676;
private static final int SPLASH_HEIGHT = 227;
@Override
public void init() {
ImageView splash = new ImageView(new Image(
SPLASH_IMAGE
));
loadProgress = new ProgressBar();
loadProgress.setPrefWidth(SPLASH_WIDTH - 20);
progressText = new Label("Loading . . .");
splashLayout = new VBox();
splashLayout.getChildren().addAll(splash, loadProgress, progressText);
progressText.setAlignment(Pos.CENTER);
splashLayout.setStyle(
"-fx-padding: 5; "
+ "-fx-background-color: white; "
+ "-fx-border-width:5; "
);
splashLayout.setEffect(new DropShadow());
}
@Override
public void start(Stage stage) throws Exception {
System.out.println("PreloaderFx::start();");
//this.stage = new Stage(StageStyle.DECORATED);
stage.setTitle("Title");
stage.getIcons().add(new Image(APPLICATION_ICON));
stage.initStyle(StageStyle.UNDECORATED);
final Rectangle2D bounds = Screen.getPrimary().getBounds();
stage.setScene(new Scene(splashLayout));
stage.setX(bounds.getMinX() + bounds.getWidth() / 2 - SPLASH_WIDTH / 2);
stage.setY(bounds.getMinY() + bounds.getHeight() / 2 - SPLASH_HEIGHT / 2);
stage.show();
this.stage = stage;
}
@Override
public void handleProgressNotification(ProgressNotification pn) {
System.out.println("PreloaderFx::handleProgressNotification(); progress = " + pn.getProgress());
//application loading progress is rescaled to be first 50%
//Even if there is nothing to load 0% and 100% events can be
// delivered
if (pn.getProgress() != 1.0 /*|| !noLoadingProgress*/) {
loadProgress.setProgress(pn.getProgress() / 2);
/*if (pn.getProgress() > 0) {
noLoadingProgress = false;
}*/
}
}
@Override
public void handleStateChangeNotification(StateChangeNotification evt) {
//ignore, hide after application signals it is ready
System.out.println("PreloaderFx::handleStateChangeNotification(); state = " + evt.getType());
}
@Override
public void handleApplicationNotification(PreloaderNotification pn) {
if (pn instanceof ProgressNotification) {
//expect application to send us progress notifications
//with progress ranging from 0 to 1.0
double v = ((ProgressNotification) pn).getProgress();
System.out.println("PreloaderFx::handleApplicationNotification(); progress = " + v);
//if (!noLoadingProgress) {
//if we were receiving loading progress notifications
//then progress is already at 50%.
//Rescale application progress to start from 50%
v = 0.5 + v / 2;
//}
loadProgress.setProgress(v);
} else if (pn instanceof StateChangeNotification) {
System.out.println("PreloaderFx::handleApplicationNotification(); state = " + ((StateChangeNotification) pn).getType());
//hide after get any state update from application
stage.hide();
}
}
}
阶段 3 中处理的代码来自与预加载程序交互的主应用程序,这是在进度条中看到的内容:
public class MainApp extends Application {
BooleanProperty ready = new SimpleBooleanProperty(false);
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(final Stage initStage) throws Exception {
System.out.println("MainApp::start();");
this.mainStage = initStage;
longStart();
ready.addListener((ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) -> {
if (Boolean.TRUE.equals(t1)) {
Platform.runLater(() -> {
System.out.println("MainApp::showMainStage();");
showMainStage();
});
}
});
}
private void longStart() {
//simulate long init in background
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
int max = 10;
for (int i = 1; i <= max; i++) {
Thread.sleep(500);
System.out.println("longStart " + i);
// Send progress to preloader
notifyPreloader(new ProgressNotification(((double) i)/max)); //this moves the progress bar of the preloader
}
// After init is ready, the app is ready to be shown
// Do this before hiding the preloader stage to prevent the
// app from exiting prematurely
ready.setValue(Boolean.TRUE);
notifyPreloader(new StateChangeNotification(
StateChangeNotification.Type.BEFORE_START));
return null;
}
};
new Thread(task).start();
}
private void showMainStage() {
//showing the login window
}
}
日本国家语言学
<jnlp spec="1.0+" xmlns:jfx="http://javafx.com" codebase="<***>/preloadertest/jnlp" href="launch.jnlp">
<information>
...
</information>
<resources>
<j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se" />
... //whole bunch of JARS
<jar href="lib/preloader-1.1.1.jar" download="progress" />
</resources>
<security>
<all-permissions/>
</security>
<applet-desc width="1024" height="768" main-class="com.javafx.main.NoJavaFXFallback" name="JavaFX Client">
<param name="requiredFXVersion" value="8.0+"/>
</applet-desc>
<jfx:javafx-desc width="1024" height="768" main-class="GUI.MainApp" name="JavaFX Client" preloader-class="GUI.PreloaderFX" />
<update check="background"/>
</jnlp>
调试
在启动文件时,我仔细观察了Java控制台(启用了显示日志记录,禁用了显示跟踪),并注意到以下情况:
在阶段 2 期间,Java 控制台中不显示任何内容(在此阶段之后控制台关闭)
在阶段 3 期间,将生成以下输出(在新的控制台窗口中):
PreloaderFx::start();
PreloaderFx::handleProgressNotification(); progress = 1.0
PreloaderFx::handleStateChangeNotification(); state = BEFORE_LOAD
PreloaderFx::handleStateChangeNotification(); state = BEFORE_INIT
PreloaderFx::handleStateChangeNotification(); state = BEFORE_START
MainApp::start();
MainApp::longstart();
longStart 1
PreloaderFx::handleApplicationNotification(); progress = 0.1
longStart 2
PreloaderFx::handleApplicationNotification(); progress = 0.2
longStart 3
PreloaderFx::handleApplicationNotification(); progress = 0.3
longStart 4
PreloaderFx::handleApplicationNotification(); progress = 0.4
longStart 5
PreloaderFx::handleApplicationNotification(); progress = 0.5
longStart 6
PreloaderFx::handleApplicationNotification(); progress = 0.6
longStart 7
PreloaderFx::handleApplicationNotification(); progress = 0.7
longStart 8
PreloaderFx::handleApplicationNotification(); progress = 0.8
longStart 9
PreloaderFx::handleApplicationNotification(); progress = 0.9
longStart 10
PreloaderFx::handleApplicationNotification(); progress = 1.0
MainApp::showMainStage();
PreloaderFx::handleApplicationNotification(); state = BEFORE_START
2016 年 3 月 13 日更新:
- 调整了代码,以便使用方法中传递的阶段,而不是创建一个新阶段,并注释掉与布尔值相关的所有内容(由 nhylated 建议)
noLoadingProgress
- 在 MainApp 中添加了一些额外的内容
System.out.println()
溶液
简单地添加到JNLP文件修复了它。添加该行后,预加载器将显示在阶段 2 中。我还冒昧地将 结果更改为:<jfx:javafx-runtime version="8.0+"/>
j2se version="1.6+"
j2se version="1.8+"
前 50% 是处理 JAR 下载。这是通过该方法完成的。第二个 50% 是 MainApp 的实际初始化(它通知预加载器),由 .handleProgressNotification()
longstart()
handleApplicationNotification()