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 java.util.Map;
020 import java.util.concurrent.TimeUnit;
021
022 import org.apache.camel.CamelContext;
023 import org.apache.camel.ProducerTemplate;
024 import org.apache.camel.ServiceStatus;
025 import org.apache.camel.impl.ServiceSupport;
026 import org.apache.camel.spi.ManagementStrategy;
027 import org.springframework.jmx.export.annotation.ManagedAttribute;
028 import org.springframework.jmx.export.annotation.ManagedOperation;
029 import org.springframework.jmx.export.annotation.ManagedResource;
030
031 /**
032 * @version $Revision: 892595 $
033 */
034 @ManagedResource(description = "Managed CamelContext")
035 public class ManagedCamelContext {
036
037 private CamelContext context;
038
039 public ManagedCamelContext(CamelContext context) {
040 this.context = context;
041 }
042
043 public void init(ManagementStrategy strategy) {
044 // do nothing
045 }
046
047 public CamelContext getContext() {
048 return context;
049 }
050
051 @ManagedAttribute(description = "Camel id")
052 public String getCamelId() {
053 return context.getName();
054 }
055
056 @ManagedAttribute(description = "Camel Version")
057 public String getCamelVersion() {
058 return context.getVersion();
059 }
060
061 @ManagedAttribute(description = "Camel State")
062 public String getState() {
063 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
064 ServiceStatus status = ((ServiceSupport) context).getStatus();
065 // if no status exists then its stopped
066 if (status == null) {
067 status = ServiceStatus.Stopped;
068 }
069 return status.name();
070 }
071
072 @ManagedAttribute(description = "Camel Properties")
073 public Map<String, String> getProperties() {
074 if (context.getProperties().isEmpty()) {
075 return null;
076 }
077 return context.getProperties();
078 }
079
080 @ManagedAttribute(description = "Tracing")
081 public Boolean getTracing() {
082 return context.isTracing();
083 }
084
085 @ManagedAttribute(description = "Tracing")
086 public void setTracing(Boolean tracing) {
087 context.setTracing(tracing);
088 }
089
090 @ManagedAttribute(description = "Current number of inflight Exchanges")
091 public Integer getInflightExchanges() {
092 return context.getInflightRepository().size();
093 }
094
095 @ManagedAttribute(description = "Shutdown timeout")
096 public void setTimeout(long timeout) {
097 context.getShutdownStrategy().setTimeout(timeout);
098 }
099
100 @ManagedAttribute(description = "Shutdown timeout")
101 public long getTimeout() {
102 return context.getShutdownStrategy().getTimeout();
103 }
104
105 @ManagedAttribute(description = "Shutdown timeout time unit")
106 public void setTimeUnit(TimeUnit timeUnit) {
107 context.getShutdownStrategy().setTimeUnit(timeUnit);
108 }
109
110 @ManagedAttribute(description = "Shutdown timeout time unit")
111 public TimeUnit getTimeUnit() {
112 return context.getShutdownStrategy().getTimeUnit();
113 }
114
115 @ManagedAttribute(description = "Whether to force shutdown now when a timeout occurred")
116 public void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout) {
117 context.getShutdownStrategy().setShutdownNowOnTimeout(shutdownNowOnTimeout);
118 }
119
120 @ManagedAttribute(description = "Whether to force shutdown now when a timeout occurred")
121 public boolean isShutdownNowOnTimeout() {
122 return context.getShutdownStrategy().isShutdownNowOnTimeout();
123 }
124
125 @ManagedOperation(description = "Start Camel")
126 public void start() throws Exception {
127 context.start();
128 }
129
130 @ManagedOperation(description = "Stop Camel")
131 public void stop() throws Exception {
132 context.stop();
133 }
134
135 @ManagedOperation(description = "Send body (in only)")
136 public void sendBody(String endpointUri, String body) throws Exception {
137 ProducerTemplate template = context.createProducerTemplate();
138 template.sendBody(endpointUri, body);
139 template.stop();
140 }
141
142 @ManagedOperation(description = "Request body (in out)")
143 public Object requestBody(String endpointUri, String body) throws Exception {
144 ProducerTemplate template = context.createProducerTemplate();
145 Object answer = template.requestBody(endpointUri, body);
146 template.stop();
147 return answer;
148 }
149
150 }