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