001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.work.api;
020
021import java.util.Collections;
022import java.util.HashSet;
023import java.util.Set;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeList;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.runtime.model.Descriptor;
029
030/**
031 * Descriptor for a {@link WorkManager} queue configuration.
032 *
033 * @since 5.6
034 */
035@XObject("queue")
036public class WorkQueueDescriptor implements Descriptor {
037
038    public static final String ALL_QUEUES = "*";
039
040    public static final int DEFAULT_MAX_THREADS = 4;
041
042    public static final int DEFAULT_CLEAR_COMPLETED_AFTER_SECONDS = 600;
043
044    public static final int DEFAULT_CAPACITY = -1;
045
046    @XNode("@id")
047    public String id;
048
049    @XNode("@queueing")
050    public Boolean queuing;
051
052    @Override
053    public String getId() {
054        return id;
055    }
056
057    /**
058     * Whether queuing of work instances to this queue is enabled for this Nuxeo instance.
059     */
060    public boolean isQueuingEnabled() {
061        return !Boolean.FALSE.equals(queuing);
062    }
063
064    @XNode("@processing")
065    public Boolean processing;
066
067    /**
068     * Whether processing of work instances from this queue is enabled for this Nuxeo instance.
069     */
070    public boolean isProcessingEnabled() {
071        return !Boolean.FALSE.equals(processing);
072    }
073
074    @XNode("name")
075    public String name;
076
077    @XNode("maxThreads")
078    public Integer maxThreads;
079
080    public int getMaxThreads() {
081        return maxThreads == null ? DEFAULT_MAX_THREADS : maxThreads.intValue();
082    }
083
084    @XNodeList(value = "category", type = HashSet.class, componentType = String.class)
085    public Set<String> categories = Collections.emptySet();
086
087    /**
088     * When specified, make the blocking queue bounded, so submission will block until space become available. This
089     * option can not be used with a priority queue.
090     *
091     * @since 5.7
092     */
093    @XNode("capacity")
094    public Integer capacity;
095
096    public int getCapacity() {
097        return capacity == null ? DEFAULT_CAPACITY : capacity.intValue();
098    }
099
100    @Override
101    public Descriptor merge(Descriptor o) {
102        WorkQueueDescriptor other = (WorkQueueDescriptor) o;
103        WorkQueueDescriptor merged = new WorkQueueDescriptor();
104        merged.id = id;
105        merged.name = other.name != null ? other.name : name;
106        merged.queuing = other.queuing != null ? other.queuing : queuing;
107        merged.capacity = other.capacity != null ? other.capacity : capacity;
108        merged.processing = other.processing != null ? other.processing : processing;
109        merged.maxThreads = other.maxThreads != null ? other.maxThreads : maxThreads;
110        merged.categories = new HashSet<>(categories);
111        merged.categories.addAll(other.categories);
112        return merged;
113    }
114
115    @Override
116    public String toString() {
117        StringBuilder buf = new StringBuilder(getClass().getSimpleName());
118        buf.append("(id=");
119        buf.append(id);
120        buf.append(" categories=");
121        buf.append(categories);
122        buf.append(" queuing=");
123        buf.append(isQueuingEnabled());
124        buf.append(" processing=");
125        buf.append(isProcessingEnabled());
126        buf.append(" maxThreads=");
127        buf.append(getMaxThreads());
128        buf.append(" capacity=");
129        buf.append(getCapacity());
130        buf.append(")");
131        return buf.toString();
132    }
133}