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.Route;
021 import org.apache.camel.Service;
022 import org.apache.camel.ServiceStatus;
023 import org.apache.camel.SuspendableService;
024 import org.apache.camel.impl.ServiceSupport;
025 import org.apache.camel.spi.ManagementStrategy;
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 Service")
031 public class ManagedService {
032
033 private CamelContext context;
034 private Service service;
035 private Route route;
036
037 public ManagedService(CamelContext context, Service service) {
038 this.context = context;
039 this.service = service;
040 }
041
042 public void init(ManagementStrategy strategy) {
043 // do nothing
044 }
045
046 public Service getService() {
047 return service;
048 }
049
050 public CamelContext getContext() {
051 return context;
052 }
053
054 public Route getRoute() {
055 return route;
056 }
057
058 public void setRoute(Route route) {
059 this.route = route;
060 }
061
062 @ManagedAttribute(description = "Service State")
063 public String getState() {
064 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
065 if (service instanceof ServiceSupport) {
066 ServiceStatus status = ((ServiceSupport) service).getStatus();
067 // if no status exists then its stopped
068 if (status == null) {
069 status = ServiceStatus.Stopped;
070 }
071 return status.name();
072 }
073
074 // assume started if not a ServiceSupport instance
075 return ServiceStatus.Started.name();
076 }
077
078 @ManagedAttribute(description = "Camel id")
079 public String getCamelId() {
080 return context.getName();
081 }
082
083 @ManagedAttribute(description = "Route id")
084 public String getRouteId() {
085 if (route != null) {
086 return route.getId();
087 }
088 return null;
089 }
090
091 @ManagedOperation(description = "Start Service")
092 public void start() throws Exception {
093 service.start();
094 }
095
096 @ManagedOperation(description = "Stop Service")
097 public void stop() throws Exception {
098 service.stop();
099 }
100
101 @ManagedAttribute(description = "Whether this service supports suspension")
102 public boolean isSupportSuspension() {
103 return service instanceof SuspendableService;
104 }
105
106 @ManagedAttribute(description = "Whether this service is suspended")
107 public boolean isSuspended() {
108 if (service instanceof SuspendableService) {
109 SuspendableService ss = (SuspendableService) service;
110 return ss.isSuspended();
111 } else {
112 return false;
113 }
114 }
115
116 @ManagedOperation(description = "Suspend Service")
117 public void suspend() throws Exception {
118 if (service instanceof SuspendableService) {
119 SuspendableService ss = (SuspendableService) service;
120 ss.suspend();
121 } else {
122 throw new UnsupportedOperationException("suspend() is not a supported operation");
123 }
124 }
125
126 @ManagedOperation(description = "Resume Service")
127 public void resume() throws Exception {
128 if (service instanceof SuspendableService) {
129 SuspendableService ss = (SuspendableService) service;
130 ss.resume();
131 } else {
132 throw new UnsupportedOperationException("resume() is not a supported operation");
133 }
134 }
135
136 }