synapse-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hira...@apache.org
Subject svn commit: r814533 - in /synapse/trunk/java/modules: core/src/main/java/org/apache/synapse/ core/src/main/java/org/apache/synapse/config/ core/src/main/java/org/apache/synapse/startup/quartz/ tasks/src/main/java/org/apache/synapse/task/
Date Mon, 14 Sep 2009 09:09:48 GMT
Author: hiranya
Date: Mon Sep 14 09:09:48 2009
New Revision: 814533

URL: http://svn.apache.org/viewvc?rev=814533&view=rev
Log:
Refactored task module and fixed a bug


Added:
    synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/SynapseTaskManager.java
Removed:
    synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/TaskHelper.java
Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/startup/quartz/SimpleQuartz.java

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java?rev=814533&r1=814532&r2=814533&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
Mon Sep 14 09:09:48 2009
@@ -132,7 +132,7 @@
         
         addDefaultBuildersAndFormatters(configurationContext.getAxisConfiguration());
         deployMediatorExtensions();
-        initTaskHelper(serverContextInformation);
+        initTaskManager(serverContextInformation);
         initDataSourceHelper(serverContextInformation);
         initSharedSecretCallbackHandlerCache(serverContextInformation);
         initialized = true;
@@ -227,7 +227,7 @@
         transportHelper.pauseSenders();
 
         // put tasks on hold
-        TaskHelper.getInstance().pauseAll();
+        SynapseTaskManager.getInstance().pauseAll();
         
         log.info("Entered maintenence mode");
     }
@@ -244,7 +244,7 @@
         transportHelper.resumeSenders();
 
         // resume tasks
-        TaskHelper.getInstance().resumeAll();
+        SynapseTaskManager.getInstance().resumeAll();
 
         log.info("Resumed normal operation from maintenence mode");
     }
@@ -255,8 +255,8 @@
     public void stop() {
         try {
             // stop tasks
-            if (TaskHelper.getInstance().isInitialized()) {
-                TaskHelper.getInstance().cleanup();
+            if (SynapseTaskManager.getInstance().isInitialized()) {
+                SynapseTaskManager.getInstance().cleanup();
             }
 
             // stop the listener manager
@@ -395,7 +395,7 @@
         }
         
         addServerIPAndHostEnrties();
-        
+
         return synapseConfiguration;
     }
 
@@ -448,7 +448,7 @@
                 log.info("Waiting for: " + pendingCallbacks + " callbacks/replies..");
             }
 
-            int runningTasks = TaskHelper.getInstance().getTaskScheduler().getRunningTaskCount();
+            int runningTasks = SynapseTaskManager.getInstance().getTaskScheduler().getRunningTaskCount();
             if (runningTasks > 0) {
                 log.info("Waiting for : " + runningTasks + " tasks to complete..");
             }
@@ -669,16 +669,16 @@
     }
 
     /**
-     *  Initialize TaskHelper - with any existing  TaskDescriptionRepository and TaskScheduler
+     *  Initialize Task Manager - with any existing  TaskDescriptionRepository and TaskScheduler
      *  or without those
      * @param serverContextInformation  ServerContextInformation instance
      */
