db-scheduler: Unable to deserialize task data: Failed to deserialize object exception

First, some particulars:

db-scheduler 6.9 Spring Boot 2.1.5.RELEASE PostgresQL 11.6

I have been wrestling with this for a few hours now, but I am stumped. Following the Spring Boot idiom, I have a OneTimeTask defined as a Bean on a TasksConfiguration class (marked with @Configuration) with the signature Task<CountdownTimerTask> countdownTimerTask().

I am able to successfully schedule this task. The task data I supply as an instance of CountdownTimerTask, which implements Serializable and carries a serialVersionUID, serializes just fine.

The task fires as it should and my handler executes, but I get a deserialization exception (as described in the subject line above).

Here are the relevant bits of code (exception-handling has been elided):

(I tried eliminating Lombok and the toString override, just to be pure, but it had no effect).

CountdownTimerTask.java

import lombok.Data;
import java.io.Serializable;

@Data
public class CountdownTimerTask implements Serializable {
    private static final long serialVersionUID = 1L;
    private String projectId;

    @Override
    public String toString() {
        return projectId;
    }
}

TasksConfiguration.java

import com.github.kagkarlsson.scheduler.task.Task;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import ***************************.ITwilioService;
import ***************************.CountdownTimerTask;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TasksConfiguration {
    private final ITwilioService twilioService;

    public TasksConfiguration(ITwilioService twilioService) {
        this.twilioService = twilioService;
    }
   
    @Bean
    Task<CountdownTimerTask> countdownTimerTask() {
        return Tasks.oneTime("countdown-timer-task", CountdownTimerTask.class)
                .execute((instance, ctx) -> {
                    this.twilioService.sendSms(new String[] { "**********"}, "Project ID: " + instance.getData().getProjectId());
                });
    }
}

Below is from my service class, which is also the site of the dynamic scheduling of the CountdownTimerTask instance. The transaction event listener fires post-commit, and the handler code executes within the same transaction of the method that published the ProjectCreatedOrUpdatedEvent.

ProjectsService.java

import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.task.Task;
import ***************************.service.interfaces.IProjectsService;
import ***************************.model.project.*;
import ***************************.model.project.dtos.*;
import ***************************.service.event.ProjectCreatedOrUpdatedEvent;
import ***************************.task.CountdownTimerTask;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

@TransactionalEventListener
public void processProjectCreatedOrUpdatedEvent(ProjectCreatedOrUpdatedEvent event) 
    throws JsonProcessingException {
        ...

        if (event.getProjectDto().getEarliestBidDeadline() == null) return;

        CountdownTimerTask task = new CountdownTimerTask();
        task.setProjectId(event.getProjectDto().getId());

        scheduler.schedule(countdownTimerTask.instance(
                "countdown-timer-task", task), event.getProjectDto().getEarliestBidDeadline().toInstant());
    }

I’ve got to be missing something here. Also, everything works fine if I don’t involve task data.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments