Concurrency Utilities support in WebLogic Server 12.2.1, Part Three: ManagedThreadFactory

Overview

ManagedThreadFactory is for creating threads managed by WebLogic Server. It extends from java.util.concurrent.ThreadFactory without new methods, and provides the method newThread from ThreadFactory. It can be used with Java SE concurrency utilities APIs where ThreadFactory is needed. e.g. in java.util.concurrent.Executors.

Weblogic Server provides a preconfigured, default ManagedThreadFactory for each application, and applications can easily use it in web or EJB components without any configuration. Let's begin with a simple example that uses default ManagedThreadFactory in a servlet.

Example-1: Use Default ManagedThreadFactory to Create a Thread in a Servlet

Step1: Write a Runnable,logging data until the thread is interrupted.

public class LoggerTask implements Runnable {

@Override

public void run() {

while (!Thread.interrupted()) {

// collect data and write them to database or file system

}

}

}

Step2: SomeServlet.java injects the default ManagedThreadFactory and use it to create the thread.

Runtime Behavior

Application Scoped Instance

ManagedThreadFactories are application scoped. Each application has its own ManagedThreadFactory instances, and the lifecycle of the ManagedThreadFactory instances are bound to the application. Threads created by ManagedThreadFactory are also application scoped, so that when the application is shut down, related threads will be interrupted.

Each application has its own default ManagedThreadFactory instance. Besides, Applications or system administrators can define customized ManagedThreadFactory. Please note that even ManagedThreadFactory templates(see in a later section) defined globally in the console are application scoped during runtime.

Context Propagation

ManagedThreadFactories will capture the application context at ManagedThreadFactory creation(NOT at newThread method invocation), then propagate the captured application context before task execution, so that the task can also run with the application context.

Four types of application context are propagated: JNDI, ClassLoader, Security and WorkArea. The propagated context types are the same for four types of the concurrent managed objects.

Limit of Running Threads

When newThread method is invoked, WebLogic Server creates a new thread. Because an excessive number of running threads can have a negative affect on server performance and stability, WebLogic Server provides configurations(Max Concurrent New Threads) to limit the number of running threads in a ManagedThreadFactory instance, in the global (domain-level) runtime on a server or in the server. By default, the limits are: 10 for a ManagedThreadFactory instance, 50 for the global (domain-level) runtime on a server and 100 for a Server. When either of the limits is exceeded, calls to newThread() method of ManagedThreadFactory return null.

Please note the difference between the global (domain-level) runtime scope Max Concurrent New Threads and the server scope Max Concurrent New Threads. One of the key features in WLS 12.2.1 is the multi-tenancy support where a single Weblogic Server domain can contain multiple partitions. The global (domain-level) runtime Max Concurrent New Threads is the maximum number of threads created by all of the ManagedThreadFactories on the server for global (domain-level) runtime, this excludes threads created within the scope of partitions running on the server, while the server scope Max Concurrent New Threads is the maximum number of threads created by all of the ManagedThreadFactories on the server, including threads created within the scope of partitions. For partition scope Max Concurrent New Threads, please read Part Five - Multi-tenancy Support.

ManagedThreadFactory returns a new thread only when neither of the 3 limits is exceeded. For instance, there is an application deployed to global (domain-level) runtime on a server, when servlets or EJBs invoke the newThread method of the default ManagedThreadFactory, they will get null if there are 10 in progress threads which are created by this ManagedThreadFactory, or there are 50 in progress threads which are created by the ManagedThreadFactories in scope of global (domain-level) runtime on the server, or there are 100 in progress threads which are created by the ManagedThreadFactories in the server.

There are examples on how to specify the Max Concurrent New Threads in a later section.

Configuration

As mentioned earlier, each application has its own default ManagedThreadFactory. The default ManagedThreadFactory has a default max concurrent new threads(10), and has a default thread priority(Thread.NORM_PRIORITY). There is also a default max concurrent new threads(100) for the whole server. If the default configuration is not good enough you will need to read further for configurations. For instance, when you need to create threads with higher priority, you will need to configure a ManagedThreadFactory; and if there would be more than 100 concurrent running threads in the server, you will need to change the server scope Max Concurrent New Threads.

Configure ManagedThreadFactories

Name, Max Concurrent New Threads, and Priority are configured inside a ManagedThreadFactory. Name is a string that identifies the ManagedThreadFactory,Max Concurrent New Threads is the limit of running threads created by this ManagedThreadFactory, and Priority is the priority of threads.

An application can configure a ManagedThreadFactory in DD(weblogic-application.xml/weblogic-ejb-jar.xml/weblogic.xml), and gets the ManagedThreadFactory instance using @Resource(mappedName=<Name of ManagedThreadFactory>), then uses it to create threads. Besides annotation, the application can also bind the ManagedThreadFactory instance to JNDI by specifying <resource-env-description> and <resource-env-ref> in DD, then look it up using JNDI Naming Context, you can read Configuring Concurrent Managed Objects in the product documentation for details.

Also, a WebLogic system administrator can configure pre-defined ManagedThreadFactory templates. When an application is deployed, WebLogic Server creates ManagedThreadFactories based on the configuration of ManagedThreadFactory templates, and the created ManagedThreadFactories are all in scope of this application.

If there is requirement on multiple applications instead of individual application, you can create ManagedThreadFactory templates globally that are available to all applications. For instance, when you need to create threads from all applications with lower priority, you will need to configure a ManagedThreadFactory template. As mentioned earlier, if there is a ManagedThreadFactory template, WebLogic Server creates a ManagedThreadFactory instance for each application based on the configuration of the template.

Step1: in WebLogic Administration Console, a ManagedThreadFactory template can be created by clicking on the “New” button from the “Summary of Concurrent Managed Object Templates” page. This brings up the "Create a New Managed Thread Factory Template" page where the name and other parameters of the new ManagedThreadFactory template can be specified. In this example, a ManagedThreadFactory template called "testMTF" is being created with priority 3.

Step2: Once a ManagedThreadFactory template is created, any application in the WebLogic Server can get its own ManagedThreadFactory instance to use.

Max Concurrent New Threadsof global (domain-level) runtime is the limit of threads created by ManagedThreadFactories in global (domain-level) runtime on that server, this excludes threads created within the scope of partitions running on that server.

In WebLogic Administration Console, Max Concurrent New Threads of global (domain-level) runtime can be edited from the “Settings for <domainName>” screen. In this example, global (domain-level) runtime Max Concurrent New Threadsof mydomain is set to 100.

Example-5: Configure Server Scope Max Concurrent New Threads

Max Concurrent New Threads of a server is the limit of running threads submitted to all ManagedThreadFactories in that server.

In WebLogic Administration Console, Max Concurrent New Threads of a server can be edited from the “Settings for <serverName>” screen. In this example, Max Concurrent New Threadsof myserver is set to 200.