我创建了一个应用程序来回答问题。在大多数情况下,该应用程序运行正常,但我从ScheduledService
updateMessage
. 我的理解udpateMessage
可能是错误的,这可能会让我误入歧途。我的理解是每次调用时都updateMessage
应该更新。在本例中,应该每三秒一次。在这个应用程序中,这种情况没有发生。并且都按预期进行更新。这是错误的行为,还是我对工作原理的想法错误?messageProperty
updateMessage
updateValue
System.out.println
updateMessage
主要的
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import org.controlsfx.control.Notifications;
public class App extends Application {
static List<MyNotification> myNotifications = new ArrayList();
static FakeDatabaseHandler fakeDatabaseHandler = new FakeDatabaseHandler();
@Override
public void start(Stage stage) {
Button btnGenerateNotification = new Button("Generate!");
btnGenerateNotification.setOnAction(actionEvent ->{
fakeDatabaseHandler.addFakeNotificationsToDb();
});
Label lblUpdateMessage = new Label("Update Message: ");
ScheduledService<List<MyNotification>> checkForScheduledService = new CheckForNotificationsScheduledService();
checkForScheduledService.setPeriod(Duration.seconds(5));
checkForScheduledService.messageProperty().addListener((ov, oldValue, newValue) -> {
if(newValue != null)
{
lblUpdateMessage.setText("Update Message: " + newValue);
}
});
checkForScheduledService.valueProperty().addListener((ov, oldValue, newValue) -> {
if(newValue != null && !newValue.isEmpty())
{
myNotifications.clear();
myNotifications.addAll(newValue);
checkForScheduledService.cancel();
AtomicInteger index = new AtomicInteger(0);
checkForScheduledService.cancel();
Timeline threeSecondsWonder = new Timeline(
new KeyFrame(Duration.seconds(3), event -> {
MyNotification myNotification = myNotifications.get(index.getAndIncrement());
Notifications.create().title(myNotification.department()+ " - " + myNotification.title()).text(myNotification.message()).hideAfter(Duration.seconds(3)).showWarning();
})
);
threeSecondsWonder.setCycleCount(myNotifications.size());
threeSecondsWonder.play();
threeSecondsWonder.setOnFinished((t) -> {
checkForScheduledService.restart();
});
}
});
checkForScheduledService.start();
StackPane root = new StackPane(new VBox(btnGenerateNotification, lblUpdateMessage));
stage.setScene(new Scene(root, 500, 500));
stage.setTitle("Facilities");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
private static class CheckForNotificationsScheduledService extends ScheduledService<List<MyNotification>> {
@Override
protected Task<List<MyNotification>> createTask() {
return new Task<List<MyNotification>>() {
@Override
protected List<MyNotification> call() throws Exception {
List<MyNotification> fakeNotificationList = fakeDatabaseHandler.getFakeNotificationsFromDB();
System.out.println("Checking for notifications. Notifications Found: " + fakeNotificationList.size());
updateMessage("Checking for notifications. Notifications Found: " + fakeNotificationList.size());
updateValue(fakeNotificationList);
return fakeNotificationList;
}
};
}
}
}
假数据库处理程序
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
*
* @author Sed
*/
public class FakeDatabaseHandler {
private final List<MyNotification> fakeDBData = new ArrayList();
public FakeDatabaseHandler() {}
public void addFakeNotificationsToDb(){
List<String> departmentList = Arrays.asList("A", "B", "C", "D");
for(int i = 0; i < ThreadLocalRandom.current().nextInt(1, 6); i++)
{
int notificationID = ThreadLocalRandom.current().nextInt( 1000000);
fakeDBData.add(new MyNotification("Department: " + departmentList.get(ThreadLocalRandom.current().nextInt(4)), "Title " + notificationID, "Message: " + notificationID));
}
}
public List<MyNotification> getFakeNotificationsFromDB(){
List<MyNotification> returnList = new ArrayList(fakeDBData);
fakeDBData.clear();
return returnList;
}
}
我的通知
public record MyNotification(String department, String title, String message) {}
模块信息
module com.mycompany.javafxsimpletest {
requires org.controlsfx.controls;
exports com.mycompany.javafxsimpletest;
}
输出
技术信息
Product Version: Apache NetBeans IDE 18
Java: 19.0.2; OpenJDK 64-Bit Server VM 19.0.2+7-jvmci-22.3-b12
Runtime: OpenJDK Runtime Environment 19.0.2+7-jvmci-22.3-b12
System: Windows 11 version 10.0 running on amd64; UTF-8; en_US (nb)
请注意,有两条消息:一条属于创建的任务,一条属于服务。当服务正在运行任务时,其消息会绑定到任务的消息,但一旦任务完成,服务的消息将重置为空。
因此,您的任务根据按下按钮生成的值设置其消息(以及服务的消息),但随后任务立即退出,导致服务的消息设置为 null。
请注意,这两个更改可能会合并,具体取决于服务的实现。但即使不是,在渲染下一帧之前,对 null 的更改将立即遵循任务中显式编码的更改。因此,无论哪种情况,标签都只会显示空文本。
正如问题下面的评论所指出的那样,您的代码还存在各种其他问题(模型类不是线程安全的,而是从多个线程访问,并且反复重新启动,使其变得多余)
ScheduledService
。