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 */
017 package org.apache.camel.management;
018
019 import java.util.EventObject;
020 import java.util.concurrent.atomic.AtomicLong;
021 import javax.management.Notification;
022 import javax.management.NotificationBroadcasterSupport;
023
024 import org.apache.camel.api.management.JmxNotificationBroadcasterAware;
025 import org.apache.camel.spi.EventNotifier;
026 import org.apache.camel.support.EventNotifierSupport;
027 import org.slf4j.Logger;
028 import org.slf4j.LoggerFactory;
029
030 /**
031 * A JMX based {@link EventNotifier} which broadcasts JMX {@link Notification}s.
032 *
033 * @version
034 */
035 public class JmxNotificationEventNotifier extends EventNotifierSupport implements JmxNotificationBroadcasterAware {
036 private static final transient Logger LOG = LoggerFactory.getLogger(JmxNotificationEventNotifier.class);
037 private final AtomicLong counter = new AtomicLong();
038 private NotificationBroadcasterSupport notificationBroadcaster;
039 private String source = "Camel";
040
041 public void setNotificationBroadcaster(NotificationBroadcasterSupport broadcaster) {
042 notificationBroadcaster = broadcaster;
043 }
044
045 public void notify(EventObject event) throws Exception {
046 if (notificationBroadcaster != null) {
047 // its recommended to send light weight events and we don't want to have the entire Exchange/CamelContext etc
048 // serialized as these are the typical source of the EventObject. So we use our own source which is just
049 // a human readable name, which can be configured.
050 String type = event.getClass().getSimpleName();
051 String message = event.toString();
052 Notification notification = new Notification(type, source, counter.getAndIncrement(), message);
053
054 LOG.trace("Broadcasting JMX notification: {}", notification);
055 notificationBroadcaster.sendNotification(notification);
056 }
057 }
058
059 public boolean isEnabled(EventObject event) {
060 return true;
061 }
062
063 protected void doStart() throws Exception {
064 counter.set(0);
065 }
066
067 protected void doStop() throws Exception {
068 // noop
069 }
070
071 public String getSource() {
072 return source;
073 }
074
075 /**
076 * Sets the source to be used when broadcasting events.
077 * The source is just a readable identifier which helps the receiver see where the event is coming from.
078 * You can assign a value such a server or application name etc.
079 * <p/>
080 * By default <tt>Camel</tt> will be used as source.
081 *
082 * @param source the source
083 */
084 public void setSource(String source) {
085 this.source = source;
086 }
087 }