BackgroundTask implemntation compilation weirdness

I am trying to create a background task that runs periodically in my mains screen. I found the documentation, it seems straightforward. However, I am running into some weird behavior.

This is my class code:

open class MainMenuBackgroundTask constructor(
    private val timeoutSeconds: Long,
    private val mainScreenSideMenu: MainScreenSideMenu,
    private val menuList: List<SideMenu.MenuItem>,
    private val menuMap: MutableMap<String, Int>,
    private val mainWindowService: MainWindowService,
    private val progressBar: ProgressBar
) : BackgroundTask<Int, Unit>(timeoutSeconds, mainScreenSideMenu) {
    @Throws(Exception::class)
    override fun run(taskLifeCycle: TaskLifeCycle<Int>): Unit {
        var index = 0
        for (item in menuList) {
            if (taskLifeCycle.isCancelled) {
                break
            }
            menuMap[item.id] = mainWindowService.countItems(item.id)
            taskLifeCycle.publish(index)
            index++
        }
        return
    }

    override fun done(result: Unit) {
        super.done(result)
        var total = 0
        for (item in menuList) {
            total += mainScreenSideMenu.setMenuBadge(item)
        }
        ownerScreen!!.window.caption = if (total == 0) "" else "($total) Aspen"
        progressBar.isVisible = false
    }

    override fun canceled() {
        super.canceled()
        progressBar.isVisible = false
    }

    override fun progress(changes: List<Int>) {
        val lastValue = changes[changes.size - 1].toDouble()
        progressBar.value = lastValue / menuList.size
    }
}

And the corresponding code in the controller to create and execute the task

progressBar.isVisible = true
val task = MainMenuBackgroundTask(1000,this, menuList, menuMap,mainWindowService,progressBar)
val taskHandler: BackgroundTaskHandler<*> = backgroundWorker.handle(task)
taskHandler.execute()

I can compile it successfully, but at runtime I keep getting a ClassNotFound exception. I have tried to separate the class into its own file, I have tried to make it an inner class of my main screen controller, and I have tried to inline it as an anonymous class. But for whatever reason, the .class file for the class just never exists in my build directory.

I have attempted to clean my project then compile, but I had no luck.

What could be the cause of this?

Greetings,
In jmix exists more simple way to update UI periodically: Timer: Timer :: Jmix Documentation

But if you want do it with your MainMenuBackgroundTask instead of timer, read documentation about server and client threads, and this problem happens because from ui thread class is not available.

I am making use of a timer, in fact that is what I am trying to use to kick off the background task.

I’m running some SQL queries periodically and then updating the main screen accordingly. I’m using a background task so that my SQL queries don’t block the UI thread. Isn’t that the intended purpose of the BackgroundTask API?

For sure, first idea of ui thread is refreshing ui component, i dont think that running sql inside ui thread is a good idea, but also sql should works, so running sql inside ui thread is just not recommended for escaping errors like this. I am not pretty sure about this error, but i have an idea how to fix it.

  1. Just refresh ui inside your component.
  2. Add item provider (like vaadin in any list components) → CallbackDataProvider that runs any sql commands, so you always can refresh items just by refreshing data provider
  3. refresh data provider and repaint element

UPD: use case for data provider:

public static DataProvider createCallbackDataProvider(List<?> items) {
        return new CallbackDataProvider<>(e -> {
            return (Stream) items.stream()
                    .limit(e.getLimit())
                    .skip(e.getOffset());

        }, e -> {
            return Math.toIntExact(items.stream()
                    .limit(e.getLimit())
                    .skip(e.getOffset())
                    .count());
        });
    }