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.component.jmx;
018
019 import javax.management.MBeanServer;
020 import javax.management.Notification;
021 import javax.management.ObjectName;
022 import javax.management.monitor.CounterMonitor;
023 import org.apache.camel.Consumer;
024 import org.apache.camel.Processor;
025 import org.apache.camel.Producer;
026 import org.apache.camel.impl.DefaultEndpoint;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029
030 /**
031 * Creates a CounterMonitor for jmx attributes
032 *
033 * @version $Revision: 523016 $
034 */
035 public class JMXEndpoint extends DefaultEndpoint<JMXExchange> {
036
037 private static final Log LOG = LogFactory.getLog(JMXEndpoint.class);
038 private String name;
039 private ObjectName ourName;
040 private String observedObjectName;
041 private String attributeName;
042 private long granularityPeriod = 5000;
043 private Number threshold;
044 private Number offset;
045 private MBeanServer mbeanServer;
046 private CounterMonitor counterMonitor = new CounterMonitor();
047
048 protected JMXEndpoint(String endpointUri, JMXComponent component) {
049 super(endpointUri, component);
050 observedObjectName = endpointUri;
051 }
052
053 /**
054 * @return a Producer
055 * @throws Exception
056 * @see org.apache.camel.Endpoint#createProducer()
057 */
058 public Producer<JMXExchange> createProducer() throws Exception {
059 throw new RuntimeException("Not supported");
060 }
061
062 /**
063 * @param proc
064 * @return a Consumer
065 * @throws Exception
066 * @see org.apache.camel.Endpoint#createConsumer(org.apache.camel.Processor)
067 */
068 public Consumer<JMXExchange> createConsumer(Processor proc) throws Exception {
069 ObjectName observedName = new ObjectName(observedObjectName);
070 if (name == null) {
071 String type = observedName.getKeyProperty("type");
072 type = type != null ? type : "UNKNOWN";
073 name = mbeanServer.getDefaultDomain() + ":type=CounterMonitor_" + type;
074 }
075 JMXConsumer result = new JMXConsumer(this, proc);
076 ourName = new ObjectName(name);
077 counterMonitor.setNotify(true);
078 counterMonitor.addObservedObject(observedName);
079 counterMonitor.setObservedAttribute(attributeName);
080 counterMonitor.setGranularityPeriod(granularityPeriod);
081 counterMonitor.setDifferenceMode(false);
082 counterMonitor.setInitThreshold(threshold);
083 counterMonitor.setOffset(offset);
084 mbeanServer.registerMBean(counterMonitor, ourName);
085 mbeanServer.addNotificationListener(ourName, result, null, new Object());
086 return result;
087 }
088
089 public boolean isSingleton() {
090 return true;
091 }
092
093 public JMXExchange createExchange(Notification notification) {
094 return new JMXExchange(getContext(), notification);
095 }
096
097 public JMXExchange createExchange() {
098 return new JMXExchange(getContext(), null);
099 }
100
101 public String getAttributeName() {
102 return attributeName;
103 }
104
105 public void setAttributeName(String attributeName) {
106 this.attributeName = attributeName;
107 }
108
109 public long getGranularityPeriod() {
110 return granularityPeriod;
111 }
112
113 public void setGranularityPeriod(long granularityPeriod) {
114 this.granularityPeriod = granularityPeriod;
115 }
116
117 public String getName() {
118 return name;
119 }
120
121 public void setName(String name) {
122 this.name = name;
123 }
124
125 public Number getOffset() {
126 return offset;
127 }
128
129 public void setOffset(Number offset) {
130 this.offset = offset;
131 }
132
133 public Number getThreshold() {
134 return threshold;
135 }
136
137 public void setThreshold(Number threshold) {
138 this.threshold = threshold;
139 }
140
141 public MBeanServer getMbeanServer() {
142 return mbeanServer;
143 }
144
145 public void setMbeanServer(MBeanServer mbeanServer) {
146 this.mbeanServer = mbeanServer;
147 }
148 }