Debugging Scheduled Tasks Service Beans

I have an schedule task interface defined as: -

/**
 * Interface the defines bean methods that can be accessed by the Scheduled Tasks feature of CUBA.
 */
public interface FinsScheduledTasksService {
    String NAME = "fins_FinsScheduledTasksService";

    /**
     * Performs updates to the StdMasterWaterbody entity. For example, if a water is a lake and has no reach, then
     * a default one is created.
     * @return Returns a string with the result of updating waters. If an exception occurs, then the result will be
     * the Exception.toString() result.
     */
    String updateWaters();

    /**
     * Identifies photos uploaded from the iPad without a thumbnail and creates one for it.
     * @return Returns a string with the number of thumbnails generated. If an exception occurs, then the result will be
     * the Exception.toString() result.
     */
    String addPhotoThumbnail();
}

The addPhotoThumbnail implementation looks like this: -

/**
 * Actual implementation of the scheduled task service interfaces.
 */
@Component(FinsScheduledTasksService.NAME)
public class FinsScheduledTasksServiceBean implements FinsScheduledTasksService {

    // create logger
    private Logger log = LoggerFactory.getLogger(FinsScheduledTasksServiceBean.class);

    @Inject
    private Persistence persistence;

    @Inject
    private UserSessionSource userSessionSource;

    @Inject
    private FileStorageService fileStorageService;

    @Inject
    private Metadata metadata;

    /**
     * Generates thumbnail images for photos uploaded from the iPad for faster rending when viewing a fish record.
     * @return Returns a string with the number of thumbnails generated. If an exception occurs, then the result will be
     * the Exception.toString() result.
     */
    @Transactional
    @Override
    public String addPhotoThumbnail() {
        String result;
        Integer thumbnailsAdded = 0;
        log.warn("Start addPhotoThumbnail Service Method: Retrieve Photos without thumbnails.");
        Transaction tx = persistence.createTransaction();
        try {
              // logic
            tx.commit();
            result = String.format("End addPhotoThumbnail Service Method: There are %s Photos without thumbnails. Added %s thumbnails.", photos.size(), thumbnailsAdded);
            log.warn(result);
            return result;
        }
        catch (Exception ex) {
            return ex.toString();
        }
        finally {
            tx.end();
        }
    }
}

The scheduled task registers and runs successfully, however I cannot figure out how to debug using IntelliJ. Debugging works fine for screens and another service bean I have registered in the middleware.

How do I debug a scheduled task using Tomcat?

Regards, Mike.

Hello, @mwhy

I’ve created test service and used it in scheduled task:

изображение

Then I connected to Tomcat in Intellij IDEA and execution was stopped on a breakpoint successfully:

изображение

Сould you check to see if your task is actually starting and share small test project with your case?

Regards,
Daniil

Your bean is defined with the @Service annotation, mine is defined with the @Component annotation as recommended here: -

https://doc.cuba-platform.com/manual-6.8/managed_beans_creation.html

The @javax.annotation.ManagedBean can also be used for the managed bean definition, but it can cause problems when deploying the application into some application servers. Therefore we recommend to use only @Component annotation from Spring Framework.

If you have time, can you try yours with @Component and see if debugging still works. Maybe the @Component annotation shouldn’t be used for scheduled task manage beans. It’s hard to tell from the documentation.

I have another service bean that’s not scheduled that uses @Service and I can debug this one.

Hello,

I’ve tried to use @Component instead of @Service and everything is still OK:

image

image

Here is small project for testing:

debug-scheduled-bean.zip (71.0 KB)

Regards,
Daniil.