我正在尝试使用dropwizard-sundial来安排工作。在我预定的工作中,我需要访问我的DAO。每当我的预定作业运行时,我都会收到以下错误..
org.hibernate.HibernateException: No session currently bound to execution context
我正在尝试使用此链接中描述的方法,https://github.com/timmolter/XDropWizard在注入全局对象或将参数配置到作业
部分下这是我的应用程序代码:
public class ServiceApplication extends Application<OnboardingServiceConfiguration> {
public static final Logger logger = LoggerFactory.getLogger(OnboardingServiceApplication.class);
public static void main(String[] args) throws Exception {
new OnboardingServiceApplication().run(args);
}
private final HibernateBundle<OnboardingServiceConfiguration> hibernate = new HibernateBundle<OnboardingServiceConfiguration>(com.rm.models.Application.class, EmailQueue.class) {
@Override
public DataSourceFactory getDataSourceFactory(OnboardingServiceConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};
@Override
public String getName() {
return "onboarding";
}
@Override
public void initialize(Bootstrap<OnboardingServiceConfiguration> bootstrap) {
// add scheduled jobs
bootstrap.addBundle(new SundialBundle<OnboardingServiceConfiguration>(){
@Override
public SundialConfiguration getSundialConfiguration(OnboardingServiceConfiguration configuration) {
return configuration.getSundialConfiguration();
}
});
// to the database
bootstrap.addBundle(hibernate);
}
@Override
public void run(OnboardingServiceConfiguration configuration, Environment environment) throws Exception {
// add health checks
final HealthCheckResource resource = new HealthCheckResource(
environment.healthChecks()
);
environment.jersey().register(resource);
// configure DAOs for database management
final ApplicationDAO applicationDAO = new ApplicationDAO(hibernate.getSessionFactory());
final EmailDAO emailDAO = new EmailDAO(hibernate.getSessionFactory());
logger.info("Package set for scheduled jobs: " + configuration.getSundialConfiguration().getAnnotatedJobsPackageName());
environment.getApplicationContext().setAttribute("EmailDAO", emailDAO);
environment.getApplicationContext().setAttribute("ApplicationDAO", applicationDAO);
// add resources for REST API endpoints
environment.jersey().register(new ApplicationResource());
// setup environment to run scheduled jobs
// more info and tutorial found at https://github.com/timmolter/dropwizard-sundial
environment.admin().addTask(new LockSundialSchedulerTask());
environment.admin().addTask(new UnlockSundialSchedulerTask());
environment.admin().addTask(new RemoveJobTriggerTask());
environment.admin().addTask(new AddCronJobTriggerTask());
environment.admin().addTask(new StartJobTask());
environment.admin().addTask(new StopJobTask());
environment.admin().addTask(new RemoveJobTask());
environment.admin().addTask(new AddJobTask());
}
}
这是我的预定工作:
@Override
public void doRun() throws JobInterruptException {
this.logger = LoggerFactory.getLogger(EmailJobs.class);
logger.info("Running Scheduled Tasks:");
this.emailerService = new EmailerService((EmailDAO) SundialJobScheduler.getServletContext().getAttribute("EmailDAO"));
this.applicationDAO = (ApplicationDAO) SundialJobScheduler.getServletContext().getAttribute("ApplicationDAO");
List<Application> applications = applicationDAO.getAllForManagerIncompleteReminder();
}
我正在获取DAO,它们不是null,但它们不受任何会话的约束。我该如何解决这个问题?
(PS,当我通过资源访问它时,hibernate会话工作正常,而不是计划任务)
答案 0 :(得分:0)
在ServiceApplication中按如下方式实例化您的emailerService并将其添加到servletContext。
// configure DAOs for database management
final ApplicationDAO applicationDAO = new ApplicationDAO(hibernate.getSessionFactory());
final EmailDAO emailDAO = new EmailDAO(hibernate.getSessionFactory());
Class[] constructorParams = {EmailDAO.class};
Object[] objectsParam = {emailDAO};
EmailerService emailerService = new UnitOfWorkAwareProxyFactory(hibernate)
.create(EmailerService.class, constructorParams, objectsParam);
environment.getApplicationContext().setAttribute("EMAIL_SERVICE", emailerService);