-    private void initTaskHelper(ServerContextInformation serverContextInformation) {
+    private void initTaskManager(ServerContextInformation serverContextInformation) {
 
-        TaskHelper taskHelper = TaskHelper.getInstance();
-        if (taskHelper.isInitialized()) {
+        SynapseTaskManager synapseTaskManager = SynapseTaskManager.getInstance();
+        if (synapseTaskManager.isInitialized()) {
             if (log.isDebugEnabled()) {
-                log.debug("TaskHelper has been already initialized.");
+                log.debug("SynapseTaskManager has been already initialized.");
             }
             return;
         }
@@ -687,20 +687,15 @@
             serverContextInformation.getProperty(TaskConstants.TASK_DESCRIPTION_REPOSITORY);
         Object taskScheduler = serverContextInformation.getProperty(TaskConstants.TASK_SCHEDULER);
 
-        if (repo instanceof TaskDescriptionRepository && taskScheduler instanceof
TaskScheduler) {
-            taskHelper.init((TaskDescriptionRepository) repo, (TaskScheduler) taskScheduler);
-        } else {
+        if (repo != null && !(repo instanceof TaskDescriptionRepository)) {
+            handleFatal("Invalid property value specified for TaskDescriptionRepository");
+        }
 
-            if (repo == null && taskScheduler == null) {
-                taskHelper.init(
-                        TaskDescriptionRepositoryFactory.getTaskDescriptionRepository(
-                                TaskConstants.TASK_DESCRIPTION_REPOSITORY),
-                        TaskSchedulerFactory.getTaskScheduler(TaskConstants.TASK_SCHEDULER));
-            } else {
-                handleFatal("Invalid property values for " +
-                        "TaskDescriptionRepository or / and TaskScheduler ");
-            }
+        if (taskScheduler != null && !(taskScheduler instanceof TaskScheduler)) {
+            handleFatal("Invalid property value specified for TaskScheduler");
         }
+
+        synapseTaskManager.init((TaskDescriptionRepository) repo, (TaskScheduler) taskScheduler);
     }
 
     private void addDefaultBuildersAndFormatters(AxisConfiguration axisConf) {

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java?rev=814533&r1=814532&r2=814533&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
Mon Sep 14 09:09:48 2009
@@ -26,10 +26,7 @@
 import org.apache.synapse.*;
 import org.apache.synapse.eventing.SynapseEventSource;
 import org.apache.synapse.commons.datasource.DataSourceHelper;
-import org.apache.synapse.task.TaskDescriptionRepository;
-import org.apache.synapse.task.TaskDescriptionRepositoryFactory;
-import org.apache.synapse.task.TaskScheduler;
-import org.apache.synapse.task.TaskSchedulerFactory;
+import org.apache.synapse.task.*;
 import org.apache.synapse.config.xml.MediatorFactoryFinder;
 import org.apache.synapse.config.xml.endpoints.XMLToEndpointMapper;
 import org.apache.synapse.core.SynapseEnvironment;
@@ -102,10 +99,6 @@
     /** Hold reference to the Axis2 ConfigurationContext */
     private AxisConfiguration axisConfiguration = null;
     
-    private final TaskDescriptionRepository taskDescriptionRepository = 
-            TaskDescriptionRepositoryFactory.getTaskDescriptionRepository(
-                    SynapseConstants.SYNAPSE_STARTUP_TASK_DESCRIPTIONS_REPOSITORY);     
  
-    
     /**
      * Save the path to the configuration file loaded, to save it later if
      * required
@@ -942,16 +935,12 @@
         for (ManagedLifecycle stp : startups.values()) {
             stp.destroy();
         }
-        
-        TaskScheduler taskScheduler = TaskSchedulerFactory.getTaskScheduler(
-                SynapseConstants.SYNAPSE_STARTUP_TASK_SCHEDULER);
+
+        SynapseTaskManager synapseTaskManager = SynapseTaskManager.getInstance();
+        TaskScheduler taskScheduler = synapseTaskManager.getTaskScheduler();
         if (taskScheduler != null && taskScheduler.isInitialized()) {
             taskScheduler.shutDown();
         }
-
-        if (taskDescriptionRepository != null) {
-            taskDescriptionRepository.clear();
-        }
         
         // clear session information used for SA load balancing
         try {
@@ -1023,10 +1012,6 @@
         throw new SynapseException(msg);
     }
 
-    public TaskDescriptionRepository getTaskDescriptionRepository() {
-        return taskDescriptionRepository;
-    }
-
     /**
      * Add an event source to the configuration. If an event source already exists by the
      * specified name a runtime exception is thrown.

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/startup/quartz/SimpleQuartz.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/startup/quartz/SimpleQuartz.java?rev=814533&r1=814532&r2=814533&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/startup/quartz/SimpleQuartz.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/startup/quartz/SimpleQuartz.java
Mon Sep 14 09:09:48 2009
@@ -21,7 +21,6 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.synapse.SynapseConstants;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.config.SynapseConfigUtils;
 import org.apache.synapse.config.SynapseConfiguration;
@@ -49,7 +48,7 @@
 
     private TaskDescription taskDescription;
 
-    private final TaskHelper taskHelper = TaskHelper.getInstance();
+    private final SynapseTaskManager synapseTaskManager = SynapseTaskManager.getInstance();
 
     public QName getTagQName() {
         return SimpleQuartzFactory.TASK;
@@ -64,15 +63,15 @@
             return;
         }
 
-        if (taskHelper.isInitialized()) {
+        if (synapseTaskManager.isInitialized()) {
 
-            TaskScheduler taskScheduler = taskHelper.getTaskScheduler();
+            TaskScheduler taskScheduler = synapseTaskManager.getTaskScheduler();
 
             if (taskScheduler != null && taskScheduler.isInitialized()) {
                 taskScheduler.deleteTask(taskDescription.getName(), taskDescription.getGroup());
             }
 
-            TaskDescriptionRepository repository = taskHelper.getTaskDescriptionRepository();
+            TaskDescriptionRepository repository = synapseTaskManager.getTaskDescriptionRepository();
             if (repository != null) {
                 repository.removeTaskDescription(taskDescription.getName());
             }
@@ -82,22 +81,19 @@
     public void init(SynapseEnvironment synapseEnvironment) {
 
         if (taskDescription == null) {
-            handleException("TaskDescription is null");
+            handleException("Error while initializing the startup. TaskDescription is null.");
         }
 
         SynapseConfiguration synapseConfiguration = synapseEnvironment.getSynapseConfiguration();
 
-        if (!taskHelper.isInitialized()) {
-            taskHelper.init(
-                    TaskDescriptionRepositoryFactory.getTaskDescriptionRepository(
-                            TaskConstants.TASK_DESCRIPTION_REPOSITORY),
-                    TaskSchedulerFactory.getTaskScheduler(TaskConstants.TASK_SCHEDULER));
+        if (!synapseTaskManager.isInitialized()) {
+            synapseTaskManager.init(null, null);
         }
 
-        TaskDescriptionRepository repository = taskHelper.getTaskDescriptionRepository();
+        TaskDescriptionRepository repository = synapseTaskManager.getTaskDescriptionRepository();
 
         if (repository == null) {
-            handleException("Task Description Repository can not found");
+            handleException("Task Description Repository cannot be found");
         }
 
         repository.addTaskDescription(taskDescription);
@@ -112,7 +108,7 @@
                 thisServerName = addr.getHostName();
 
             } catch (UnknownHostException e) {
-                log.warn("Could not get local host name", e);
+                log.warn("Could not get the host name", e);
             }
 
             if (thisServerName == null || thisServerName.equals("")) {
@@ -138,8 +134,7 @@
 
         try {
 
-            TaskScheduler taskScheduler = TaskSchedulerFactory.getTaskScheduler(
-                    SynapseConstants.SYNAPSE_STARTUP_TASK_SCHEDULER);
+            TaskScheduler taskScheduler = synapseTaskManager.getTaskScheduler();
             if (taskScheduler != null) {
                 if (!taskScheduler.isInitialized()) {
                     taskScheduler.init(synapseConfiguration.getProperties());
@@ -148,7 +143,7 @@
             } else {
                 if (log.isDebugEnabled()) {
                     log.debug("TaskScheduler cannot be found for :" +
-                            SynapseConstants.SYNAPSE_STARTUP_TASK_SCHEDULER + " , " +
+                            TaskConstants.TASK_SCHEDULER + " , " +
                             "therefore ignore scheduling of Task  " + taskDescription);
                 }
             }

Added: synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/SynapseTaskManager.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/SynapseTaskManager.java?rev=814533&view=auto
==============================================================================
--- synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/SynapseTaskManager.java
(added)
+++ synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/SynapseTaskManager.java
Mon Sep 14 09:09:48 2009
@@ -0,0 +1,128 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *//**
+ *
+ */
+package org.apache.synapse.task;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.commons.SynapseCommonsException;
+
+/**
+ * Helper class to a share Scheduler and  TaskDescriptionRepository within a single class
space
+ */
+public class SynapseTaskManager {
+
+    private static final Log log = LogFactory.getLog(SynapseTaskManager.class);
+
+    private static SynapseTaskManager INSTANCE = new SynapseTaskManager();
+
+    private TaskDescriptionRepository taskDescriptionRepository;
+    private TaskScheduler taskScheduler;
+    private boolean initialized = false;
+
+    public static SynapseTaskManager getInstance() {
+        return INSTANCE;
+    }
+
+    /**
+     * Initialize the task manager instance with the given task description repository
+     * and the task scheduler. If any of these arguments are null new instances will
+     * be created. Note that this method does not initialize the actual task scheduler
+     * instance. It is up to the task manager clients to make sure that is initialized.
+     *
+     * @param taskDescriptionRepository TaskDescriptionRepository  instance
+     * @param taskScheduler             TaskScheduler instance
+     */
+    public void init(TaskDescriptionRepository taskDescriptionRepository,
+                     TaskScheduler taskScheduler) {
+
+        if (taskDescriptionRepository != null) {
+            this.taskDescriptionRepository = taskDescriptionRepository;
+        } else {
+            if (log.isDebugEnabled()) {
+                log.debug("Creating new TaskDescriptionRepository as given instance is null.");
+            }
+            this.taskDescriptionRepository =
+                    TaskDescriptionRepositoryFactory.getTaskDescriptionRepository(
+                            TaskConstants.TASK_DESCRIPTION_REPOSITORY);
+        }
+
+        if (taskScheduler != null) {
+            this.taskScheduler = taskScheduler;
+        } else {
+            if (log.isDebugEnabled()) {
+                log.debug("Creating new TaskScheduler as given instance is null.");
+            }
+            this.taskScheduler = TaskSchedulerFactory.getTaskScheduler(TaskConstants.TASK_SCHEDULER);
+
+        }
+        initialized = true;
+    }
+
+    public TaskDescriptionRepository getTaskDescriptionRepository() {
+        assertInitialized();
+        return taskDescriptionRepository;
+    }
+
+    public TaskScheduler getTaskScheduler() {
+        assertInitialized();
+        return taskScheduler;
+    }
+
+    private void assertInitialized() {
+        if (!initialized) {
+            String msg = "Task helper has not been initialized, it requires to be initialized";
+            log.error(msg);
+            throw new SynapseCommonsException(msg);
+        }
+    }
+
+    public boolean isInitialized() {
+        return initialized;
+    }
+
+    public void cleanup() {
+        assertInitialized();
+        taskDescriptionRepository.clear();
+        taskScheduler.shutDown();
+    }
+
+    public void pauseAll() {
+        if (taskScheduler != null) {
+            if(taskScheduler.isInitialized()) {
+                try {
+                    taskScheduler.pauseAll();
+                } catch (SynapseTaskException ignore) {
+                    // This exceptions has already been logged and we don't want to interrupt
the flow
+                }
+            }
+        }
+    }
+
+    public void resumeAll() {
+        if (taskScheduler != null) {
+            try {
+                taskScheduler.resumeAll();
+            } catch (SynapseTaskException ignore) {
+                // This exceptions has already been logged and we don't want to interrupt
the flow
+            }
+        }
+    }
+}



Mime
View raw message