关于@PostConstruct我有一个奇怪的问题。我有几个类,它们在初始化期间运行,以将后台任务加载到内存中。
我已经写了3个这样的课程。 在启动期间只运行其中一个类PostConstruct方法。 我删除了以下这些类中的其他方法,因为它们并不重要。 SchedulableService只包含帮助方法
这个工作并正确初始化,并显示在日志
中的消息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);
}
}
我写的几乎是同一个类,但这不会在启动时运行。我已经尝试在启动期间在这个类上设置一个断点,但它不会在这个类中破坏,也不会在日志中有任何内容。有人可以建议为什么?我一直在谷歌上搜索,但没有帮助。初始化期间的堆栈是清晰的,项目" boot"很好。
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);
}
}
为什么NightlyWorkSuspensionEmailSender postConstruct方法工作时,它与NightlyWorkTerminationEmailSender基本相同
答案 0 :(得分:0)
您有2个具有相同界面的会话Bean。这不是一个坏习惯,但你需要不同的JNDI名称,否则会混淆容器。可能只启动了第一个EJB。
尝试为每个会话Bean创建SchedulableService的特化。