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.mbean;
018
019 import org.apache.camel.CamelContext;
020 import org.apache.camel.Endpoint;
021 import org.apache.camel.ManagementStatisticsLevel;
022 import org.apache.camel.Route;
023 import org.apache.camel.ServiceStatus;
024 import org.apache.camel.spi.RoutePolicy;
025 import org.apache.camel.util.ObjectHelper;
026 import org.springframework.jmx.export.annotation.ManagedAttribute;
027 import org.springframework.jmx.export.annotation.ManagedOperation;
028 import org.springframework.jmx.export.annotation.ManagedResource;
029
030 @ManagedResource(description = "Managed Route")
031 public class ManagedRoute extends ManagedPerformanceCounter {
032
033 public static final String VALUE_UNKNOWN = "Unknown";
034 private Route route;
035 private String description;
036 private CamelContext context;
037
038 public ManagedRoute(CamelContext context, Route route) {
039 this.route = route;
040 this.context = context;
041 this.description = route.toString();
042 boolean enabled = context.getManagementStrategy().getStatisticsLevel() != ManagementStatisticsLevel.Off;
043 setStatisticsEnabled(enabled);
044 }
045
046 public Route getRoute() {
047 return route;
048 }
049
050 public CamelContext getContext() {
051 return context;
052 }
053
054 @ManagedAttribute(description = "Route id")
055 public String getRouteId() {
056 String id = route.getId();
057 if (id == null) {
058 id = VALUE_UNKNOWN;
059 }
060 return id;
061 }
062
063 @ManagedAttribute(description = "Route Description")
064 public String getDescription() {
065 return description;
066 }
067
068 @ManagedAttribute(description = "Route Endpoint Uri")
069 public String getEndpointUri() {
070 Endpoint ep = route.getEndpoint();
071 return ep != null ? ep.getEndpointUri() : VALUE_UNKNOWN;
072 }
073
074 @ManagedAttribute(description = "Route State")
075 public String getState() {
076 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
077 ServiceStatus status = context.getRouteStatus(route.getId());
078 // if no status exists then its stopped
079 if (status == null) {
080 status = ServiceStatus.Stopped;
081 }
082 return status.name();
083 }
084
085 @ManagedAttribute(description = "Current number of inflight Exchanges")
086 public Integer getInflightExchanges() {
087 if (route.getEndpoint() != null) {
088 return context.getInflightRepository().size(route.getEndpoint());
089 } else {
090 return null;
091 }
092 }
093
094 @ManagedAttribute(description = "Camel id")
095 public String getCamelId() {
096 return context.getName();
097 }
098
099 @ManagedAttribute(description = "Tracing")
100 public Boolean getTracing() {
101 return route.getRouteContext().isTracing();
102 }
103
104 @ManagedAttribute(description = "Tracing")
105 public void setTracing(Boolean tracing) {
106 route.getRouteContext().setTracing(tracing);
107 }
108
109 @ManagedAttribute(description = "Route Policy")
110 public String getRoutePolicy() {
111 RoutePolicy policy = route.getRouteContext().getRoutePolicy();
112 if (policy != null) {
113 StringBuilder sb = new StringBuilder();
114 sb.append(policy.getClass().getSimpleName());
115 sb.append("(").append(ObjectHelper.getIdentityHashCode(policy)).append(")");
116 return sb.toString();
117 }
118 return null;
119 }
120
121 @ManagedOperation(description = "Start Route")
122 public void start() throws Exception {
123 context.startRoute(getRouteId());
124 }
125
126 @ManagedOperation(description = "Stop Route")
127 public void stop() throws Exception {
128 context.stopRoute(getRouteId());
129 }
130 }