@Bean public JobsBeingExecuted microEventJobsBeingExecuted() { return new JobsBeingExecuted( bus, maxLoggingCapacity, JobName.Types.SIMPLE); }
private Object executeScheduledJob(final ProceedingJoinPoint pjp, final String type) throws Throwable { addTypeToStatCounter(type); JobExecutingData data = new JobExecutingData( type, statCounter.count(type)); String id = buildId(type, data.getProcessingThread()); events.active(id, data); SystemData retVal = null; long correlationId = r.nextLong(); eventBus.post(new JobStartEvent( correlationId, type)); try { retVal = Optional.ofNullable(((SystemData) pjp.proceed())) .map(sd -> sd.withCorrelationId(id)) .orElse(null); return retVal; } finally { logSystemEvent(pjp, type, data, retVal); retVal = Optional.ofNullable(retVal) .orElse(SystemData.builder() .correlationId("" + correlationId) .errors(0l) .processed(0l) .build()); eventBus.post(new JobCompleteEvent( correlationId, type, retVal.getErrors(), retVal.getProcessed())); } }
@Around("execution(* com.oath.micro.server.events.ScheduledJob.scheduleAndLog(..))") public Object aroundScheduledJob(ProceedingJoinPoint pjp) throws Throwable { String type = jobNameType.getCreator() .getType(pjp.getSignature() .getDeclaringType()); return executeScheduledJob(pjp, type); }
private void postEvent(ProceedingJoinPoint pjp, String type, JobExecutingData data, SystemData retVal) { if (retVal != null) { eventBus.post(retVal); } events.finished(buildId(type, data.getProcessingThread())); }
private void scheduleAround() throws Throwable { Signature sig = Mockito.mock(Signature.class); when(pjp.getSignature()).thenReturn(sig); when(sig.getDeclaringType()).thenReturn(String.class); when(pjp.proceed()).thenReturn(data); when(pjp.getTarget()).thenReturn(this); jobs.aroundScheduledJob(pjp); } }
@Test public void testOverflow() throws Throwable { when(pjp.getTarget()).thenReturn(this); Field field = findField(ConcurrentHashMultiset.class, "countMap"); makeAccessible(field); ConcurrentMap map = (ConcurrentMap) getField(field, jobs.getStatCounter()); map.put("java.lang.String", Integer.MAX_VALUE); testExecute(); }
private void postEvent(ProceedingJoinPoint pjp, String type, JobExecutingData data, SystemData retVal) { if (retVal != null) { eventBus.post(retVal); } events.finished(buildId(type, data.getProcessingThread())); }
@Test public void testExecute() throws Throwable { Signature sig = Mockito.mock(Signature.class); Mockito.when(pjp.getSignature()) .thenReturn(sig); when(pjp.getTarget()).thenReturn(this); Mockito.when(sig.getDeclaringType()) .thenReturn(String.class); jobs.aroundScheduledJob(pjp); verify(pjp, times(1)).proceed(); }
private Object executeScheduledJob(final ProceedingJoinPoint pjp, final String type) throws Throwable { addTypeToStatCounter(type); JobExecutingData data = new JobExecutingData( type, statCounter.count(type)); String id = buildId(type, data.getProcessingThread()); events.active(id, data); SystemData retVal = null; long correlationId = r.nextLong(); eventBus.post(new JobStartEvent( correlationId, type)); try { retVal = Optional.ofNullable(((SystemData) pjp.proceed())) .map(sd -> sd.withCorrelationId(id)) .orElse(null); return retVal; } finally { logSystemEvent(pjp, type, data, retVal); retVal = Optional.ofNullable(retVal) .orElse(SystemData.builder() .correlationId("" + correlationId) .errors(0l) .processed(0l) .build()); eventBus.post(new JobCompleteEvent( correlationId, type, retVal.getErrors(), retVal.getProcessed())); } }
@Before public void setUp() throws Exception { data = SystemData.builder() .dataMap(Maps.newHashMap()) .errors(1) .processed(100) .build(); bus = new EventBus(); bus.register(this); jobs = new JobsBeingExecuted( bus, 10, JobName.Types.SIMPLE); pjp = Mockito.mock(ProceedingJoinPoint.class); }
@Around("execution(* com.oath.micro.server.events.ScheduledJob.scheduleAndLog(..))") public Object aroundScheduledJob(ProceedingJoinPoint pjp) throws Throwable { String type = jobNameType.getCreator() .getType(pjp.getSignature() .getDeclaringType()); return executeScheduledJob(pjp, type); }
@Test public void testActiveJobs() throws Throwable { ProceedingJoinPoint pjp = mock(ProceedingJoinPoint.class); Signature signature = mock(Signature.class); when(pjp.getSignature()).thenReturn(signature); when(pjp.getTarget()).thenReturn(this); when(signature.getDeclaringType()).thenReturn(ScheduledJob.class); jobs.aroundScheduledJob(pjp); MockAsyncResponse<String> response = new MockAsyncResponse<>(); active.activeJobs(response); assertThat(convert(response.response()).get("events"), is(1)); }
@Before public void setUp() throws Exception { bus = new EventBus(); queries1 = new RequestsBeingExecuted( bus); queries2 = new RequestsBeingExecuted( bus, "partition"); jobs = new JobsBeingExecuted( new EventBus(), 10, JobName.Types.SIMPLE); RequestTypes types = new RequestTypes( bus, true); types.getMap() .put(queries1.getType(), queries1); types.getMap() .put(queries2.getType(), queries2); active = new ActiveResource( types, jobs); }
@Bean public JobsBeingExecuted microEventJobsBeingExecuted() { return new JobsBeingExecuted( bus, maxLoggingCapacity, JobName.Types.SIMPLE); }