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    @Override
041    public void setNotificationBroadcaster(NotificationBroadcasterSupport broadcaster) {
042        notificationBroadcaster = broadcaster;
043    }
044
045    @Override
046    public void notify(CamelEvent 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    @Override
061    public boolean isEnabled(CamelEvent event) {
062        return true;
063    }
064
065    @Override
066    protected void doStart() throws Exception {
067        counter.set(0);
068    }
069
070    public String getSource() {
071        return source;
072    }
073
074    /**
075     * Sets the source to be used when broadcasting events. The source is just a readable identifier which helps the
076     * receiver see where the event is coming from. You can assign a value such a server or application name etc.
077     * <p/>
078     * By default <tt>Camel</tt> will be used as source.
079     *
080     * @param source the source
081     */
082    public void setSource(String source) {
083        this.source = source;
084    }
085}