001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.management; 018 019import java.util.EventObject; 020import java.util.concurrent.atomic.AtomicLong; 021 022import javax.management.Notification; 023import javax.management.NotificationBroadcasterSupport; 024 025import org.apache.camel.api.management.JmxNotificationBroadcasterAware; 026import org.apache.camel.spi.EventNotifier; 027import org.apache.camel.support.EventNotifierSupport; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * A JMX based {@link EventNotifier} which broadcasts JMX {@link Notification}s. 033 * 034 * @version 035 */ 036public class JmxNotificationEventNotifier extends EventNotifierSupport implements JmxNotificationBroadcasterAware { 037 private static final Logger LOG = LoggerFactory.getLogger(JmxNotificationEventNotifier.class); 038 private final AtomicLong counter = new AtomicLong(); 039 private NotificationBroadcasterSupport notificationBroadcaster; 040 private String source = "Camel"; 041 042 public void setNotificationBroadcaster(NotificationBroadcasterSupport broadcaster) { 043 notificationBroadcaster = broadcaster; 044 } 045 046 public void notify(EventObject event) throws Exception { 047 if (notificationBroadcaster != null) { 048 // its recommended to send light weight events and we don't want to have the entire Exchange/CamelContext etc 049 // serialized as these are the typical source of the EventObject. So we use our own source which is just 050 // a human readable name, which can be configured. 051 String type = event.getClass().getSimpleName(); 052 String message = event.toString(); 053 Notification notification = new Notification(type, source, counter.getAndIncrement(), message); 054 055 LOG.trace("Broadcasting JMX notification: {}", notification); 056 notificationBroadcaster.sendNotification(notification); 057 } 058 } 059 060 public boolean isEnabled(EventObject event) { 061 return true; 062 } 063 064 protected void doStart() throws Exception { 065 counter.set(0); 066 } 067 068 protected void doStop() throws Exception { 069 // noop 070 } 071 072 public String getSource() { 073 return source; 074 } 075 076 /** 077 * Sets the source to be used when broadcasting events. 078 * The source is just a readable identifier which helps the receiver see where the event is coming from. 079 * You can assign a value such a server or application name etc. 080 * <p/> 081 * By default <tt>Camel</tt> will be used as source. 082 * 083 * @param source the source 084 */ 085 public void setSource(String source) { 086 this.source = source; 087 } 088}