Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring job launcher not able to access services

Hi Still learning hope someone can fill in the blanks

I have JSVC quartz spring batch system that has been running for over a year. The Job launcher needs to connect to 2 spring services that successfully run in other parts of the system. Each service has a number of sql repositories or services injected into it. Please make note of package declarations. for the application context entry.

package com.mycompany.business.services.impl;
....
@Service
public class BatchProcessService {
   private final DomainSepcificRepository1 rep1;
   ...
   private final DomainSepcificRepositoryN repN;

   @Inject
   public BatchProcessService(Final final DomainSepcificRepository1 rep1,..., final DomainSepcificRepositoryN repN) {
   // injected values assigned to instance variables.
   }
   public List <...> findByCriteria(.....)(
      .....
   }
}

and

package com.mycompany.business.services.impl;
 ....
 @Service
 public class EmailNotificationServiceImpl implements EmailNotificationService {

     private UserService userService;

     private final MailMessage mailMessage;

     private final MailTransport mailTransport;

     @Inject
     public EmailNotificationServiceImpl(final UserService userService, final MailMessage mailMessage, final MailTransport mailTransport) {
        this.userService = userService;
        this.mailMessage = mailMessage;
        this.mailTransport = mailTransport;
     }
     .....
     public void notifySupportStaff(....){
          .....
    }
}

In my application context xml file, there is the following line that should allow my job launcher to see and instantiate the above services. I think "base-package=" specifies the packages to look for @services, @components and @repositories that can be injected.

 <context:component-scan base-package="com.mycompany.common.batch, com.mycompany.batch, com.mycompany.business.services" >
    <context:exclude-filter type="assignable" expression="com.mycompany.common.batch.scheduler.service.MyCompanySchedulerService"/>
 </context:component-scan>  
  1. I think that @Component should allow this class to see the services and instaniate them and any dependancies (other services) they have.
  2. For some reason the jsvc system only wants to invoke class below with the NO arg constructor. and it is not injecting the 2 services.
  3. My unit tests are able to test the method using the service only if I provide a 2 argument constructor for MOCK services. lll

Any thoughts on why batch system cannot inject the dependencies?

package com.mycompany.batch.scheduler;
 ....
 @Inject
 private BatchProcessService batchProcessService;
 @Inject
 private EmailNotificationService emailNotificationService;
 @Component
 public class MyCompanySchedulerJobLauncher extends SchedulerJobLauncher {

 public MyCompanySchedulerJobLauncher() {
    super();
}

// @Inject
public MyCompanySchedulerJobLauncher(final BatchProcessService batchProcessService, final EmailNotificationService emailNotificationService) {
    super();
    this.batchProcessService = batchProcessService;
    this.emailNotificationService = emailNotificationService;
}

@Override
public int processJob(final JobExecutionContext context) throws JobRestartException, JobExecutionAlreadyRunningException, ParseException {
......
    if(batchProcessSerive.findByCriteria(....).size() == 0) {
        emailNotificationService.notifySupport(...)
     }
}
like image 675
peter cooke Avatar asked Nov 08 '22 17:11

peter cooke


1 Answers

Well Don't I feel silly. The problem was that at the point where I was assuming I could/would inject dependancies. The application context was private. Once I made my application context protected and to get the services. all worked

like image 132
peter cooke Avatar answered Nov 15 '22 05:11

peter cooke