<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~ Copyright The WildFly Authors
  ~ SPDX-License-Identifier: Apache-2.0
  -->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:jboss:domain:threads:1.0"
            xmlns="urn:jboss:domain:threads:1.0"
            elementFormDefault="qualified"
            attributeFormDefault="unqualified"
            version="1.0">

    <!-- The threads subsystem root element -->
    <xs:element name="subsystem" type="subsystem"/>

    <xs:complexType name="subsystem">
        <xs:annotation>
            <xs:documentation>
                <![CDATA[
                    The threading subsystem, used to declare manageable thread pools and resources.
                ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="thread-factory" type="thread-factory"/>
            <xs:element name="unbounded-queue-thread-pool" type="unbounded-queue-thread-pool"/>
            <xs:element name="bounded-queue-thread-pool" type="bounded-queue-thread-pool"/>
            <xs:element name="queueless-thread-pool" type="queueless-thread-pool"/>
            <xs:element name="scheduled-thread-pool" type="scheduled-thread-pool"/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="thread-factory">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A thread factory (implementing java.util.concurrent.ThreadFactory).  The "name" attribute is
                the bean name of the created thread factory.  The optional "priority" attribute may be used to specify
                the thread priority of created threads.  The optional "group-name" attribute specifies the name of a the
                thread group to create for this thread factory.

                The "thread-name-pattern" is the template used to create names for threads.  The following patterns
                may be used:

                 %% - emit a percent sign
                 %t - emit the per-factory thread sequence number
                 %g - emit the global thread sequence number
                 %f - emit the factory sequence number
                 %i - emit the thread ID
                 %G - emit the thread group name
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="properties" type="properties" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="group-name" type="xs:string" use="optional"/>
        <xs:attribute name="thread-name-pattern" type="xs:string" use="optional"/>
        <xs:attribute name="priority" type="priority" use="optional"/>
    </xs:complexType>

    <xs:complexType name="unbounded-queue-thread-pool">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A thread pool executor with an unbounded queue.  Such a thread pool has a core size and a queue with no
                upper bound.  When a task is submitted, if the number of running threads is less than the core size,
                a new thread is created.  Otherwise, the task is placed in queue.  If too many tasks are allowed to be
                submitted to this type of executor, an out of memory condition may occur.

                The "name" attribute is the bean name of the created executor.

                The nested "max-threads" element must be used to specify the thread pool size.  The nested
                "keepalive-time" element may used to specify the amount of time that pool threads should
                be kept running when idle; if not specified, threads will run until the executor is shut down.
                The "thread-factory" element specifies the bean name of a specific thread factory to use to create worker
                threads.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="max-threads" type="scaled-count"/>
            <xs:element name="keepalive-time" type="time" minOccurs="0"/>
            <xs:element name="thread-factory" type="ref" minOccurs="0"/>
            <xs:element name="properties" type="properties" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="name" use="required" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="bounded-queue-thread-pool">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A thread pool executor with a bounded queue.  Such a thread pool has a core and maximum size and a
                specified queue length.  When a task is submitted, if the number of running threads is less than the
                core size, a new thread is created.  Otherwise, if there is room in the queue, the task is enqueued.
                Otherwise, if the number of running threads is less than the maximum size, a new thread is created.
                Otherwise, if blocking is enabled, the caller blocks until room becomes available in the queue.
                Otherwise, the task is handed off to the designated handoff executor, if one is specified.  Otherwise,
                the task is rejected.

                The "name" attribute is the bean name of the created executor.  The "allow-core-timeout" attribute
                specifies whether core threads may time out; if false, only threads above the core size will time out.
                The "blocking" attribute specifies whether the submitter thread will block if no space is available in
                this executor.

                The optional "core-threads" element may be used to specify the core thread pool size which is smaller
                than the maximum pool size.  The required "max-threads" element specifies the maximum thread pool size.
                The required "queue-length" element specifies the queue length.  The nested "keepalive-time" element may
                used to specify the amount of time that threads beyond the core pool size should be kept running when idle.
                The optional "thread-factory" element specifies the bean name of a specific thread factory to use to
                create worker threads.  The optional "handoff-executor" element specifies an executor to delegate tasks
                to in the event that a task cannot be accepted.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="core-threads" type="scaled-count" minOccurs="0"/>
            <xs:element name="queue-length" type="scaled-count"/>
            <xs:element name="max-threads" type="scaled-count"/>
            <xs:element name="keepalive-time" type="time" minOccurs="0"/>
            <xs:element name="thread-factory" type="ref" minOccurs="0"/>
            <xs:element name="handoff-executor" type="ref" minOccurs="0"/>
            <xs:element name="properties" type="properties" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="name" use="required" type="xs:string"/>
        <xs:attribute name="allow-core-timeout" use="optional" type="xs:boolean" default="false"/>
        <xs:attribute name="blocking" use="optional" type="xs:boolean" default="false"/>
    </xs:complexType>

    <xs:complexType name="queueless-thread-pool">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A thread pool executor with no queue.  When a task is submitted, if the number of running threads is
                less than the maximum size, a new thread is created.  Otherwise, if blocking is enabled, the caller
                blocks until another thread completes its task and accepts the new one.  Otherwise, the task is handed
                off to the designated handoff executor, if one is specified.  Otherwise, the task is rejected.

                The "name" attribute is the bean name of the created executor.  The "blocking" attribute specifies
                whether the submitter thread will block if no space is available in this executor.

                The "max-threads" element specifies the number of threads to use for this executor before
                tasks cannot be accepted anymore.  The optional "keepalive-time" is used to specify the amount of time
                that threads should be kept running when idle; by default threads run indefinitely.  The optional
                "thread-factory" element specifies the bean name of a specific thread factory to use to create worker
                threads.  The optional "handoff-executor" element specifies an executor to delegate tasks to in the
                event that a task cannot be accepted.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="max-threads" type="scaled-count"/>
            <xs:element name="keepalive-time" type="time" minOccurs="0"/>
            <xs:element name="thread-factory" type="ref" minOccurs="0"/>
            <xs:element name="handoff-executor" type="ref" minOccurs="0"/>
            <xs:element name="properties" type="properties" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="name" use="required" type="xs:string"/>
        <xs:attribute name="blocking" use="optional" type="xs:boolean" default="false"/>
    </xs:complexType>

    <xs:complexType name="scheduled-thread-pool">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A scheduled thread pool executor.  The "name" attribute is the bean name of the created executor.  The
                "thread-factory" attribute specifies the bean name of the thread factory to use to create worker
                threads.  The nested "max-threads" element may be used to specify the thread pool size.  The nested
                "keepalive-time" element is used to specify the amount of time that threads should be kept running when idle.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="max-threads" type="scaled-count"/>
            <xs:element name="keepalive-time" type="time" minOccurs="0"/>
            <xs:element name="thread-factory" type="ref" minOccurs="0"/>
            <xs:element name="properties" type="properties" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="name" use="required" type="xs:string"/>
    </xs:complexType>

    <xs:simpleType name="priority">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A priority which can range from 1 to 10 (inclusive).  See http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#setPriority(int) for more information.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="1"/>
            <xs:maxInclusive value="10"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="properties">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A set of free-form properties.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="property" type="property"/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="property">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A free-form property.  The name is required; the value is optional.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="value" type="xs:string" use="optional"/>
    </xs:complexType>

    <xs:complexType name="ref">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A reference to another named service.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="scaled-count">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                A scaled size designation.  The "count" attribute specifies a flat quantity.  The
                "per-cpu" attribute specifies a quantity per available CPU, as determined by
                java.lang.Runtime#availableProcessors().  The numbers are added together and rounded off
                to an integer value.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="count" type="xs:float" use="optional"/>
        <xs:attribute name="per-cpu" type="xs:float" use="optional"/>
    </xs:complexType>

    <xs:complexType name="time">
        <xs:annotation>
            <xs:documentation>
                An amount of time.  Comprised of a time value and a unit value.
            </xs:documentation>
        </xs:annotation>
        <xs:attribute name="time" type="xs:long" use="required"/>
        <xs:attribute name="unit" type="time-unit-name" use="required"/>
    </xs:complexType>

    <xs:simpleType name="time-unit-name">
        <xs:annotation>
            <xs:documentation>
                The name of a unit of time.
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:token">
            <xs:enumeration value="seconds"/>
            <xs:enumeration value="minutes"/>
            <xs:enumeration value="milliseconds"/>
            <xs:enumeration value="nanoseconds"/>
            <xs:enumeration value="hours"/>
            <xs:enumeration value="days"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>
