001/* 002 * Copyright 2006-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.sample.jmx; 018 019import javax.management.Notification; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.springframework.context.ApplicationEvent; 024import org.springframework.context.ApplicationListener; 025import org.springframework.jmx.export.notification.NotificationPublisher; 026import org.springframework.jmx.export.notification.NotificationPublisherAware; 027 028/** 029 * JMX notification broadcaster 030 * 031 * @author Dave Syer 032 * @since 1.0 033 */ 034public class JobExecutionNotificationPublisher implements ApplicationListener<SimpleMessageApplicationEvent>, NotificationPublisherAware { 035 private static final Log LOG = LogFactory.getLog(JobExecutionNotificationPublisher.class); 036 037 private NotificationPublisher notificationPublisher; 038 039 private int notificationCount = 0; 040 041 /** 042 * Injection setter. 043 * 044 * @see org.springframework.jmx.export.notification.NotificationPublisherAware#setNotificationPublisher(org.springframework.jmx.export.notification.NotificationPublisher) 045 */ 046 @Override 047 public void setNotificationPublisher(NotificationPublisher notificationPublisher) { 048 this.notificationPublisher = notificationPublisher; 049 } 050 051 /** 052 * If the event is a {@link SimpleMessageApplicationEvent} for open and 053 * close we log the event at INFO level and send a JMX notification if we 054 * are also an MBean. 055 * 056 * @see ApplicationListener#onApplicationEvent(ApplicationEvent) 057 */ 058 @Override 059 public void onApplicationEvent(SimpleMessageApplicationEvent applicationEvent) { 060 String message = applicationEvent.toString(); 061 LOG.info(message); 062 publish(message); 063 } 064 065 /** 066 * Publish the provided message to an external listener if there is one. 067 * 068 * @param message the message to publish 069 */ 070 private void publish(String message) { 071 if (notificationPublisher != null) { 072 Notification notification = new Notification("JobExecutionApplicationEvent", this, notificationCount++, 073 message); 074 /* 075 * We can't create a notification with a null source, but we can set 076 * it to null after creation(!). We want it to be null so that 077 * Spring will replace it automatically with the ObjectName (in 078 * ModelMBeanNotificationPublisher). 079 */ 080 notification.setSource(null); 081 notificationPublisher.sendNotification(notification); 082 } 083 } 084}