0

I have a weird problem regarding @PostConstruct. I have several classes, which are run during initialization to load background tasks into memory.

I have written 3 of these classes. Only one of those classes PostConstruct method is run during startup. I have deleted other methods in these following classes, because they do not matter. SchedulableService only holds helper methods

this one work and is initialized correctly with the message appearing in log

package ee.rmit.tor.ejb.timer;

import ee.rmit.tor.domain.enums.SystemParameterType;
import ee.rmit.tor.ejb.search.WorkingSearchService;
import org.slf4j.Logger;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.ScheduleExpression;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Singleton
@Startup
@TransactionManagement(TransactionManagementType.BEAN)
public class NightlyWorkTerminationEmailSender extends SchedulableService {
    private final static String TIMER_ID = "WorkTerminationEmailNotificationServiceTimer";

    /**
     * Default schedule time
     */
    public static final String DEFAULT_SCHEDULE_TIME = "05:00";


    @PersistenceContext
    EntityManager entityManager;

    @Inject
    private Logger logger;

    @Resource
    private TimerService timerService;

    @Inject
    protected WorkingSearchService workingSearchService;


    /**
     * Schedules the timer to run at predetermined time
     */

    @PostConstruct
    public void postConstruct() {
        // get the configuration parameter from DB
        Date notificationTime = getNextExecutionTime(SystemParameterType.EMAIL_TEAVITUS_TOOTAJALE_AEG,
                                                     DEFAULT_SCHEDULE_TIME);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(notificationTime);

        String timeHhMm = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
        // create schedule
        ScheduleExpression schedule = new ScheduleExpression();
        schedule.hour(calendar.get(Calendar.HOUR_OF_DAY)).minute(calendar.get(Calendar.MINUTE));

        timerService.createCalendarTimer(schedule, new TimerConfig(TIMER_ID, false));
        logger.info("Work termination Email notification service has been scheduled to run every day at {}", timeHhMm);
    }

}

I have written almost the same class, but this does not run at startup. I have tried putting a breakpoint on this class during startup aswell, but it does not break inside this class nor is there anything in the logs. Could someone suggest why? I have been googling around, but without help. The stack during init is clear and the project "boots" up fine.

package ee.rmit.tor.ejb.timer;

import ee.rmit.tor.domain.enums.SystemParameterType;
import ee.rmit.tor.ejb.search.WorkingSearchService;
import org.slf4j.Logger;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.ScheduleExpression;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Singleton
@Startup
@TransactionManagement(TransactionManagementType.BEAN)
public class NightlyWorkSuspensionEmailSender extends SchedulableService {
    private final static String TIMER_ID = "WorkSuspensionEmailNotificationServiceTimer";

    /**
     * Default schedule time
     */
    public static final String DEFAULT_SCHEDULE_TIME = "05:00";


    @PersistenceContext
    EntityManager entityManager;

    @Inject
    private Logger logger;

    @Resource
    private TimerService timerService;

    @Inject
    protected WorkingSearchService workingSearchService;


    /**
     * Schedules the timer to run at predetermined time
     */

    @PostConstruct
    public void postConstruct() {
        // get the configuration parameter from DB
        Date notificationTime = getNextExecutionTime(SystemParameterType.EMAIL_TEAVITUS_TOOTAJALE_AEG,
                                                     DEFAULT_SCHEDULE_TIME);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(notificationTime);

        String timeHhMm = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
        // create schedule
        ScheduleExpression schedule = new ScheduleExpression();
        schedule.hour(calendar.get(Calendar.HOUR_OF_DAY)).minute(calendar.get(Calendar.MINUTE));

        timerService.createCalendarTimer(schedule, new TimerConfig(TIMER_ID, false));
        logger.info("Work suspension Email notification service has been scheduled to run every day at {}", timeHhMm);
    }

}

Why doesn't NightlyWorkSuspensionEmailSender postConstruct method work, when it is basically identical with NightlyWorkTerminationEmailSender

Cœur
  • 37,241
  • 25
  • 195
  • 267
DarkFeud
  • 181
  • 2
  • 16

1 Answers1

0

You have 2 Session Beans with same interface. It's not a bad practice, but you need different JNDI names, or will confuse the container. Probably only first EJB is started.

Try creating specializations of your SchedulableService to each Session Bean.

Gilliard Macedo
  • 386
  • 1
  • 6
  • I have 7 nightly processes extending SchedulableService. 5 of them get run during initialization (2 others are just not init as stated above), but I'll try creating specializations for the 2 of them that randomly don't. Thanks for the suggestions, I have been stuck in this old project and can't figure out how to solve this – DarkFeud Nov 10 '17 at 07:11