Spring boot application - Injecting HistoryService in a TaskListener returns Null

cancel
Showing results for 
Search instead for 
Did you mean: 
papalanisamy
Active Member

Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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 Solution

Accepted Solutions
papalanisamy
Active Member

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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 solution in original post

8 Replies
bassam_al-saror
Alfresco Employee

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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">

rupeshsawaliya
Established Member

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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

papalanisamy
Active Member

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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>

papalanisamy
Active Member

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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>

papalanisamy
Active Member

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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

rupeshsawaliya
Established Member

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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

bassam_al-saror
Alfresco Employee

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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.

papalanisamy
Active Member

Re: Spring boot application - Injecting HistoryService in a TaskListener returns Null

Jump to solution

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>