jakarta-eeeclipselinkquartz-schedulerweblogic12c

EntityManager null in QuartzJob


i've this issue using EntityManager on QuartzJob instance:

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

This is my job:

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 is correctly injected, same thing for my photoRepository method called in photoService)

This is my repository:

@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();
    }
}

This is my job factory

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);
}}

NullPointerException is thrown on em.createquery(---) method. This query works nice when method is called out of my QuartzJob. Do you have some nice workaround for this?


Solution

  • I solved getting EntityManager in this way:

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