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;
028
029/**
030 * Descriptor for a {@link WorkManager} queue configuration.
031 *
032 * @since 5.6
033 */
034@XObject("queue")
035public class WorkQueueDescriptor {
036
037    public static final String ALL_QUEUES = "*";
038
039    public static final int DEFAULT_MAX_THREADS = 4;
040
041    public static final int DEFAULT_CLEAR_COMPLETED_AFTER_SECONDS = 600;
042
043    public static final int DEFAULT_CAPACITY = -1;
044
045    @XNode("@id")
046    public String id;
047
048    @XNode("@queueing")
049    public Boolean queuing;
050
051    /**
052     * Whether queuing of work instances to this queue is enabled for this Nuxeo instance.
053     */
054    public boolean isQueuingEnabled() {
055        return !Boolean.FALSE.equals(queuing);
056    }
057
058    @XNode("@processing")
059    public Boolean processing;
060
061    /**
062     * Whether processing of work instances from this queue is enabled for this Nuxeo instance.
063     */
064    public boolean isProcessingEnabled() {
065        return !Boolean.FALSE.equals(processing);
066    }
067
068    @XNode("name")
069    public String name;
070
071    @XNode("maxThreads")
072    public Integer maxThreads;
073
074    public int getMaxThreads() {
075        return maxThreads == null ? DEFAULT_MAX_THREADS : maxThreads.intValue();
076    }
077
078    @XNodeList(value = "category", type = HashSet.class, componentType = String.class)
079    public Set<String> categories = Collections.emptySet();
080
081    /**
082     * When specified, make the blocking queue bounded, so submission will block until space become available. This
083     * option can not be used with a priority queue.
084     *
085     * @since 5.7
086     */
087    @XNode("capacity")
088    public Integer capacity;
089
090    public int getCapacity() {
091        return capacity == null ? DEFAULT_CAPACITY : capacity.intValue();
092    }
093
094    @Override
095    public WorkQueueDescriptor clone() {
096        WorkQueueDescriptor o = new WorkQueueDescriptor();
097        o.id = id;
098        o.queuing = queuing;
099        o.processing = processing;
100        o.name = name;
101        o.maxThreads = maxThreads;
102        o.capacity = capacity;
103        o.categories = new HashSet<String>(categories);
104        return o;
105    }
106
107    public void merge(WorkQueueDescriptor other) {
108        if (other.queuing != null) {
109            queuing = other.queuing;
110        }
111        if (other.processing != null) {
112            processing = other.processing;
113        }
114        if (other.name != null) {
115            name = other.name;
116        }
117        if (other.maxThreads != null) {
118            maxThreads = other.maxThreads;
119        }
120        if (other.capacity != null) {
121            capacity = other.capacity;
122        }
123        categories.addAll(other.categories);
124    }
125
126    @Override
127    public String toString() {
128        StringBuilder buf = new StringBuilder(getClass().getSimpleName());
129        buf.append("[id=");
130        buf.append(id);
131        buf.append(" categories=");
132        buf.append(categories);
133        if (queuing != null) {
134            buf.append(" queuing=");
135            buf.append(queuing);
136        }
137        if (processing != null) {
138            buf.append(" processing=");
139            buf.append(processing);
140        }
141        if (maxThreads != null) {
142            buf.append(" maxThreads=");
143            buf.append(maxThreads);
144        }
145        if (capacity != null) {
146            buf.append(" capacity=");
147            buf.append(capacity);
148        }
149        buf.append("]");
150        return buf.toString();
151    }
152
153    public String toEffectiveString() {
154        StringBuilder buf = new StringBuilder(getClass().getSimpleName());
155        buf.append("(id=");
156        buf.append(id);
157        buf.append(" categories=");
158        buf.append(categories);
159        buf.append(" queuing=");
160        buf.append(isQueuingEnabled());
161        buf.append(" processing=");
162        buf.append(isProcessingEnabled());
163        buf.append(" maxThreads=");
164        buf.append(getMaxThreads());
165        buf.append(" capacity=");
166        buf.append(getCapacity());
167        buf.append(" clearCompletedAfterSeconds=");
168        buf.append(")");
169        return buf.toString();
170    }
171
172}