Background Task for business logic not related to one UI Screen

Hi All,

I’d like to define a background task that starts when my Cuba Application Server startups, like a Scheduled Task, but it never stops. The code is not related to any UI Screen and is used for
creating some backend business logic to acquire data from a real-time source (like an external broker) and store it into the DBMS.

What’s the best way to manage such a task in CUBA Platform?

Best

Hi,
I would advise the following:

  1. Declare a task executor (or task scheduler for periodic background activity) in spring.xml in the core module.
    If you need one never-ending process than use just one thread:
    <bean id="myExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="1"/>
        <property name="maxPoolSize" value="1"/>
        <property name="daemon" value="true"/>
    </bean>

  1. Inject this executor to one of application beans in the core module and call myExecutor.execute(Runnable) to start background task.

  2. CUBA requires user code to be authenticated, e.g. when you are persisting entities to DB or using DataManager or UserSessionSource.
    Use com.haulmont.cuba.security.app.Authentication bean to authenticate your code as some system user.

1 Like