Search in sources :

Example 1 with TaskDescription

use of org.apache.synapse.task.TaskDescription in project wso2-synapse by wso2.

the class ScheduledMessageProcessor method start.

@Override
public boolean start() {
    for (int i = 0; i < memberCount; i++) {
        /*
			 * Make sure to fetch the task after initializing the message sender
			 * and consumer properly. Otherwise you may get NullPointer
			 * exceptions.
			 */
        task = this.getTask();
        TaskDescription taskDescription = new TaskDescription();
        taskDescription.setName(TASK_PREFIX + name + i);
        taskDescription.setTaskGroup(MessageProcessorConstants.SCHEDULED_MESSAGE_PROCESSOR_GROUP);
        /*
			 * If this interval value is less than 1000 ms, ntask will throw an
			 * exception while building the task. So to get around that we are
			 * setting threshold interval value of 1000 ms to the task
			 * description here. But actual interval value may be less than 1000
			 * ms, and hence isThrotling is set to TRUE.
			 */
        if (interval < MessageProcessorConstants.THRESHOULD_INTERVAL) {
            taskDescription.setInterval(MessageProcessorConstants.THRESHOULD_INTERVAL);
        } else {
            taskDescription.setInterval(interval);
        }
        taskDescription.setIntervalInMs(true);
        taskDescription.addResource(TaskDescription.INSTANCE, task);
        taskDescription.addResource(TaskDescription.CLASSNAME, task.getClass().getName());
        /*
			 * If there is a Cron Expression we need to set it into the
			 * TaskDescription so that the framework will take care of it.
			 */
        if (cronExpression != null) {
            taskDescription.setCronExpression(cronExpression);
        }
        taskManager.schedule(taskDescription);
    }
    logger.info("Started message processor. [" + getName() + "].");
    /*
         * If the Message Processor is Deactivated through the Advanced parameter 
         * explicitly, then we deactivate the task immediately.
         */
    if (isProcessorStartAsDeactivated()) {
        deactivate();
    }
    return true;
}
Also used : TaskDescription(org.apache.synapse.task.TaskDescription)

Example 2 with TaskDescription

use of org.apache.synapse.task.TaskDescription in project wso2-synapse by wso2.

the class SimpleQuartzSerializer method serializeStartup.

public OMElement serializeStartup(OMElement parent, Startup s) {
    if (!(s instanceof StartUpController)) {
        throw new SynapseException("called TaskSerializer on some other " + "kind of startup" + s.getClass().getName());
    }
    StartUpController sq = (StartUpController) s;
    TaskDescription taskDescription = sq.getTaskDescription();
    if (taskDescription != null) {
        OMElement task = TaskDescriptionSerializer.serializeTaskDescription(SynapseConstants.SYNAPSE_OMNAMESPACE, taskDescription);
        if (task == null) {
            throw new SynapseException("Task Element can not be null.");
        }
        if (parent != null) {
            parent.addChild(task);
        }
        return task;
    } else {
        throw new SynapseException("Task Description is null");
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) TaskDescription(org.apache.synapse.task.TaskDescription) OMElement(org.apache.axiom.om.OMElement)

Example 3 with TaskDescription

use of org.apache.synapse.task.TaskDescription in project wso2-synapse by wso2.

the class SimpleQuartzFactory method createStartup.

public Startup createStartup(OMElement el) {
    if (log.isDebugEnabled()) {
        log.debug("Creating StartUpController Task");
    }
    if (el.getQName().equals(TASK)) {
        StartUpController startUpController = new StartUpController();
        TaskDescription taskDescription = TaskDescriptionFactory.createTaskDescription(el, XMLConfigConstants.SYNAPSE_OMNAMESPACE);
        if (taskDescription == null) {
            handleException("Invalid task - Task description can not be created  from :" + el);
            return null;
        }
        startUpController.setName(taskDescription.getName());
        startUpController.setTaskDescription(taskDescription);
        startUpController.setDescription(taskDescription.getTaskDescription());
        return startUpController;
    } else {
        handleException("Syntax error in the task : wrong QName for the task");
        return null;
    }
}
Also used : TaskDescription(org.apache.synapse.task.TaskDescription)

Example 4 with TaskDescription

use of org.apache.synapse.task.TaskDescription in project wso2-synapse by wso2.

the class SynapseXMLConfigurationSerializerTest method testSerializeConfiguration8.

/**
 * Test serializeConfigurationMethod with startUp added for SynapseConfiguration and assert OMElement returned
 */
@Test
public void testSerializeConfiguration8() {
    SynapseXMLConfigurationSerializer serializer = new SynapseXMLConfigurationSerializer();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    StartUpController startup = new StartUpController();
    startup.setName("testStartup");
    TaskDescription taskDescription = new TaskDescription();
    taskDescription.setName("testTask");
    startup.setTaskDescription(taskDescription);
    synapseConfiguration.addStartup(startup);
    OMElement element = serializer.serializeConfiguration(synapseConfiguration);
    Assert.assertNotNull("OMElement is not returned", element);
    Assert.assertEquals("definitions", element.getLocalName());
    Assert.assertTrue("StartUp added is not serialized.", element.getChildren().next().toString().contains("name=\"testTask\""));
}
Also used : StartUpController(org.apache.synapse.startup.quartz.StartUpController) TaskDescription(org.apache.synapse.task.TaskDescription) OMElement(org.apache.axiom.om.OMElement) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Test(org.junit.Test)

Example 5 with TaskDescription

use of org.apache.synapse.task.TaskDescription in project wso2-synapse by wso2.

the class TaskSchedulerTest method testScheduleTask.

@Test()
public void testScheduleTask() throws Exception {
    String path = this.getClass().getClassLoader().getResource("task/task_TestScheduler.xml").getFile();
    OMElement taskOme = loadOMElement(path);
    TaskDescription taskDescription = TaskDescriptionFactory.createTaskDescription(taskOme, SYNAPSE_OMNAMESPACE);
    TaskManager quartzTaskManager = Mockito.mock(TaskManager.class);
    Mockito.when(quartzTaskManager.schedule(taskDescription)).thenReturn(true);
    org.apache.synapse.task.TaskScheduler taskScheduler = new org.apache.synapse.task.TaskScheduler("task");
    Assert.assertFalse("Task cannot be scheduled before initialization.", taskScheduler.scheduleTask(taskDescription));
    taskScheduler.init(new Properties(), quartzTaskManager);
    Assert.assertTrue("Task at file task_TestScheduler.xml not " + "scheduled even after initializing the scheduler.", taskScheduler.scheduleTask(taskDescription));
}
Also used : TaskManager(org.apache.synapse.task.TaskManager) TaskDescription(org.apache.synapse.task.TaskDescription) OMElement(org.apache.axiom.om.OMElement) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

TaskDescription (org.apache.synapse.task.TaskDescription)5 OMElement (org.apache.axiom.om.OMElement)3 Test (org.junit.Test)2 Properties (java.util.Properties)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)1 StartUpController (org.apache.synapse.startup.quartz.StartUpController)1 TaskManager (org.apache.synapse.task.TaskManager)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1