3 Replies Latest reply: Mar 2, 2009 5:02 AM by 685516 RSS

    WorkManager configuration and usage docs


      Is there some documentation about WorkManager configuration and usage? I've just found short description of the API itself on the "Provide a Data Grid" wiki page which says nothing about Coherence implementation of the API. And there's one post in the forum which shows how to start WorkManager just by instantiating new object. Is there some more detailed examples or docs?


      Edited by: user10898627 on Feb 24, 2009 5:40 AM
        • 1. Re: WorkManager configuration and usage docs
          Hi Andrew,

          I don't know if this is the forum post that you were referring to but this shows a simple example using a WorkManager ...


          • 2. Re: WorkManager configuration and usage docs
            Hi Andrew,

            Unfortunately, the available WorkManager documentation barely scratches the surface of what you need to know to use Coherence WorkManager.

            The basics are as documented: you create an instance of WorkManager with a given name and the number of worker threads and schedule serializable Work to be executed. However, there are a number of caveats:

            1. You can only create an instance of a WorkManager with a given name once per node. If you try to create a manager with the same name again you will get an exception. That means that you either need to hold on to a reference to it, or obtain it from the underlying InvocationService (Coherence WorkManager is built on top of InvocationService). Here is the utility class I use that will allow you to obtain an existing WorkManager or create a new one for a given name:
            public class WorkManagerFactory {

                public static WorkManager getServerInstance(String name) {
                    return getInstance(name, -1);

                public static WorkManager getClientInstance(String name) {
                    return getInstance(name, 0);

                public static WorkManager getInstance(String name, int threadCount) {
                    InvocationService service = getInvocationService(name);
                    if (service.isRunning()) {
                        return (WorkManager) service.getUserContext();
                    return new WorkManager(name, threadCount);

                private static InvocationService getInvocationService(String name) {
                    Cluster cluster = CacheFactory.ensureCluster();

                    return (InvocationService)
                            cluster.ensureService(name, InvocationService.TYPE_DEFAULT);
            The getInstance() method is where the logic is: it checks if the invocation service with a given name is already running, and retrieves the WorkManager from it by calling getUserContext() if it is. Otherwise, it creates a new WorkManager instance. There are also two convenience methods that allow you to get either a client-only WorkManager instance (thread count 0), or a WorkManager server that uses the default number of threads for the invocation service. In order to use the latter, you will have to set the system property tangosol.coherence.invocation.threads to a positive integer when starting your cluster nodes.

            2. If you want to use WorkManager, you cannot enable POF globally using tangosol.pof.enabled system property. Instead, you will have to specify serializer explicitly for all cache schemes that should use POF.

            This is because WorkManager-related classes don't support POF as a serialization format. This will hopefully be fixed in the future release of Coherence, but currently that's the way it is.

            3. You need to decide how to start WorkManager instances on cluster nodes that should execute the tasks scheduled by the manager. There are several options:

            a) You can start standalone WorkManager servers by executing WorkManager from the command line (it has the main method) and passing name and thread count as command line arguments.
            b) You can start it on the application server by calling constructor directly or using WorkManagerFactory above.
            c) You can start it on the cache server by creating it in a class that is instantiated automatically by Coherence, such as backing map listener, for example. If you use SpringAwareCacheFactory, you can also start it by simply registering singleton bean definition for it in the Spring application context.

            Other than that, you can use it just like any other WorkManager implementation. You schedule the Work to execute and if you care about the results wait until it is completed. Keep in mind that the WorkItem returned by the call to schedule method is always RemoteWorkItem, so you will need to call getResult method on it to retrieve the execution result.

            Of course, you should also be aware that Coherence WorkManager provides 'at most once' execution semantics, so there is no guarantee that the scheduled work will be executed. If you need better execution guarantee you should look into Coherence Incubator patterns instead.

            • 3. Re: WorkManager configuration and usage docs

              Thanks a lot for that super informative answer