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: 640438 $ 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 /** 056 * @return a Producer 057 * @throws Exception 058 * @see org.apache.camel.Endpoint#createProducer() 059 */ 060 public Producer<JMXExchange> createProducer() throws Exception { 061 throw new RuntimeException("Not supported"); 062 } 063 064 /** 065 * @param proc 066 * @return a Consumer 067 * @throws Exception 068 * @see org.apache.camel.Endpoint#createConsumer(org.apache.camel.Processor) 069 */ 070 public Consumer<JMXExchange> createConsumer(Processor proc) throws Exception { 071 ObjectName observedName = new ObjectName(observedObjectName); 072 if (name == null) { 073 String type = observedName.getKeyProperty("type"); 074 type = type != null ? type : "UNKNOWN"; 075 name = mbeanServer.getDefaultDomain() + ":type=CounterMonitor_" + type; 076 } 077 JMXConsumer result = new JMXConsumer(this, proc); 078 ourName = new ObjectName(name); 079 counterMonitor.setNotify(true); 080 counterMonitor.addObservedObject(observedName); 081 counterMonitor.setObservedAttribute(attributeName); 082 counterMonitor.setGranularityPeriod(granularityPeriod); 083 counterMonitor.setDifferenceMode(false); 084 counterMonitor.setInitThreshold(threshold); 085 counterMonitor.setOffset(offset); 086 mbeanServer.registerMBean(counterMonitor, ourName); 087 mbeanServer.addNotificationListener(ourName, result, null, new Object()); 088 return result; 089 } 090 091 public boolean isSingleton() { 092 return true; 093 } 094 095 public JMXExchange createExchange(Notification notification) { 096 return new JMXExchange(getContext(), getExchangePattern(), notification); 097 } 098 099 public JMXExchange createExchange() { 100 return new JMXExchange(getContext(), getExchangePattern(), null); 101 } 102 103 public JMXExchange createExchange(ExchangePattern pattern) { 104 return new JMXExchange(getContext(), pattern, null); 105 } 106 107 public String getAttributeName() { 108 return attributeName; 109 } 110 111 public void setAttributeName(String attributeName) { 112 this.attributeName = attributeName; 113 } 114 115 public long getGranularityPeriod() { 116 return granularityPeriod; 117 } 118 119 public void setGranularityPeriod(long granularityPeriod) { 120 this.granularityPeriod = granularityPeriod; 121 } 122 123 public String getName() { 124 return name; 125 } 126 127 public void setName(String name) { 128 this.name = name; 129 } 130 131 public Number getOffset() { 132 return offset; 133 } 134 135 public void setOffset(Number offset) { 136 this.offset = offset; 137 } 138 139 public Number getThreshold() { 140 return threshold; 141 } 142 143 public void setThreshold(Number threshold) { 144 this.threshold = threshold; 145 } 146 147 public MBeanServer getMbeanServer() { 148 return mbeanServer; 149 } 150 151 public void setMbeanServer(MBeanServer mbeanServer) { 152 this.mbeanServer = mbeanServer; 153 } 154 }