Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Calling method from inside the constructor

HI,
This question is about calling a method from inside a constructor. As i have read many places that one should not do this as the class is not created. I saw a similar code here :
@EagerSingleton
public class SftpUploaderCron {
  private final SpectraLogger logger = OlympusSpectra.getLogger(SftpUploaderCron.class);
  private final ZetaHostMessagingService zhms;
  private final TaskScheduler taskScheduler;
  private SftpDocsUploadHandler sftpDocsUploadHandler;
  private ImmutableMap<String, Long> nameIFIMapping;
  private ScheduledFuture scheduledFuture;


  @Inject
  public SftpUploaderCron(ZetaHostMessagingService zetaHostMessagingService,
                          SftpDocsUploadHandler sftpDocsUploadHandler,
                          @Named("nameIFIMapping") ImmutableMap<String, Long> nameIFIMapping) {
    logger.info("Inside SftpUploaderCron constructor").log();
    this.taskScheduler = new ThreadPoolTaskScheduler();
    ((ThreadPoolTaskScheduler)taskScheduler).initialize();
    this.zhms = zetaHostMessagingService;
    this.sftpDocsUploadHandler = sftpDocsUploadHandler;
    this.nameIFIMapping = nameIFIMapping;
    startSftpUploaderThread();
  }


  private void startSftpUpload() {
    logger.info("Inside startSftpUpload").log();
    if (scheduledFuture != null)
      scheduledFuture.cancel(true);
    long period = 30 * 60 * 1000l;
    Trigger trigger = new PeriodicTrigger(period);
    scheduledFuture = taskScheduler.schedule(this::scheduledKycUploadTask, trigger);
  }

  private void scheduledKycUploadTask() {
    logger.info("Inside scheduledKycUploadTask").log();
    sftpDocsUploadHandler.fetchDailyApplications(nameIFIMapping.get(RBL)).thenAccept((list) -> {
      if (list.size() == 0) {
        scheduledFuture.cancel(false);
        //TODO: make a call to jasper that the processing is over
      }
      else {
        sftpDocsUploadHandler.uploadApplications(list);
      }
    });
  }

  public void startSftpUploaderThread() {
    final JID entity = getEntity();
    logger.info("")
        .log();

    zhms.getSpartan().onAllocation(entity, (ignored) -> {
      if (!zhms.isLocalNode(entity)) {
        logger.info("genericCron.nonLocalNode")
            .log();
      } else {
        Trigger trigger = new CronTrigger("0 28 12 * * *");
        taskScheduler.schedule(this::startSftpUpload, trigger);
      }
    });
  }


  public JID getEntity() {
    return new JID("sftp_cron_user@app-portal.zeta.in");
  }

}

Open in new window


In practice has anyone seen any problem while calling a method from inside a constructor ?
or do you see any issue with the above ?
Thanks
Avatar of Jeffrey Dake
Jeffrey Dake
Flag of United States of America image

There is absolutely nothing wrong with calling a function from within the constructor.  I often do it just to keep what I am initializing simple to read by having specific initialization functions.  I have a feeling what you are reading is that you shouldn't call functions within the constructor that you wouldn't consider as initializing the object.
ASKER CERTIFIED SOLUTION
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial