Creating thread to run extra activities

Hi,

Is it possible to create a thread similar to like implementing Runnable interface or extending thread class.
As i tried the same but it didn’t work. Also tried taskexecutor to run activities but sometimes it doesn’t work on updating database record or calling external api. Even adding @Authenticated it shows same error to add @ Authentication.
Is there any other simplest way to run extra activities on thread to avoid delay in response.

Thank you in advance.

Hello!

I guess you added annotation on the method that runs another thread. However you should setup authentication inside of run thread. In Jmix, depends on requirements you can use:

  1. Background Tasks :: Jmix Documentation
  2. Quartz :: Jmix Documentation

If you want to use TaskExecutor, you can do the following to setup authentication:

@Component
public class UsersService {

    private final TaskExecutor taskExecutor;
    private final SystemAuthenticator systemAuthenticator;
    private final DataManager dataManager;

    //...

    public void clearNotActiveUsers() {
        taskExecutor.execute(() -> {
            systemAuthenticator.runWithSystem(() -> {
                List<User> users = dataManager.load(User.class).all().list();
                // ...
            });
        });
    }
}

You aslo can use UnconstrainedDataManager that does not check authentication:

@Component
public class UsersService {

    private final UnconstrainedDataManager unconstrainedDataManager;
    // ...

    public void clearNotActiveUsers() {
        taskExecutor.execute(() -> {
            List<User> users = unconstrainedDataManager.load(User.class).all().list();
            // ...
        });
    }
}

Or configure the @Async annotation in application class using @EnableAsync (see Task Execution and Scheduling :: Spring Framework).

@Component
public class UsersService {

    private final DataManager dataManager;
    // ...

    @Async
    @Authenticated
    public void clearNotActiveUsers() {
        List<User> users = dataManager.load(User.class).all().list();
        // ...
    }
}