Migrate old Quartz job name

44 views Asked by At

I have this old Quartz job which I would like to migrate to a new version:

@Component
public class SystemProcessJob implements Job {
  
  ...... 

  @Override
  protected void execute(JobExecutionContext context) throws JobExecutionException 
  {
    final JobKey key = context.getJobDetail().getKey();
    final UUID runId = UUID.fromString(key.getName());

    executeInternalProcess(runId);

    logger.info("Job Executed");
  }

When I run it I get error:

java.lang.IllegalArgumentException: Invalid UUID string: jobDetail

What should be the proper way to migrate this code? I need to generate manual UUID runId or get the UUID rom the job?

1

There are 1 answers

0
ozkanpakdil On

You are getting the IllegalArgumentException because UUID.fromString is expecting you to provide a real UUID which should look like "24b7cc4e-a3ba-4ee7-b0e3-1ccdda09ec30" 32 digit hexadecimal number check, it is getting "jobDetail" as UUID and throwing the exception.

Without knowing the full context of the project. If you want to use a UUID as the job identifier in Quartz, you should ensure that you're setting the job name to a valid UUID string when you're creating the job. Here's an example of how you can do this:

@SpringBootApplication
public class QuartzDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(QuartzDemoApplication.class, args);
        onStartup();
    }

    private static void onStartup() throws SchedulerException {
        JobDetail job = JobBuilder.newJob(SystemProcessJob.class)
                .withIdentity(UUID.randomUUID().toString(), "group1") // set job name to a valid UUID
                .build();

        Date afterFiveSeconds = Date.from(LocalDateTime.now().plusSeconds(5)
                .atZone(ZoneId.systemDefault()).toInstant());
        Trigger trigger = TriggerBuilder.newTrigger()
                .startAt(afterFiveSeconds)
                .build();

        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
    }
}

now the method below will bring the UUID as you wanted

@Override
protected void execute(JobExecutionContext context) throws JobExecutionException
{
  final JobKey key = context.getJobDetail().getKey();
  final UUID runId = UUID.fromString(key.getName());

  executeInternalProcess(runId);

  logger.info("Job Executed");
}

I do not know the how the UUID is used in executeInternalProcess personally I would use only UUID.randomUUID() instead of UUID.fromString this will lower the complexity, And if you do not see any problems you don't need to create that job at the beginning with the random UUID.

@Override
protected void execute(JobExecutionContext context) throws JobExecutionException
{
  final UUID runId = UUID.randomUUID();

  executeInternalProcess(runId);

  logger.info("Job Executed");
}