I searched for some answer to my question on this site; but failed on every turn. I can delete fine if I don't put this in a ExecutorService, but if I do, it doesn't delete. No error occurs just the records are still in the database. Please advise.
public void deleteAllTrials(List<Trials>list) {
threadList = list;
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(new Job1());
executor.shutdown();
}
public class Job1 implements Runnable {
@Override
public void run() {
//Session session = (Session) entityManager.getDelegate();
EntityManagerFactory emf = entityManager.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
System.out.println("Size of threadList" + threadList.size());
long start = System.currentTimeMillis();
for(int i =0; i<threadList.size(); i++){
System.out.println("In thread...");
Trials mergedEntity = em.merge(threadList.get(i));
em.remove(mergedEntity);
}
//System.out.println("Result list in service:" + list.size());
//em.close();
long end = System.currentTimeMillis();
System.out.println("Threads took this long:" + (end - start));
}
}
I found out that EJBs are more powerful than I thought. If you just add @Asynchronus on top of the method you want the application to separate in a backing thread, it will be acting as a separate thread allowing the user to continue doing what he wants to do without waiting on the process to finish.
@Asynchronous
public void deleteAllTrials(List<TrialBillet>list) {
List<TrialBillet> threadList = new ArrayList<TrialBillet>();
threadList = list;
for(int i =0; i<threadList.size(); i++){
this.delete(threadList.get(i));
}
}