001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 */
017package org.nuxeo.ecm.core.work.api;
018
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.Set;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XNodeList;
025import org.nuxeo.common.xmap.annotation.XObject;
026
027/**
028 * Descriptor for a {@link WorkManager} queue configuration.
029 *
030 * @since 5.6
031 */
032@XObject("queue")
033public class WorkQueueDescriptor {
034
035    public static final String ALL_QUEUES = "*";
036
037    public static final int DEFAULT_MAX_THREADS = 4;
038
039    public static final int DEFAULT_CLEAR_COMPLETED_AFTER_SECONDS = 3600;
040
041    public static final int DEFAULT_CAPACITY = -1;
042
043    @XNode("@id")
044    public String id;
045
046    @XNode("@queueing")
047    public Boolean queuing;
048
049    /**
050     * Whether queuing of work instances to this queue is enabled for this Nuxeo instance.
051     */
052    public boolean isQueuingEnabled() {
053        return !Boolean.FALSE.equals(queuing);
054    }
055
056    @XNode("@processing")
057    public Boolean processing;
058
059    /**
060     * Whether processing of work instances from this queue is enabled for this Nuxeo instance.
061     */
062    public boolean isProcessingEnabled() {
063        return !Boolean.FALSE.equals(processing);
064    }
065
066    @XNode("name")
067    public String name;
068
069    @XNode("maxThreads")
070    public Integer maxThreads;
071
072    public int getMaxThreads() {
073        return maxThreads == null ? DEFAULT_MAX_THREADS : maxThreads.intValue();
074    }
075
076    @XNode("clearCompletedAfterSeconds")
077    public Integer clearCompletedAfterSeconds;
078
079    public int getClearCompletedAfterSeconds() {
080        return clearCompletedAfterSeconds == null ? DEFAULT_CLEAR_COMPLETED_AFTER_SECONDS : clearCompletedAfterSeconds.intValue();
081    }
082
083    @XNodeList(value = "category", type = HashSet.class, componentType = String.class)
084    public Set<String> categories = Collections.emptySet();
085
086    /**
087     * When specified, make the blocking queue bounded, so submission will block until space become available. This
088     * option can not be used with a priority queue.
089     *
090     * @since 5.7
091     */
092    @XNode("capacity")
093    public Integer capacity;
094
095    public int getCapacity() {
096        return capacity == null ? DEFAULT_CAPACITY : capacity.intValue();
097    }
098
099    @Override
100    public WorkQueueDescriptor clone() {
101        WorkQueueDescriptor o = new WorkQueueDescriptor();
102        o.id = id;
103        o.queuing = queuing;
104        o.processing = processing;
105        o.name = name;
106        o.maxThreads = maxThreads;
107        o.clearCompletedAfterSeconds = clearCompletedAfterSeconds;
108        o.capacity = capacity;
109        o.categories = new HashSet<String>(categories);
110        return o;
111    }
112
113    public void merge(WorkQueueDescriptor other) {
114        if (other.queuing != null) {
115            queuing = other.queuing;
116        }
117        if (other.processing != null) {
118            processing = other.processing;
119        }
120        if (other.name != null) {
121            name = other.name;
122        }
123        if (other.maxThreads != null) {
124            maxThreads = other.maxThreads;
125        }
126        if (other.clearCompletedAfterSeconds != null) {
127            clearCompletedAfterSeconds = other.clearCompletedAfterSeconds;
128        }
129        if (other.capacity != null) {
130            capacity = other.capacity;
131        }
132        categories.addAll(other.categories);
133    }
134
135    @Override
136    public String toString() {
137        StringBuilder buf = new StringBuilder(getClass().getSimpleName());
138        buf.append("[id=");
139        buf.append(id);
140        buf.append(" categories=");
141        buf.append(categories);
142        if (queuing != null) {
143            buf.append(" queuing=");
144            buf.append(queuing);
145        }
146        if (processing != null) {
147            buf.append(" processing=");
148            buf.append(processing);
149        }
150        if (maxThreads != null) {
151            buf.append(" maxThreads=");
152            buf.append(maxThreads);
153        }
154        if (capacity != null) {
155            buf.append(" capacity=");
156            buf.append(capacity);
157        }
158        if (clearCompletedAfterSeconds != null) {
159            buf.append(" clearCompletedAfterSeconds=");
160            buf.append(clearCompletedAfterSeconds);
161        }
162        buf.append("]");
163        return buf.toString();
164    }
165
166    public String toEffectiveString() {
167        StringBuilder buf = new StringBuilder(getClass().getSimpleName());
168        buf.append("(id=");
169        buf.append(id);
170        buf.append(" categories=");
171        buf.append(categories);
172        buf.append(" queuing=");
173        buf.append(isQueuingEnabled());
174        buf.append(" processing=");
175        buf.append(isProcessingEnabled());
176        buf.append(" maxThreads=");
177        buf.append(getMaxThreads());
178        buf.append(" capacity=");
179        buf.append(getCapacity());
180        buf.append(" clearCompletedAfterSeconds=");
181        buf.append(getClearCompletedAfterSeconds());
182        buf.append(")");
183        return buf.toString();
184    }
185
186}