File tree Expand file tree Collapse file tree
main/java/org/javaee7/batch/listeners
test/java/org.javaee7.batch.listeners Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77 <version >1.0-SNAPSHOT</version >
88 <relativePath >../pom.xml</relativePath >
99 </parent >
10-
10+
1111 <artifactId >batch-listeners</artifactId >
1212 <packaging >war</packaging >
13+
14+ <dependencies >
15+ <dependency >
16+ <groupId >org.javaee7</groupId >
17+ <artifactId >util-samples</artifactId >
18+ </dependency >
19+ </dependencies >
1320</project >
Original file line number Diff line number Diff line change 1+ package org .javaee7 .batch .listeners ;
2+
3+ import javax .interceptor .InterceptorBinding ;
4+ import java .lang .annotation .ElementType ;
5+ import java .lang .annotation .Inherited ;
6+ import java .lang .annotation .Retention ;
7+ import java .lang .annotation .RetentionPolicy ;
8+ import java .lang .annotation .Target ;
9+
10+ /**
11+ * @author Roberto Cortez
12+ */
13+ @ Inherited
14+ @ InterceptorBinding
15+ @ Retention (RetentionPolicy .RUNTIME )
16+ @ Target ({ElementType .METHOD , ElementType .TYPE })
17+ public @interface BatchListener {}
Original file line number Diff line number Diff line change 1+ package org .javaee7 .batch .listeners ;
2+
3+ import javax .annotation .Priority ;
4+ import javax .inject .Inject ;
5+ import javax .interceptor .AroundInvoke ;
6+ import javax .interceptor .Interceptor ;
7+ import javax .interceptor .InvocationContext ;
8+
9+ /**
10+ * @author Roberto Cortez
11+ */
12+ @ BatchListener
13+ @ Interceptor
14+ @ Priority (Interceptor .Priority .APPLICATION )
15+ public class BatchListenerInterceptor {
16+ @ Inject
17+ private BatchListenerRecorder batchListenerRecorder ;
18+
19+ @ AroundInvoke
20+ private Object recordListenersExecution (InvocationContext ctx ) throws Exception {
21+ batchListenerRecorder .addListenerMethodExecution (ctx .getMethod ().getDeclaringClass (),
22+ ctx .getMethod ().getName ());
23+ return ctx .proceed ();
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package org .javaee7 .batch .listeners ;
2+
3+ import javax .inject .Singleton ;
4+ import java .util .HashMap ;
5+ import java .util .Map ;
6+
7+ /**
8+ * @author Roberto Cortez
9+ */
10+ @ Singleton
11+ public class BatchListenerRecorder {
12+ private Map <String , Integer > recordedMethodExecutions = new HashMap <>();
13+
14+ public void addListenerMethodExecution (Class klass , String method ) {
15+ String key = getKey (klass , method );
16+ if (recordedMethodExecutions .containsKey (key )) {
17+ recordedMethodExecutions .put (key , recordedMethodExecutions .get (key ) + 1 );
18+ } else {
19+ recordedMethodExecutions .put (key , 1 );
20+ }
21+ }
22+
23+ public boolean isListenerMethodExecuted (Class klass , String method ) {
24+ return recordedMethodExecutions .containsKey (getKey (klass , method ));
25+ }
26+
27+ public Integer totalListenerMethodExecutions (Class klass , String method ) {
28+ Integer total = recordedMethodExecutions .get (getKey (klass , method ));
29+ return total != null ? total : Integer .valueOf (0 );
30+ }
31+
32+ private String getKey (Class klass , String method ) {
33+ return klass .getName () + "." + method ;
34+ }
35+ }
Original file line number Diff line number Diff line change 5050public class MyChunkListener extends AbstractChunkListener {
5151
5252 @ Override
53+ @ BatchListener
5354 public void beforeChunk () throws Exception {
5455 System .out .println ("MyChunkListener.beforeChunk" );
5556 }
5657
5758 @ Override
59+ @ BatchListener
5860 public void afterChunk () throws Exception {
5961 System .out .println ("MyChunkListener.afterChunk" );
6062 }
Original file line number Diff line number Diff line change 5050public class MyItemProcessorListener extends AbstractItemProcessListener {
5151
5252 @ Override
53+ @ BatchListener
5354 public void beforeProcess (Object item ) throws Exception {
5455 System .out .println ("MyItemProcessorListener.beforeProcess: " + item );
5556 }
5657
5758 @ Override
59+ @ BatchListener
5860 public void afterProcess (Object item , Object result ) throws Exception {
5961 System .out .println ("MyItemProcessorListener.afterProcess: " + item + ", " + result );
6062 }
6163
6264 @ Override
65+ @ BatchListener
6366 public void onProcessError (Object item , Exception ex ) throws Exception {
6467 System .out .println ("MyItemProcessorListener.onProcessError: " + item + ", " + ex .getLocalizedMessage ());
6568 }
66-
6769}
Original file line number Diff line number Diff line change 5050public class MyItemReadListener extends AbstractItemReadListener {
5151
5252 @ Override
53+ @ BatchListener
5354 public void beforeRead () throws Exception {
5455 System .out .println ("MyItemReadListener.beforeRead" );
5556 }
5657
5758 @ Override
59+ @ BatchListener
5860 public void afterRead (Object item ) throws Exception {
5961 System .out .println ("MyItemReadListener.afterRead: " + item );
6062 }
6163
6264 @ Override
65+ @ BatchListener
6366 public void onReadError (Exception ex ) throws Exception {
6467 System .out .println ("MyItemReadListener.onReadError: " + ex .getLocalizedMessage ());
6568 }
66-
67-
6869}
Original file line number Diff line number Diff line change 5151public class MyItemWriteListener extends AbstractItemWriteListener {
5252
5353 @ Override
54+ @ BatchListener
5455 public void beforeWrite (List items ) throws Exception {
5556 System .out .println ("MyItemWriteListener.beforeWrite: " + items );
5657 }
5758
5859 @ Override
60+ @ BatchListener
5961 public void afterWrite (List items ) throws Exception {
6062 System .out .println ("MyItemWriteListener.afterWrite: " + items );
6163 }
6264
6365 @ Override
66+ @ BatchListener
6467 public void onWriteError (List items , Exception ex ) throws Exception {
6568 System .out .println ("MyItemWriteListener.onError: " + items + ", " + ex .getLocalizedMessage ());
6669 }
67-
6870}
Original file line number Diff line number Diff line change 5050public class MyJobListener extends AbstractJobListener {
5151
5252 @ Override
53+ @ BatchListener
5354 public void beforeJob () {
5455 System .out .println ("MyJobListener.beforeJob" );
5556 }
5657
5758 @ Override
59+ @ BatchListener
5860 public void afterJob () {
5961 System .out .println ("MyJobListener.afterJob" );
6062 }
61-
6263}
Original file line number Diff line number Diff line change 5050public class MyStepListener extends AbstractStepListener {
5151
5252 @ Override
53+ @ BatchListener
5354 public void beforeStep () throws Exception {
5455 System .out .println ("MyStepListener.beforeStep" );
5556 }
5657
5758 @ Override
59+ @ BatchListener
5860 public void afterStep () throws Exception {
5961 System .out .println ("MyStepListener.afterStep" );
6062 }
61-
6263}
You can’t perform that action at this time.
0 commit comments