实践JBoss7之http执行器(线程池)

介绍

JBoss作为Java web服务器,监听并接收http请求和应答响应,采用同步阻塞IO。启线程模型如下:

JBoss7线程模型

Acceptor和Poller线程栈参考如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"http--0.0.0.0-8080-Acceptor-0" daemon prio=10 tid=0x00002aaabc0d1800 nid=0x864 runnable [0x000000004577b000]
   java.lang.Thread.State: RUNNABLE
     at java.net.PlainSocketImpl.socketAccept(Native Method)
     at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
     - locked <0x00000007807f7428> (a java.net.SocksSocketImpl)
     at java.net.ServerSocket.implAccept(ServerSocket.java:462)
     at java.net.ServerSocket.accept(ServerSocket.java:430)
     at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61)
     at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:321)
     at java.lang.Thread.run(Thread.java:662)

"http--0.0.0.0-8080-Poller" daemon prio=10 tid=0x00002aaabc0dc800 nid=0x863 in Object.wait() [0x000000004567a000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
     at java.lang.Object.wait(Native Method)
     at org.apache.tomcat.util.net.JIoEndpoint$Poller.run(JIoEndpoint.java:742)
     - locked <0x00000007807f75d8> (a org.apache.tomcat.util.net.JIoEndpoint$Poller)
     at java.lang.Thread.run(Thread.java:662)

默认处理线程名为:http--0.0.0.0:8080-N,自定义执行器时处理线程名:${executor}-threads-N${executor}executorname属性值。

JBOSS7支持多种执行器(executor,线程池),参考如下图,详情可以参考${JBOSS_HOME}\docs\schema\jboss-as-threads_1_1.xsd

JBoss7执行器

各种执行器配置参数如下:

executor core-threads queue max-threads keepalive-time blocking thread-factory
unbounded-queue-thread-pool   无上限 指定 指定    
bounded-queue-thread-pool 指定 指定 指定 指定 false  
blocking-bounded-queue-thread-pool 指定 指定 指定 指定 true  
queueless-thread-pool     指定 指定 false  
blocking-queueless-thread-pool     指定 指定 true  
scheduled-thread-pool     指定 指定    

在JBOSS7配置文件${JBOSS_HOME}/standalone/configuration/standalone.xmlthreads节点下定义,默认没有定义(jboss7默认可能用的thread-factory(因为没有过期时间,从执行来看,线程不释放 )),如:

1
<subsystem xmlns="urn:jboss:domain:threads:1.1"/>

thread-factory

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:

1
2
3
4
5
6
             %% - 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 ## 定义执行器

bounded-queue-thread-pool

A thread pool executor with a bounded queue, where threads attempting to submit tasks will not block. 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, the task is handed off to the designated handoff executor, if one is specified. Otherwise, the task is discarded.

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 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 optional “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.

1
2
3
4
5
6
7
8
<subsystem xmlns="urn:jboss:domain:threads:1.1">
  <bounded-queue-thread-pool name="http-executor1" allow-core-timeout="false">
    <core-threads count="50" />
    <queue-length count="50" />
    <max-threads count="50" />
    <keepalive-time time="30" unit="seconds" />
  </bounded-queue-thread-pool>
</subsystem>

疑问:设置blocking=”true”会报解析配置文件失败,Failed to parse configuration,Unexpected attribute ‘blocking’ encountered

unbounded-queue-thread-pool

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 “max-threads” attribute 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.

1
2
3
4
<unbounded-queue-thread-pool name="http-executor1">
                <max-threads count="60"/>
                <keepalive-time time="30" unit="seconds"/>
            </unbounded-queue-thread-pool>

blocking-bounded-queue-thread-pool

A thread pool executor with a bounded queue, where threads attempting to submit tasks may block. 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, the caller blocks until room becomes available in the queue.

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 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 optional “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.

1
2
3
4
5
6
<blocking-bounded-queue-thread-pool name="http-executor3" allow-core-timeout="false">
                <core-threads count="30"/>
                <queue-length count="30"/>
                <max-threads count="60"/>
                <keepalive-time time="30" unit="seconds"/>
            </blocking-bounded-queue-thread-pool>

queueless-thread-pool

A thread pool executor with no queue, where threads attempting to submit tasks will not block. When a task is submitted, if the number of running threads is less than the maximum size, a new thread is created. Otherwise, the task is handed off to the designated handoff executor, if one is specified. Otherwise, the task is discarded.

The “name” attribute is the bean name of the created executor. The “max-threads” attribute 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.

1
2
3
4
<queueless-thread-pool name="http-executor4">
                <max-threads count="60"/>
                <keepalive-time time="30" unit="seconds"/>
            </queueless-thread-pool>

blocking-queueless-thread-pool

A thread pool executor with no queue, where threads attempting to submit tasks may block. When a task is submitted, if the number of running threads is less than the maximum size, a new thread is created. Otherwise, the caller blocks until another thread completes its task and accepts the new one.

The “name” attribute is the bean name of the created executor. The “max-threads” attribute 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.

1
2
3
4
<blocking-queueless-thread-pool name="http-executor5">
                <max-threads count="60"/>
                <keepalive-time time="30" unit="seconds"/>
            </blocking-queueless-thread-pool>

scheduled-thread-pool

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” attribute 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.

1
2
3
4
<scheduled-thread-pool name="http-executor6">
                <max-threads count="60"/>
                <keepalive-time time="30" unit="seconds"/>
            </scheduled-thread-pool>

配置使用执行器

web节点下指定已定义的执行器(默认没有指定),新增配置executor="http-executor1",参考如下:

1
2
3
4
5
6
7
<subsystem xmlns="urn:jboss:domain:web:1.1" default-virtual-server="default-host" native="false">
            <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http" executor="http-executor1"/>
            <virtual-server name="default-host" enable-welcome-root="true">
                <alias name="localhost"/>
                <alias name="example.com"/>
            </virtual-server>
        </subsystem>

###