Hybris Task Error: Failed to retrieve lock on task

2019-08-16 18:34发布

问题:

I am trying to trigger a Task Runner through Java in Hybris 5.3

This is my Controller where I Trigger the task:

final TestDataModel dataModel = *save data in model*
dataModel .setRunnerBean("responseTaskRunner");

final TaskConditionModel conditionModel = modelService.create(TaskConditionModel.class);

final String uid = "Task" + System.currentTimeMillis();
conditionModel.setUniqueID(uid);
dataModel .setConditions(Collections.singleton(conditionModel));

        // Start the task in 1 second
 dataModel.setExecutionDate(new Date(System.currentTimeMillis() + 1000));
taskService.scheduleTask(gkaRequestBAPIdataModel);
taskService.triggerEvent(uid);

I get the error:

"[DefaultTaskService] Failed to retrieve lock on task #8796617360968. Skipping it."

while I try to trigger the task. Basically the log inside the Task Runner is not being printed as the Task Runner is not being hit.

回答1:

I think you are mixing time based & event based Trigger.

Time-based: If you want to execute your action in a timely manner then you can scheduleTask like..

ModelService modeService = ...
TaskService taskService = ...

// create model
TaskModel task = modelService.create(TaskModel.class);

// configure it
task.setRunnerBean("MyRunner"); // the action bean name
task.setExecutionTime( new Date() );  // the execution time - here asap

// schedule
taskService.scheduleTask(task);

Event-based: If you want to execute your action on some event then your code would be like

// create models
TaskModel task = modelService.create(TaskModel.class);
TaskConditionModel cond = modelService.create(TaskConditionModel.class);

// configure them
task.setRunnerBean("MyRunner");
// define event name
cond.setUniqueID("MyEventArrived");
// add to task
task.setConditions( Collections.singleton( cond ) );

// schedule
taskService.scheduleTask(task);

To trigger the event

taskService.triggerEvent( "MyEventArrived" );

Read more details here



回答2:

I gave "Deployment table" name while creating the DataModel Bean which extends Task. Looks like we should not give deployment table name. My Code was all okay.