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