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.mbean;
018
019import java.io.InputStream;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.Comparator;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027import java.util.concurrent.TimeUnit;
028
029import javax.management.AttributeValueExp;
030import javax.management.MBeanServer;
031import javax.management.ObjectName;
032import javax.management.Query;
033import javax.management.QueryExp;
034import javax.management.StringValueExp;
035import javax.management.openmbean.CompositeData;
036import javax.management.openmbean.CompositeDataSupport;
037import javax.management.openmbean.CompositeType;
038import javax.management.openmbean.TabularData;
039import javax.management.openmbean.TabularDataSupport;
040
041import org.apache.camel.CamelContext;
042import org.apache.camel.ExtendedCamelContext;
043import org.apache.camel.ManagementStatisticsLevel;
044import org.apache.camel.Route;
045import org.apache.camel.RuntimeCamelException;
046import org.apache.camel.ServiceStatus;
047import org.apache.camel.TimerListener;
048import org.apache.camel.api.management.ManagedResource;
049import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
050import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
051import org.apache.camel.api.management.mbean.ManagedRouteMBean;
052import org.apache.camel.api.management.mbean.ManagedStepMBean;
053import org.apache.camel.api.management.mbean.RouteError;
054import org.apache.camel.model.Model;
055import org.apache.camel.model.RouteDefinition;
056import org.apache.camel.model.RoutesDefinition;
057import org.apache.camel.spi.InflightRepository;
058import org.apache.camel.spi.ManagementStrategy;
059import org.apache.camel.spi.RoutePolicy;
060import org.apache.camel.util.ObjectHelper;
061import org.slf4j.Logger;
062import org.slf4j.LoggerFactory;
063
064@ManagedResource(description = "Managed Route")
065public class ManagedRoute extends ManagedPerformanceCounter implements TimerListener, ManagedRouteMBean {
066
067    public static final String VALUE_UNKNOWN = "Unknown";
068
069    private static final Logger LOG = LoggerFactory.getLogger(ManagedRoute.class);
070
071    protected final Route route;
072    protected final String description;
073    protected final CamelContext context;
074    private final LoadTriplet load = new LoadTriplet();
075    private final String jmxDomain;
076
077    public ManagedRoute(CamelContext context, Route route) {
078        this.route = route;
079        this.context = context;
080        this.description = route.getDescription();
081        this.jmxDomain = context.getManagementStrategy().getManagementAgent().getMBeanObjectDomainName();
082    }
083
084    @Override
085    public void init(ManagementStrategy strategy) {
086        super.init(strategy);
087        boolean enabled = context.getManagementStrategy().getManagementAgent().getStatisticsLevel() != ManagementStatisticsLevel.Off;
088        setStatisticsEnabled(enabled);
089    }
090
091    public Route getRoute() {
092        return route;
093    }
094
095    public CamelContext getContext() {
096        return context;
097    }
098
099    @Override
100    public String getRouteId() {
101        String id = route.getId();
102        if (id == null) {
103            id = VALUE_UNKNOWN;
104        }
105        return id;
106    }
107
108    @Override
109    public String getRouteGroup() {
110        return route.getGroup();
111    }
112
113    @Override
114    public TabularData getRouteProperties() {
115        try {
116            final Map<String, Object> properties = route.getProperties();
117            final TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.camelRoutePropertiesTabularType());
118            final CompositeType ct = CamelOpenMBeanTypes.camelRoutePropertiesCompositeType();
119
120            // gather route properties
121            for (Map.Entry<String, Object> entry : properties.entrySet()) {
122                final String key = entry.getKey();
123                final String val = context.getTypeConverter().convertTo(String.class, entry.getValue());
124
125                CompositeData data = new CompositeDataSupport(
126                        ct,
127                        new String[]{"key", "value"},
128                        new Object[]{key, val}
129                );
130
131                answer.put(data);
132            }
133            return answer;
134        } catch (Exception e) {
135            throw RuntimeCamelException.wrapRuntimeCamelException(e);
136        }
137    }
138
139    @Override
140    public String getDescription() {
141        return description;
142    }
143
144    @Override
145    public String getEndpointUri() {
146        if (route.getEndpoint() != null) {
147            return route.getEndpoint().getEndpointUri();
148        }
149        return VALUE_UNKNOWN;
150    }
151
152    @Override
153    public String getState() {
154        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
155        ServiceStatus status = context.getRouteController().getRouteStatus(route.getId());
156        // if no status exists then its stopped
157        if (status == null) {
158            status = ServiceStatus.Stopped;
159        }
160        return status.name();
161    }
162
163    @Override
164    public String getUptime() {
165        return route.getUptime();
166    }
167
168    @Override
169    public long getUptimeMillis() {
170        return route.getUptimeMillis();
171    }
172
173    public Integer getInflightExchanges() {
174        return (int) super.getExchangesInflight();
175    }
176
177    @Override
178    public String getCamelId() {
179        return context.getName();
180    }
181
182    @Override
183    public String getCamelManagementName() {
184        return context.getManagementName();
185    }
186
187    @Override
188    public Boolean getTracing() {
189        return route.isTracing();
190    }
191
192    @Override
193    public void setTracing(Boolean tracing) {
194        route.setTracing(tracing);
195    }
196
197    @Override
198    public Boolean getMessageHistory() {
199        return route.isMessageHistory();
200    }
201
202    @Override
203    public Boolean getLogMask() {
204        return route.isLogMask();
205    }
206
207    @Override
208    public String getRoutePolicyList() {
209        List<RoutePolicy> policyList = route.getRoutePolicyList();
210
211        if (policyList == null || policyList.isEmpty()) {
212            // return an empty string to have it displayed nicely in JMX consoles
213            return "";
214        }
215
216        StringBuilder sb = new StringBuilder();
217        for (int i = 0; i < policyList.size(); i++) {
218            RoutePolicy policy = policyList.get(i);
219            sb.append(policy.getClass().getSimpleName());
220            sb.append("(").append(ObjectHelper.getIdentityHashCode(policy)).append(")");
221            if (i < policyList.size() - 1) {
222                sb.append(", ");
223            }
224        }
225        return sb.toString();
226    }
227
228    @Override
229    public String getLoad01() {
230        double load1 = load.getLoad1();
231        if (Double.isNaN(load1)) {
232            // empty string if load statistics is disabled
233            return "";
234        } else {
235            return String.format("%.2f", load1);
236        }
237    }
238
239    @Override
240    public String getLoad05() {
241        double load5 = load.getLoad5();
242        if (Double.isNaN(load5)) {
243            // empty string if load statistics is disabled
244            return "";
245        } else {
246            return String.format("%.2f", load5);
247        }
248    }
249
250    @Override
251    public String getLoad15() {
252        double load15 = load.getLoad15();
253        if (Double.isNaN(load15)) {
254            // empty string if load statistics is disabled
255            return "";
256        } else {
257            return String.format("%.2f", load15);
258        }
259    }
260
261    @Override
262    public void onTimer() {
263        load.update(getInflightExchanges());
264    }
265
266    @Override
267    public void start() throws Exception {
268        if (!context.getStatus().isStarted()) {
269            throw new IllegalArgumentException("CamelContext is not started");
270        }
271        context.getRouteController().startRoute(getRouteId());
272    }
273
274    @Override
275    public void stop() throws Exception {
276        if (!context.getStatus().isStarted()) {
277            throw new IllegalArgumentException("CamelContext is not started");
278        }
279        context.getRouteController().stopRoute(getRouteId());
280    }
281
282    @Override
283    public void stop(long timeout) throws Exception {
284        if (!context.getStatus().isStarted()) {
285            throw new IllegalArgumentException("CamelContext is not started");
286        }
287        context.getRouteController().stopRoute(getRouteId(), timeout, TimeUnit.SECONDS);
288    }
289
290    @Override
291    public boolean stop(Long timeout, Boolean abortAfterTimeout) throws Exception {
292        if (!context.getStatus().isStarted()) {
293            throw new IllegalArgumentException("CamelContext is not started");
294        }
295        return context.getRouteController().stopRoute(getRouteId(), timeout, TimeUnit.SECONDS, abortAfterTimeout);
296    }
297
298    public void shutdown() throws Exception {
299        if (!context.getStatus().isStarted()) {
300            throw new IllegalArgumentException("CamelContext is not started");
301        }
302        String routeId = getRouteId();
303        context.getRouteController().stopRoute(routeId);
304        context.removeRoute(routeId);
305    }
306
307    public void shutdown(long timeout) throws Exception {
308        if (!context.getStatus().isStarted()) {
309            throw new IllegalArgumentException("CamelContext is not started");
310        }
311        String routeId = getRouteId();
312        context.getRouteController().stopRoute(routeId, timeout, TimeUnit.SECONDS);
313        context.removeRoute(routeId);
314    }
315
316    @Override
317    public boolean remove() throws Exception {
318        if (!context.getStatus().isStarted()) {
319            throw new IllegalArgumentException("CamelContext is not started");
320        }
321        return context.removeRoute(getRouteId());
322    }
323
324    @Override
325    public void restart() throws Exception {
326        restart(1);
327    }
328
329    @Override
330    public void restart(long delay) throws Exception {
331        stop();
332        if (delay > 0) {
333            try {
334                LOG.debug("Sleeping {} seconds before starting route: {}", delay, getRouteId());
335                Thread.sleep(delay * 1000);
336            } catch (InterruptedException e) {
337                // ignore
338            }
339        }
340        start();
341    }
342
343    @Override
344    public String dumpRouteAsXml() throws Exception {
345        return dumpRouteAsXml(false, false);
346    }
347
348    @Override
349    public String dumpRouteAsXml(boolean resolvePlaceholders) throws Exception {
350        return dumpRouteAsXml(resolvePlaceholders, false);
351    }
352
353    @Override
354    public String dumpRouteAsXml(boolean resolvePlaceholders, boolean resolveDelegateEndpoints) throws Exception {
355        String id = route.getId();
356        RouteDefinition def = context.getExtension(Model.class).getRouteDefinition(id);
357        if (def != null) {
358            ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
359            return ecc.getModelToXMLDumper().dumpModelAsXml(context, def, resolvePlaceholders, resolveDelegateEndpoints);
360        }
361
362        return null;
363    }
364
365    @Override
366    public void updateRouteFromXml(String xml) throws Exception {
367        // convert to model from xml
368        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
369        InputStream is = context.getTypeConverter().convertTo(InputStream.class, xml);
370        RoutesDefinition routes = (RoutesDefinition) ecc.getXMLRoutesDefinitionLoader().loadRoutesDefinition(context, is);
371        if (routes == null || routes.getRoutes().isEmpty()) {
372            return;
373        }
374        RouteDefinition def = routes.getRoutes().get(0);
375
376        // if the xml does not contain the route-id then we fix this by adding the actual route id
377        // this may be needed if the route-id was auto-generated, as the intend is to update this route
378        // and not add a new route, adding a new route, use the MBean operation on ManagedCamelContext instead.
379        if (ObjectHelper.isEmpty(def.getId())) {
380            def.setId(getRouteId());
381        } else if (!def.getId().equals(getRouteId())) {
382            throw new IllegalArgumentException("Cannot update route from XML as routeIds does not match. routeId: "
383                    + getRouteId() + ", routeId from XML: " + def.getId());
384        }
385
386        LOG.debug("Updating route: {} from xml: {}", def.getId(), xml);
387
388        try {
389            // add will remove existing route first
390            context.getExtension(Model.class).addRouteDefinition(def);
391        } catch (Exception e) {
392            // log the error as warn as the management api may be invoked remotely over JMX which does not propagate such exception
393            String msg = "Error updating route: " + def.getId() + " from xml: " + xml + " due: " + e.getMessage();
394            LOG.warn(msg, e);
395            throw e;
396        }
397    }
398
399    @Override
400    public String dumpRouteStatsAsXml(boolean fullStats, boolean includeProcessors) throws Exception {
401        // in this logic we need to calculate the accumulated processing time for the processor in the route
402        // and hence why the logic is a bit more complicated to do this, as we need to calculate that from
403        // the bottom -> top of the route but this information is valuable for profiling routes
404        StringBuilder sb = new StringBuilder();
405
406        // need to calculate this value first, as we need that value for the route stat
407        long processorAccumulatedTime = 0L;
408
409        // gather all the processors for this route, which requires JMX
410        if (includeProcessors) {
411            sb.append("  <processorStats>\n");
412            MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
413            if (server != null) {
414                // get all the processor mbeans and sort them accordingly to their index
415                String prefix = getContext().getManagementStrategy().getManagementAgent().getIncludeHostName() ? "*/" : "";
416                ObjectName query = ObjectName.getInstance(jmxDomain + ":context=" + prefix + getContext().getManagementName() + ",type=processors,*");
417                Set<ObjectName> names = server.queryNames(query, null);
418                List<ManagedProcessorMBean> mps = new ArrayList<>();
419                for (ObjectName on : names) {
420                    ManagedProcessorMBean processor = context.getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedProcessorMBean.class);
421
422                    // the processor must belong to this route
423                    if (getRouteId().equals(processor.getRouteId())) {
424                        mps.add(processor);
425                    }
426                }
427                mps.sort(new OrderProcessorMBeans());
428
429                // walk the processors in reverse order, and calculate the accumulated total time
430                Map<String, Long> accumulatedTimes = new HashMap<>();
431                Collections.reverse(mps);
432                for (ManagedProcessorMBean processor : mps) {
433                    processorAccumulatedTime += processor.getTotalProcessingTime();
434                    accumulatedTimes.put(processor.getProcessorId(), processorAccumulatedTime);
435                }
436                // and reverse back again
437                Collections.reverse(mps);
438
439                // and now add the sorted list of processors to the xml output
440                for (ManagedProcessorMBean processor : mps) {
441                    sb.append("    <processorStat").append(String.format(" id=\"%s\" index=\"%s\" state=\"%s\"", processor.getProcessorId(), processor.getIndex(), processor.getState()));
442                    // do we have an accumulated time then append that
443                    Long accTime = accumulatedTimes.get(processor.getProcessorId());
444                    if (accTime != null) {
445                        sb.append(" accumulatedProcessingTime=\"").append(accTime).append("\"");
446                    }
447                    // use substring as we only want the attributes
448                    sb.append(" ").append(processor.dumpStatsAsXml(fullStats).substring(7)).append("\n");
449                }
450            }
451            sb.append("  </processorStats>\n");
452        }
453
454        // route self time is route total - processor accumulated total)
455        long routeSelfTime = getTotalProcessingTime() - processorAccumulatedTime;
456        if (routeSelfTime < 0) {
457            // ensure we don't calculate that as negative
458            routeSelfTime = 0;
459        }
460
461        StringBuilder answer = new StringBuilder();
462        answer.append("<routeStat").append(String.format(" id=\"%s\"", route.getId())).append(String.format(" state=\"%s\"", getState()));
463        // use substring as we only want the attributes
464        String stat = dumpStatsAsXml(fullStats);
465        answer.append(" exchangesInflight=\"").append(getInflightExchanges()).append("\"");
466        answer.append(" selfProcessingTime=\"").append(routeSelfTime).append("\"");
467        InflightRepository.InflightExchange oldest = getOldestInflightEntry();
468        if (oldest == null) {
469            answer.append(" oldestInflightExchangeId=\"\"");
470            answer.append(" oldestInflightDuration=\"\"");
471        } else {
472            answer.append(" oldestInflightExchangeId=\"").append(oldest.getExchange().getExchangeId()).append("\"");
473            answer.append(" oldestInflightDuration=\"").append(oldest.getDuration()).append("\"");
474        }
475        answer.append(" ").append(stat, 7, stat.length() - 2).append(">\n");
476
477        if (includeProcessors) {
478            answer.append(sb);
479        }
480
481        answer.append("</routeStat>");
482        return answer.toString();
483    }
484
485    @Override
486    public String dumpStepStatsAsXml(boolean fullStats) throws Exception {
487        // in this logic we need to calculate the accumulated processing time for the processor in the route
488        // and hence why the logic is a bit more complicated to do this, as we need to calculate that from
489        // the bottom -> top of the route but this information is valuable for profiling routes
490        StringBuilder sb = new StringBuilder();
491
492        // gather all the steps for this route, which requires JMX
493        sb.append("  <stepStats>\n");
494        MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
495        if (server != null) {
496            // get all the processor mbeans and sort them accordingly to their index
497            String prefix = getContext().getManagementStrategy().getManagementAgent().getIncludeHostName() ? "*/" : "";
498            ObjectName query = ObjectName.getInstance(jmxDomain + ":context=" + prefix + getContext().getManagementName() + ",type=steps,*");
499            Set<ObjectName> names = server.queryNames(query, null);
500            List<ManagedStepMBean> mps = new ArrayList<>();
501            for (ObjectName on : names) {
502                ManagedStepMBean step = context.getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedStepMBean.class);
503
504                // the step must belong to this route
505                if (getRouteId().equals(step.getRouteId())) {
506                    mps.add(step);
507                }
508            }
509            mps.sort(new OrderProcessorMBeans());
510
511            // and now add the sorted list of steps to the xml output
512            for (ManagedStepMBean step : mps) {
513                sb.append("    <stepStat").append(String.format(" id=\"%s\" index=\"%s\" state=\"%s\"", step.getProcessorId(), step.getIndex(), step.getState()));
514                // use substring as we only want the attributes
515                sb.append(" ").append(step.dumpStatsAsXml(fullStats).substring(7)).append("\n");
516            }
517        }
518        sb.append("  </stepStats>\n");
519
520        StringBuilder answer = new StringBuilder();
521        answer.append("<routeStat").append(String.format(" id=\"%s\"", route.getId())).append(String.format(" state=\"%s\"", getState()));
522        // use substring as we only want the attributes
523        String stat = dumpStatsAsXml(fullStats);
524        answer.append(" exchangesInflight=\"").append(getInflightExchanges()).append("\"");
525        InflightRepository.InflightExchange oldest = getOldestInflightEntry();
526        if (oldest == null) {
527            answer.append(" oldestInflightExchangeId=\"\"");
528            answer.append(" oldestInflightDuration=\"\"");
529        } else {
530            answer.append(" oldestInflightExchangeId=\"").append(oldest.getExchange().getExchangeId()).append("\"");
531            answer.append(" oldestInflightDuration=\"").append(oldest.getDuration()).append("\"");
532        }
533        answer.append(" ").append(stat, 7, stat.length() - 2).append(">\n");
534
535        answer.append(sb);
536
537        answer.append("</routeStat>");
538        return answer.toString();
539    }
540
541    @Override
542    public void reset(boolean includeProcessors) throws Exception {
543        reset();
544
545        // and now reset all processors for this route
546        if (includeProcessors) {
547            MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
548            if (server != null) {
549                // get all the processor mbeans and sort them accordingly to their index
550                String prefix = getContext().getManagementStrategy().getManagementAgent().getIncludeHostName() ? "*/" : "";
551                ObjectName query = ObjectName.getInstance(jmxDomain + ":context=" + prefix + getContext().getManagementName() + ",type=processors,*");
552                QueryExp queryExp = Query.match(new AttributeValueExp("RouteId"), new StringValueExp(getRouteId()));
553                Set<ObjectName> names = server.queryNames(query, queryExp);
554                for (ObjectName name : names) {
555                    server.invoke(name, "reset", null, null);
556                }
557            }
558        }
559    }
560
561    @Override
562    public boolean equals(Object o) {
563        return this == o || (o != null && getClass() == o.getClass() && route.equals(((ManagedRoute) o).route));
564    }
565
566    @Override
567    public int hashCode() {
568        return route.hashCode();
569    }
570
571    private InflightRepository.InflightExchange getOldestInflightEntry() {
572        return getContext().getInflightRepository().oldest(getRouteId());
573    }
574
575    @Override
576    public Long getOldestInflightDuration() {
577        InflightRepository.InflightExchange oldest = getOldestInflightEntry();
578        if (oldest == null) {
579            return null;
580        } else {
581            return oldest.getDuration();
582        }
583    }
584
585    @Override
586    public String getOldestInflightExchangeId() {
587        InflightRepository.InflightExchange oldest = getOldestInflightEntry();
588        if (oldest == null) {
589            return null;
590        } else {
591            return oldest.getExchange().getExchangeId();
592        }
593    }
594
595    @Override
596    public Boolean getHasRouteController() {
597        return route.getRouteController() != null;
598    }
599
600    @Override
601    public RouteError getLastError() {
602        org.apache.camel.spi.RouteError error = route.getLastError();
603        if (error == null) {
604            return null;
605        } else {
606            return new RouteError() {
607                @Override
608                public Phase getPhase() {
609                    if (error.getPhase() != null) {
610                        switch (error.getPhase()) {
611                            case START: return Phase.START;
612                            case STOP: return Phase.STOP;
613                            case SUSPEND: return Phase.SUSPEND;
614                            case RESUME: return Phase.RESUME;
615                            case SHUTDOWN: return Phase.SHUTDOWN;
616                            case REMOVE: return Phase.REMOVE;
617                            default: throw new IllegalStateException();
618                        }
619                    }
620                    return null;
621                }
622
623                @Override
624                public Throwable getException() {
625                    return error.getException();
626                }
627            };
628        }
629    }
630
631    /**
632     * Used for sorting the processor mbeans accordingly to their index.
633     */
634    private static final class OrderProcessorMBeans implements Comparator<ManagedProcessorMBean> {
635
636        @Override
637        public int compare(ManagedProcessorMBean o1, ManagedProcessorMBean o2) {
638            return o1.getIndex().compareTo(o2.getIndex());
639        }
640    }
641}