cancel
Showing results for 
Search instead for 
Did you mean: 

Spring boot application - Injecting HistoryService in a TaskListener returns Null

papalanisamy
Champ on-the-rise
Champ on-the-rise

I am working in a spring boot application. 
when i try to call tasklistener class, The autowired historyservice is returning null. 

process service‌ tasklistener 

Can anyone please help !

1 ACCEPTED ANSWER

papalanisamy
Champ on-the-rise
Champ on-the-rise

Thanks both of you.. 

Finally the issue is solved by passing the listener in delegate expression instead of class .

<activiti:taskListener event="all" delegateExpression="${TestClass}"></activiti:taskListener>

View answer in original post

8 REPLIES 8

bassam_al-saror
Star Collaborator
Star Collaborator

TaskListeners that are set in 'class' attribute are created/instantiated by the engine so Spring autowiring will not work. 

<activiti:taskListener event="complete" class="example.MyTaskListener">

Hi Bassam,

Thanks for your prompt response.

Here is my code. any changes to be done for this. 

public class TestClass implements TaskListener {


  @Autowired
  private HistoryService historyService;
  public void notify(DelegateTask delegateTask) {
    String currentEvent = delegateTask.getEventName();
    if("delete".equalsIgnoreCase(currentEvent))
      return;
    try {
    
      
      System.out.println(historyService);// it returns null, please let us know the workaround
      
      }
  }
  
  
  XML File :
  
         
        <activiti:taskListener event="all" class="com.callback.TestClass"></activiti:taskListener>

this is spring boot application. anything specific for springboot ?? 

You can try registering your task listener as a spring bean (i.e. using @Component) and use an expression to call it. 

<activiti:taskListener event="all" expression="${myTaskListener.notify(execution)}" />

By the way, it could be any class (spring bean) doesn't have to implement TaskListener.

rupeshsawaliya
Star Contributor
Star Contributor

Hi,

As Bassam Al-Sarori‌ mentioned task listeners are basically bind in context of task and workflow so it should be set in your workflow tasks, not separated wired as a spring bean .

Regards,

Rupesh

Hi Rupesh,

Thanks for your prompt response.

We are using SPRING BOOT .

Here is my code. any changes to be done for this. 

public class TestClass implements TaskListener {


  @Autowired
  private HistoryService historyService;
  public void notify(DelegateTask delegateTask) {
    String currentEvent = delegateTask.getEventName();
    if("delete".equalsIgnoreCase(currentEvent))
      return;
    try {
    
      
      System.out.println(historyService);// it returns null, please let us know the workaround
      
      }
  }
  
  
  XML File :
  
         
        <activiti:taskListener event="all" class="com.callback.TestClass"></activiti:taskListener>

rupeshsawaliya
Star Contributor
Star Contributor

Hi,

Here i think you may have to depends-on attribute on task listener class the way it is added in following bean definition.

<bean id="abc" class="com.test.ABC" parent="baseJavaDelegate" depends-on="activitiBeanRegistry"
abstract="true"> 

I think this will help you. 

Regards,

Rupesh Sawaliya

EnProwess Technologies

papalanisamy
Champ on-the-rise
Champ on-the-rise

Thanks both of you.. 

Finally the issue is solved by passing the listener in delegate expression instead of class .

<activiti:taskListener event="all" delegateExpression="${TestClass}"></activiti:taskListener>