1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.sample.jmx;
18
19 import org.aspectj.lang.JoinPoint;
20 import org.springframework.batch.core.StepExecution;
21 import org.springframework.context.ApplicationEvent;
22 import org.springframework.context.ApplicationEventPublisher;
23 import org.springframework.context.ApplicationEventPublisherAware;
24
25
26
27
28
29
30
31 public class StepExecutionApplicationEventAdvice implements ApplicationEventPublisherAware {
32
33 private ApplicationEventPublisher applicationEventPublisher;
34
35
36
37
38
39 public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
40 this.applicationEventPublisher = applicationEventPublisher;
41 }
42
43 public void before(JoinPoint jp, StepExecution stepExecution) {
44 String msg = "Before: " + jp.toShortString() + " with: " + stepExecution;
45 publish(jp.getTarget(), msg);
46 }
47
48 public void after(JoinPoint jp, StepExecution stepExecution) {
49 String msg = "After: " + jp.toShortString() + " with: " + stepExecution;
50 publish(jp.getTarget(), msg);
51 }
52
53 public void onError(JoinPoint jp, StepExecution stepExecution, Throwable t) {
54 String msg = "Error in: " + jp.toShortString() + " with: " + stepExecution + " (" + t.getClass() + ":" + t.getMessage() + ")";
55 publish(jp.getTarget(), msg);
56 }
57
58
59
60
61
62 private void publish(Object source, String message) {
63 applicationEventPublisher.publishEvent(new SimpleMessageApplicationEvent(source, message));
64 }
65
66 }