@ Override public void execute ( JobExecutionContext context ) throws JobExecutionException { JobDataMap map = context . getJobDetail ( ) . getJobDataMap ( ) ; AlertService alertService = ( AlertService ) map . get ( "AlertService" ) ; AuditService auditService = ( AuditService ) map . get ( "AuditService" ) ; if ( map . containsKey ( LOCK_TYPE ) ) { lockType = ( LockType ) map . get ( LOCK_TYPE ) ; } if ( map . containsKey ( CRON_JOB ) ) { job = ( CronJob ) map . get ( CRON_JOB ) ; } try { if ( ! alertService . isDisposed ( ) ) { if ( LockType . ALERT_SCHEDULING . equals ( lockType ) ) { alertService . enqueueAlerts ( Arrays . asList ( new Alert [ ] { Alert . class . cast ( job ) } ) ) ; } else { throw new SystemException ( "Unsupported lock type " + lockType ) ; } } } catch ( Exception ex ) { auditService . createAudit ( "Could not enqueue scheduled job. " + ex . getMessage ( ) , JPAEntity . class . cast ( job ) ) ; } }
Passing the service instance from quartz main thread to worker thread as a parameter . Although the alert service used here is not thread safe we are using the enqueueAlerts in a thread safe manner since there is no shared mutable state in this method .