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.management;
018
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.HashMap;
022 import java.util.List;
023 import java.util.Map;
024
025 import javax.management.JMException;
026 import javax.management.MalformedObjectNameException;
027 import javax.management.ObjectName;
028
029 import org.apache.camel.CamelContext;
030 import org.apache.camel.Endpoint;
031 import org.apache.camel.Exchange;
032 import org.apache.camel.Route;
033 import org.apache.camel.Service;
034 import org.apache.camel.impl.DefaultCamelContext;
035 import org.apache.camel.impl.ServiceSupport;
036 import org.apache.camel.model.ExceptionType;
037 import org.apache.camel.model.ProcessorType;
038 import org.apache.camel.model.RouteType;
039 import org.apache.camel.spi.InstrumentationAgent;
040 import org.apache.camel.spi.LifecycleStrategy;
041 import org.apache.camel.spi.RouteContext;
042 import org.apache.commons.logging.Log;
043 import org.apache.commons.logging.LogFactory;
044
045 /**
046 * JMX agent that registeres Camel lifecycle events in JMX.
047 *
048 * @version $Revision: 676133 $
049 *
050 */
051 public class InstrumentationLifecycleStrategy implements LifecycleStrategy {
052 private static final transient Log LOG = LogFactory.getLog(InstrumentationProcessor.class);
053
054 private InstrumentationAgent agent;
055 private CamelNamingStrategy namingStrategy;
056 private boolean initialized;
057
058 // A map (Endpoint -> InstrumentationProcessor) to facilitate
059 // adding per-route interceptor and registering ManagedRoute MBean
060 private Map<Endpoint, InstrumentationProcessor> interceptorMap =
061 new HashMap<Endpoint, InstrumentationProcessor>();
062
063 public InstrumentationLifecycleStrategy() {
064 this(new DefaultInstrumentationAgent());
065 }
066
067 public InstrumentationLifecycleStrategy(InstrumentationAgent agent) {
068 this.agent = agent;
069 }
070 /**
071 * Constructor for camel context that has been started.
072 *
073 * @param agent
074 * @param context
075 */
076 public InstrumentationLifecycleStrategy(InstrumentationAgent agent,
077 CamelContext context) {
078 this.agent = agent;
079 onContextStart(context);
080 }
081
082 public void onContextStart(CamelContext context) {
083 if (context instanceof DefaultCamelContext) {
084 try {
085 initialized = true;
086 DefaultCamelContext dc = (DefaultCamelContext)context;
087 // call addService so that context will start and stop the agent
088 dc.addService(agent);
089 namingStrategy = new CamelNamingStrategy(agent.getMBeanObjectDomainName());
090 ManagedService ms = new ManagedService(dc);
091 agent.register(ms, getNamingStrategy().getObjectName(dc));
092 } catch (Exception e) {
093 LOG.warn("Could not register CamelContext MBean", e);
094 }
095 }
096 }
097
098 public void onEndpointAdd(Endpoint<? extends Exchange> endpoint) {
099
100 // the agent hasn't been started
101 if (!initialized) {
102 return;
103 }
104
105 try {
106 ManagedEndpoint me = new ManagedEndpoint(endpoint);
107 agent.register(me, getNamingStrategy().getObjectName(me));
108 } catch (JMException e) {
109 LOG.warn("Could not register Endpoint MBean", e);
110 }
111 }
112
113 public void onRoutesAdd(Collection<Route> routes) {
114
115 // the agent hasn't been started
116 if (!initialized) {
117 return;
118 }
119
120 for (Route route : routes) {
121 try {
122 ManagedRoute mr = new ManagedRoute(route);
123 // retrieve the per-route intercept for this route
124 InstrumentationProcessor interceptor = interceptorMap.get(route.getEndpoint());
125 if (interceptor == null) {
126 LOG.warn("Instrumentation processor not found for route endpoint "
127 + route.getEndpoint());
128 } else {
129 interceptor.setCounter(mr);
130 }
131 agent.register(mr, getNamingStrategy().getObjectName(mr));
132 } catch (JMException e) {
133 LOG.warn("Could not register Route MBean", e);
134 }
135 }
136 }
137
138 public void onServiceAdd(CamelContext context, Service service) {
139
140 // the agent hasn't been started
141 if (!initialized) {
142 return;
143 }
144 if (service instanceof ServiceSupport) {
145 try {
146 ManagedService ms = new ManagedService((ServiceSupport)service);
147 agent.register(ms, getNamingStrategy().getObjectName(context, ms));
148 } catch (JMException e) {
149 LOG.warn("Could not register Service MBean", e);
150 }
151 }
152 }
153
154 public void onRouteContextCreate(RouteContext routeContext) {
155
156 // the agent hasn't been started
157 if (!initialized) {
158 return;
159 }
160
161 // Create a map (ProcessorType -> PerformanceCounter)
162 // to be passed to InstrumentationInterceptStrategy.
163 Map<ProcessorType, PerformanceCounter> counterMap =
164 new HashMap<ProcessorType, PerformanceCounter>();
165
166 // Each processor in a route will have its own performance counter
167 // The performance counter are MBeans that we register with MBeanServer.
168 // These performance counter will be embedded
169 // to InstrumentationProcessor and wrap the appropriate processor
170 // by InstrumentationInterceptStrategy.
171 RouteType route = routeContext.getRoute();
172
173 for (ProcessorType processor : route.getOutputs()) {
174 ObjectName name = null;
175 try {
176 // get the mbean name
177 name = getNamingStrategy().getObjectName(routeContext, processor);
178
179 // register mbean wrapped in the performance counter mbean
180 PerformanceCounter pc = new PerformanceCounter();
181 agent.register(pc, name);
182
183 // add to map now that it has ben registered
184 counterMap.put(processor, pc);
185 } catch (MalformedObjectNameException e) {
186 LOG.warn("Could not create MBean name: " + name, e);
187 } catch (JMException e) {
188 LOG.warn("Could not register PerformanceCounter MBean: " + name, e);
189 }
190 }
191
192 routeContext.addInterceptStrategy(new InstrumentationInterceptStrategy(counterMap));
193
194 routeContext.setErrorHandlerWrappingStrategy(
195 new InstrumentationErrorHandlerWrappingStrategy(counterMap));
196
197 // Add an InstrumentationProcessor at the beginning of each route and
198 // set up the interceptorMap for onRoutesAdd() method to register the
199 // ManagedRoute MBeans.
200
201 RouteType routeType = routeContext.getRoute();
202 if (routeType.getInputs() != null && !routeType.getInputs().isEmpty()) {
203 if (routeType.getInputs().size() > 1) {
204 LOG.warn("Add InstrumentationProcessor to first input only.");
205 }
206
207 Endpoint endpoint = routeType.getInputs().get(0).getEndpoint();
208
209 List<ProcessorType<?>> exceptionHandlers = new ArrayList<ProcessorType<?>>();
210 List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>();
211
212 // separate out the exception handers in the outputs
213 for (ProcessorType output : routeType.getOutputs()) {
214 if (output instanceof ExceptionType) {
215 exceptionHandlers.add(output);
216 } else {
217 outputs.add(output);
218 }
219 }
220
221 // clearing the outputs
222 routeType.clearOutput();
223
224 // add exception handlers as top children
225 routeType.getOutputs().addAll(exceptionHandlers);
226
227 // add an interceptor
228 InstrumentationProcessor processor = new InstrumentationProcessor();
229 routeType.intercept(processor);
230
231 // add the output
232 for (ProcessorType<?> processorType : outputs) {
233 routeType.addOutput(processorType);
234 }
235
236 interceptorMap.put(endpoint, processor);
237 }
238
239 }
240
241 public CamelNamingStrategy getNamingStrategy() {
242 return namingStrategy;
243 }
244
245 public void setNamingStrategy(CamelNamingStrategy strategy) {
246 this.namingStrategy = strategy;
247 }
248
249 public void setAgent(InstrumentationAgent agent) {
250 this.agent = agent;
251 }
252 }