1

I want to run some process just after the deployment of Glassfish. Process will run every hour and it contains fetching data from DB table via stateless bean CarService with findAll() below:

@PersistenceContext
private EntityManager em;

public List<Cars> findAll() {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(Cars.class));
    return em.createQuery(cq).getResultList();
}

Then i am using ScheduledExecutorService with starts process after deployment.

@ManagedBean(eager=true)
@ApplicationScoped
public class ApplicationStateChange {

private ScheduledExecutorService scheduler;

@PostConstruct
public void init() {
    System.out.println("ejb init method called");
    scheduler = Executors.newScheduledThreadPool(2);
    scheduler.scheduleAtFixedRate(new ScheduleTask();, 15, 30, TimeUnit.SECONDS);
}

@PreDestroy
    public void destroy() {
        /* Shutdown stuff here */
        System.out.println("ejb destroy method called");
        scheduler.shutdownNow();
    }

above ScheduleTask() contains the process including business logic e.g:

public class ScheduleTask implements Runnable {

    @Inject
    CarService carService;
    private volatile ScheduledExecutorService scheduler = null;

    @Override
    public void run() {
        System.out.println("scheduletask is called");
        List<Car> carList = new ArrayList<>();
               carList = carService.findAll();
        if (carList != null) {
            for (Car car : carList) {
                System.out.println(car);
            }
        }
    }

i am unable to get the findALL() method by injecting into above runnable class. scheduler works fine but it fails when it reaches to carList = carService.findAll(); infact its failing at javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();

I suspect persistence context is not loaded properly at the time of its calling.

I have followed following questionsSpawning threads in a JSF managed bean for scheduled tasks using a timer

scheduledExecutorService, timerService and Stateless EJB on scheduled jobs

Community
  • 1
  • 1
umaair_653
  • 113
  • 10

1 Answers1

1

As clearly exposed in the answer for the first question you have linked, simply use @Schedule into a @Singleton SessionBean annotated with @Startup in order to ensure it runs when the server starts or the application is deployed.

As you correctly mentioned, EntityManager and PersistenceContext cannot be injected into a non container-mananged class (and Singleton SessionBean is a managed class).

Linked Answer:

Spawning threads in a JSF managed bean for scheduled tasks using a timer

Community
  • 1
  • 1
perissf
  • 15,979
  • 14
  • 80
  • 117
  • OK i can with container managed '@Schedule' instead of **ScheduledExecutorService** but now i have to tackle with Timer configuration of glassfish which is painful because it is replying **'Information: There are no EJB Timers owned by this server Information: <== Timers Restored.'** thats the reason i was trying to use **ScheduledExecutorService** which will run process at fixedrate by getting runnable implementation. Is there any way to hook/inject my EntityManager in runnable? – umaair_653 Feb 22 '17 at 12:34
  • No, you cannot inject EntityManager into a non container-managed class, and you should avoid managing tasks yourself. Regarding how to configure Timer in Glassfish, this is a good point, but it's also totally a new question. Open a new question. – perissf Feb 22 '17 at 12:46
  • thanks for the hint. i have tested my entitymanager with scheduler and works fine. this Scheduler is same like ScheduledExecutorService?? means tasks will execute asynchronously by a worker thread, and not by the thread handing the task to the Scheduler. otherwise i have to manage again all subsequent tasks by using ScheduledExecutorService for every element of ArrayList from EntityManager – umaair_653 Feb 22 '17 at 14:21
  • Unfortunately I am not so keen on ScheduledExecutorService, and honestly from your comment I am not able to understand which is the problem. You should really setup a mvce in a new question – perissf Feb 22 '17 at 15:09