Sam
Sam

Reputation: 3054

Alfresco Process Services with TaskListener @Autowired issue

I am using Alfresco Process Services and have created a created a spring boot project for custom logic like TaskListeners and Delegations. I am creating the jar file from this maven project and copying it into webapps/activiti-app/WEB-INF/lib folder.

I have a simple TaskListener as below which is getting called on Task start. But the @Autowired variables are always null.

package com.activiti.extension.bean;

@Component("myTaskListener")
public class MyTaskListener implements TaskListener {

@Autowired
UserService userService;

@Override
public void notify(DelegateTask task) { 
   logger.info("userService: " +userService); // Always prints null
}

Upvotes: 0

Views: 786

Answers (4)

Karlo Sušanj
Karlo Sušanj

Reputation: 61

There is also one more way to get to your custom service "UserService" using Alfresco Spring Application context.

First access ServiceRegistry (registry used for accessing Alfresco Services but also any other custom service):

ServiceRegistry serviceRegistry = (ServiceRegistry) Context.getProcessEngineConfiguration().getBeans().get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);

Then get custom service UserService:

QName qname = QName.createQName("UserService");
UserService userService = (UserService) serviceRegistry.getService(qname);

Upvotes: 0

Noyal
Noyal

Reputation: 243

This below code is worked for me

@Component
 public class MyTaskListener implements TaskListener {
    public static UserService getUserServiceObject() {
    return SpringApplicationContextHolder.getApplicationContext().getBean(UserService.class);
  }
@Override
  public void notify(DelegateTask delegateTask) {
  //UserService Object, It is not null now
   getUserServiceObject(); 
  }
}

@Component
public class SpringApplicationContextHolder implements ApplicationContextAware {

private static ApplicationContext applicationContext = null;

public static ApplicationContext getApplicationContext() {
    return applicationContext;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
   this.applicationContext = applicationContext;
   }
  }

Upvotes: 0

Sam
Sam

Reputation: 3054

Finally I was able to make it work. I was putting the task listener in the class field of the Task properties with full package name. Now I am putting Delegate expression like ${myTaskListener} and it worked...

Thank you all for your time and help

Upvotes: 3

Abbas Kararawala
Abbas Kararawala

Reputation: 1254

This is because your your MyTaskListener is annotated as @Component or at least being ignored by spring during init. for auto-wiring capabilities spring requires this annotation (or similar to this) under the provided @ComponentScan packages to consider the class as a bean otherwise it will take as a normal java class and hence the @autowired is of no use in your case.

Upvotes: 1

Related Questions