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 javax.management.openmbean.CompositeData;
020import javax.management.openmbean.CompositeDataSupport;
021import javax.management.openmbean.CompositeType;
022import javax.management.openmbean.TabularData;
023import javax.management.openmbean.TabularDataSupport;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.RuntimeCamelException;
027import org.apache.camel.api.management.ManagedResource;
028import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
029import org.apache.camel.api.management.mbean.ManagedTaskManagerRegistryMBean;
030import org.apache.camel.support.task.BackgroundTask;
031import org.apache.camel.support.task.Task;
032import org.apache.camel.support.task.TaskManagerRegistry;
033
034@ManagedResource(description = "Managed TaskManagerRegistry")
035public class ManagedTaskManagerRegistry extends ManagedService implements ManagedTaskManagerRegistryMBean {
036
037    private final TaskManagerRegistry registry;
038
039    public ManagedTaskManagerRegistry(CamelContext context, TaskManagerRegistry registry) {
040        super(context, registry);
041        this.registry = registry;
042    }
043
044    @Override
045    public Integer getSize() {
046        return registry.getSize();
047    }
048
049    @Override
050    public TabularData listTasks() {
051        try {
052            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listInternalTaskTabularType());
053            for (Task task : registry.getTasks()) {
054                String name = task.getName();
055                String kind = task instanceof BackgroundTask ? "background" : "foreground";
056                String status = task.getStatus().name();
057                long attempts = task.iteration();
058                long delay = task.getCurrentDelay();
059                long elapsed = task.getCurrentElapsedTime();
060                long firstTime = task.getFirstAttemptTime();
061                long lastTime = task.getLastAttemptTime();
062                long nextTime = task.getNextAttemptTime();
063                String failure = task.getException() != null ? task.getException().getMessage() : null;
064                CompositeType ct = CamelOpenMBeanTypes.listInternalTaskCompositeType();
065                CompositeData data = new CompositeDataSupport(
066                        ct,
067                        new String[] {
068                                "name", "kind", "status", "attempts", "delay", "elapsed", "firstTime", "lastTime", "nextTime",
069                                "failure" },
070                        new Object[] { name, kind, status, attempts, delay, elapsed, firstTime, lastTime, nextTime, failure });
071                answer.put(data);
072            }
073            return answer;
074        } catch (Exception e) {
075            throw RuntimeCamelException.wrapRuntimeCamelException(e);
076        }
077    }
078
079}