1 /*
2 * Copyright 2006-2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.springframework.batch.sample.jmx;
18
19 import javax.management.Notification;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.springframework.context.ApplicationEvent;
24 import org.springframework.context.ApplicationListener;
25 import org.springframework.jmx.export.notification.NotificationPublisher;
26 import org.springframework.jmx.export.notification.NotificationPublisherAware;
27
28 /**
29 * JMX notification broadcaster
30 *
31 * @author Dave Syer
32 * @since 1.0
33 */
34 public class JobExecutionNotificationPublisher implements ApplicationListener, NotificationPublisherAware {
35
36 protected static final Log logger = LogFactory.getLog(JobExecutionNotificationPublisher.class);
37
38 private NotificationPublisher notificationPublisher;
39
40 private int notificationCount = 0;
41
42 /**
43 * Injection setter.
44 *
45 * @see org.springframework.jmx.export.notification.NotificationPublisherAware#setNotificationPublisher(org.springframework.jmx.export.notification.NotificationPublisher)
46 */
47 public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
48 this.notificationPublisher = notificationPublisher;
49 }
50
51 /**
52 * If the event is a {@link SimpleMessageApplicationEvent} for open and
53 * close we log the event at INFO level and send a JMX notification if we
54 * are also an MBean.
55 *
56 * @see ApplicationListener#onApplicationEvent(ApplicationEvent)
57 */
58 public void onApplicationEvent(ApplicationEvent applicationEvent) {
59 if (applicationEvent instanceof SimpleMessageApplicationEvent) {
60 String message = applicationEvent.toString();
61 logger.info(message);
62 publish(message);
63 }
64 }
65
66 /**
67 * Publish the provided message to an external listener if there is one.
68 *
69 * @param message the message to publish
70 */
71 private void publish(String message) {
72 if (notificationPublisher != null) {
73 Notification notification = new Notification("JobExecutionApplicationEvent", this, notificationCount++,
74 message);
75 /*
76 * We can't create a notification with a null source, but we can set
77 * it to null after creation(!). We want it to be null so that
78 * Spring will replace it automatically with the ObjectName (in
79 * ModelMBeanNotificationPublisher).
80 */
81 notification.setSource(null);
82 notificationPublisher.sendNotification(notification);
83 }
84 }
85
86 }