TestJob.java

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


  2. import org.springframework.security.core.Authentication;
  3. import org.springframework.security.core.context.SecurityContextHolder;

  4. import edu.ucsb.cs156.happiercows.services.jobs.JobContext;
  5. import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer;
  6. import lombok.Builder;

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

  9.     private boolean fail;
  10.     private int sleepMs;
  11.    
  12.     @Override
  13.     public void accept(JobContext ctx) throws Exception {
  14.             // Ensure this is not null
  15.             Authentication authentication =  SecurityContextHolder.getContext().getAuthentication();

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