TestJob.java

  1. package edu.ucsb.cs156.courses.jobs;

  2. import edu.ucsb.cs156.courses.services.jobs.JobContext;
  3. import edu.ucsb.cs156.courses.services.jobs.JobContextConsumer;
  4. import lombok.Builder;
  5. import org.springframework.security.core.Authentication;
  6. import org.springframework.security.core.context.SecurityContextHolder;

  7. @Builder
  8. public class TestJob implements JobContextConsumer {

  9.   private boolean fail;
  10.   private int sleepMs;

  11.   @Override
  12.   public void accept(JobContext ctx) throws Exception {
  13.     // Ensure this is not null
  14.     Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

  15.     ctx.log("Hello World! from test job!");
  16.     if (authentication == null) {
  17.       ctx.log("authentication is null");
  18.     } else {
  19.       ctx.log("authentication is not null");
  20.     }
  21.     Thread.sleep(sleepMs);
  22.     if (fail) {
  23.       throw new Exception("Fail!");
  24.     }
  25.     ctx.log("Goodbye from test job!");
  26.   }
  27. }