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    @XNode("clearCompletedAfterSeconds")
079    public Integer clearCompletedAfterSeconds;
080
081    public int getClearCompletedAfterSeconds() {
082        return clearCompletedAfterSeconds == null ? DEFAULT_CLEAR_COMPLETED_AFTER_SECONDS : clearCompletedAfterSeconds.intValue();
083    }
084
085    @XNodeList(value = "category", type = HashSet.class, componentType = String.class)
086    public Set<String> categories = Collections.emptySet();
087
088    /**
089     * When specified, make the blocking queue bounded, so submission will block until space become available. This
090     * option can not be used with a priority queue.
091     *
092     * @since 5.7
093     */
094    @XNode("capacity")
095    public Integer capacity;
096
097    public int getCapacity() {
098        return capacity == null ? DEFAULT_CAPACITY : capacity.intValue();
099    }
100
101    @Override
102    public WorkQueueDescriptor clone() {
103        WorkQueueDescriptor o = new WorkQueueDescriptor();
104        o.id = id;
105        o.queuing = queuing;
106        o.processing = processing;
107        o.name = name;
108        o.maxThreads = maxThreads;
109        o.clearCompletedAfterSeconds = clearCompletedAfterSeconds;
110        o.capacity = capacity;
111        o.categories = new HashSet<String>(categories);
112        return o;
113    }
114
115    public void merge(WorkQueueDescriptor other) {
116        if (other.queuing != null) {
117            queuing = other.queuing;
118        }
119        if (other.processing != null) {
120            processing = other.processing;
121        }
122        if (other.name != null) {
123            name = other.name;
124        }
125        if (other.maxThreads != null) {
126            maxThreads = other.maxThreads;
127        }
128        if (other.clearCompletedAfterSeconds != null) {
129            clearCompletedAfterSeconds = other.clearCompletedAfterSeconds;
130        }
131        if (other.capacity != null) {
132            capacity = other.capacity;
133        }
134        categories.addAll(other.categories);
135    }
136
137    @Override
138    public String toString() {
139        StringBuilder buf = new StringBuilder(getClass().getSimpleName());
140        buf.append("[id=");
141        buf.append(id);
142        buf.append(" categories=");
143        buf.append(categories);
144        if (queuing != null) {
145            buf.append(" queuing=");
146            buf.append(queuing);
147        }
148        if (processing != null) {
149            buf.append(" processing=");
150            buf.append(processing);
151        }
152        if (maxThreads != null) {
153            buf.append(" maxThreads=");
154            buf.append(maxThreads);
155        }
156        if (capacity != null) {
157            buf.append(" capacity=");
158            buf.append(capacity);
159        }
160        if (clearCompletedAfterSeconds != null) {
161            buf.append(" clearCompletedAfterSeconds=");
162            buf.append(clearCompletedAfterSeconds);
163        }
164        buf.append("]");
165        return buf.toString();
166    }
167
168    public String toEffectiveString() {
169        StringBuilder buf = new StringBuilder(getClass().getSimpleName());
170        buf.append("(id=");
171        buf.append(id);
172        buf.append(" categories=");
173        buf.append(categories);
174        buf.append(" queuing=");
175        buf.append(isQueuingEnabled());
176        buf.append(" processing=");
177        buf.append(isProcessingEnabled());
178        buf.append(" maxThreads=");
179        buf.append(getMaxThreads());
180        buf.append(" capacity=");
181        buf.append(getCapacity());
182        buf.append(" clearCompletedAfterSeconds=");
183        buf.append(getClearCompletedAfterSeconds());
184        buf.append(")");
185        return buf.toString();
186    }
187
188}