QuartzJob中的EntityManager null

时间:2017-11-27 12:05:42

标签: java-ee eclipselink quartz-scheduler weblogic12c

我在QuartzJob实例上使用EntityManager来解决这个问题:

  

显示java.lang.NullPointerException     在weblogic.persistence.CICScopedEMProvider.getEMForCurrentCIC(CICScopedEMProvider.java:35)       at weblogic.persistence.TransactionalEntityManagerProxyImpl.getPersistenceContext(TransactionalEntityManagerProxyImpl.java:122)       at weblogic.persistence.BasePersistenceContextProxyImpl.invoke(BasePersistenceContextProxyImpl.java:94)       在weblogic.persistence.TransactionalEntityManagerProxyImpl.invoke(TransactionalEntityManagerProxyImpl.java:164)       at weblogic.persistence.BasePersistenceContextProxyImpl.invoke(BasePersistenceContextProxyImpl.java:86)       在com.sun.proxy。$ Proxy321.createQuery(未知来源)       at com.myproject.repository.impl.PhotoRepositoryImpl.getShotsFromBackupOption(PhotoRepositoryImpl.java:44)       在com.myproject.service.impl.PhotoServiceImpl.getPhoto(PhotoServiceImpl.java:90)       在com.myproject.monitor.BackupJob.execute(BackupJob.java:46)       在org.quartz.core.JobRunShell.run(JobRunShell.java:202)       在org.quartz.simpl.SimpleThreadPool $ WorkerThread.run(SimpleThreadPool.java:573)

这是我的工作:

public class BackupJob implements Job{
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Inject private PhotoService photoService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            String jobName = context.getJobDetail().getKey().getName();
            Long jobGroup = Long.parseLong(context.getJobDetail().getKey().getGroup());

            List<File> fileList = photoService.getPhoto(jobName, jobGroup);

            logger.info("Backup with deep {}", context.getJobDetail().getJobDataMap().get("deep"));
        } catch (Exception e) {
            logger.error("ERROR during backup job", e);
        }       
    }
}

(正确注入了photoService,对于我在photoService中调用的photoRepository方法也是如此)

这是我的存储库:

@Stateless
public class PhotoRepositoryImpl implements PhotoRepository{

    @PersistenceContext(unitName=Constant.PU_NAME)
    private EntityManager em;

    @Override
    public List<Photo> getShotsFromBackupOption(String jobName, Long jobGroup) throws SQLException{

        Query query = em.createQuery("SELECT p "
                                    + "FROM Shot s "
                                    + "JOIN Photo p "
                                    + "WHERE s.dateShooting < "
                                        + "(SELECT b.previousThan "
                                        + "FROM BackupOption b "
                                        + "WHERE b.jobName = :jobName AND b.syncType.id = :jobGroup)"
                                    + "ORDER BY p.relativePath DESC");
        query.setParameter("jobName", jobName);
        query.setParameter("jobGroup", jobGroup);


        return (List<Photo>) query.getResultList();
    }
}

这是我的工作工厂

public class JobFactoryImpl implements JobFactory {


@Inject
private Instance<Job> jobs;

@Override
public Job newJob(TriggerFiredBundle triggerFiredBundle, Scheduler scheduler) throws SchedulerException {
    final JobDetail jobDetail = triggerFiredBundle.getJobDetail();
    final Class<? extends Job> jobClass = jobDetail.getJobClass();

    for (Job job : jobs) {
        if (job.getClass().isAssignableFrom(jobClass)) {
            return job;
        }
    }

    throw new RuntimeException("Cannot create a Job of type " + jobClass);
}}

在em.createquery(---)方法上抛出NullPointerException。 当我的QuartzJob调用方法时,此查询很有用。 你有一些很好的解决方法吗?

1 个答案:

答案 0 :(得分:1)

我解决了以这种方式获取EntityManager的问题:

@PersistenceUnit(unitName= Constant.PU_NAME)
private EntityManagerFactory emf;

private EntityManager em = emf.createEntityManager();