BlockingQuartzJob implements Job { public void execute( final JobExecutionContext jobExecutionContext ) throws JobExecutionException { final boolean jobRestarted; if ( jobExecutionContext != null && jobExecutionContext.getJobDetail() != null ) { jobRestarted = jobExecutionContext.getJobDetail().getJobDataMap().getBooleanValue( QuartzScheduler.RESERVEDMAPKEY_RESTART_FLAG ); } else { jobRestarted = false; } String messageType = jobRestarted ? MessageTypes.RECREATED_INSTANCE_START : MessageTypes.INSTANCE_START; long start = System.currentTimeMillis(); long end = start; try { if ( getBlockoutManager().shouldFireNow() || isBlockoutAction( jobExecutionContext ) ) { makeAuditRecord( 0, messageType, jobExecutionContext ); createUnderlyingJob().execute( jobExecutionContext ); end = System.currentTimeMillis(); messageType = jobRestarted ? MessageTypes.RECREATED_INSTANCE_END : MessageTypes.INSTANCE_END; } else { getLogger().warn( "Job '" + jobExecutionContext.getJobDetail().getName() + "' attempted to run during a blockout period. This job was not executed" ); } } catch ( ActionAdapterQuartzJob.LoggingJobExecutionException le ) { end = System.currentTimeMillis(); messageType = jobRestarted ? MessageTypes.RECREATED_INSTANCE_FAILED : MessageTypes.INSTANCE_FAILED; } catch ( SchedulerException e ) { end = System.currentTimeMillis(); messageType = jobRestarted ? MessageTypes.RECREATED_INSTANCE_FAILED : MessageTypes.INSTANCE_FAILED; getLogger().warn( "Got Exception retrieving the Blockout Manager for job '" + jobExecutionContext.getJobDetail().getName() + "'. Executing the underlying job anyway", e ); createUnderlyingJob().execute( jobExecutionContext ); end = System.currentTimeMillis(); messageType = jobRestarted ? MessageTypes.RECREATED_INSTANCE_END : MessageTypes.INSTANCE_END; } finally { makeAuditRecord( ( (float) ( end - start ) / 1000 ), messageType, jobExecutionContext ); } }  void execute( final JobExecutionContext jobExecutionContext );  }
@Test public void testJobIsBlockedDuringABlockout() throws JobExecutionException { BlockingQuartzJob blockingJob = createTestBlockingJob( false ); mockery.checking( new Expectations() { { one( blockoutManager ).shouldFireNow(); will( returnValue( false ) ); one( logger ).warn( "Job 'myjob' attempted to run during a blockout period. This job was not executed" ); allowing( context ).getJobDetail(); will( returnValue( new JobDetail( "myjob", BlockingQuartzJob.class ) ) ); } } ); blockingJob.execute( context ); }
@Test public void testJobIsRunWhenNoBlockout() throws JobExecutionException { BlockingQuartzJob blockingJob = createTestBlockingJob( false ); try { mockery.checking( new Expectations() { { oneOf( context ).getJobDetail(); will( returnValue( new JobDetail( "somejob", BlockingQuartzJob.class ) ) ); oneOf( context ).getJobDetail(); will( returnValue( new JobDetail( "somejob", BlockingQuartzJob.class ) ) ); one( blockoutManager ).shouldFireNow(); will( returnValue( true ) ); one( underlyingJob ).execute( with( same( context ) ) ); } } ); } catch ( SchedulerException e ) { throw new RuntimeException( e ); } blockingJob.execute( context ); }
@Test public void testJobIsRunWhenThereIsAnExceptionRetrievingTheBlockoutManager() throws JobExecutionException { BlockingQuartzJob blockingJob = createTestBlockingJob( true ); mockery.checking( new Expectations() { { oneOf( context ).getJobDetail(); will( returnValue( new JobDetail( "somejob", BlockingQuartzJob.class ) ) ); oneOf( context ).getJobDetail(); will( returnValue( new JobDetail( "somejob", BlockingQuartzJob.class ) ) ); one( underlyingJob ).execute( with( same( context ) ) ); one( context ).getJobDetail(); will( returnValue( new JobDetail( "somejob", BlockingQuartzJob.class ) ) ); one( logger ) .warn( "Got Exception retrieving the Blockout Manager for job 'somejob'." + " Executing the underlying job anyway", schedulerException ); } } ); blockingJob.execute( context ); }