@Override
public void cancelJob(int idJob)
{
EntityManager em = null;
JobInstance ji = null;
try
{
em = getEm();
em.getTransaction().begin();
ji = em.find(JobInstance.class, idJob, LockModeType.PESSIMISTIC_WRITE);
if (ji.getState().equals(State.SUBMITTED))
{
ji.setState(State.CANCELLED);
}
else
{
throw new NoResultException();
}
em.getTransaction().commit();
}
catch (NoResultException e)
{
closeQuietly(em);
throw new JqmClientException("the job is already running, has already finished or never existed to begin with");
}
try
{
em.getTransaction().begin();
History h = new History();
h.setId(ji.getId());
h.setJd(ji.getJd());
h.setSessionId(ji.getSessionID());
h.setQueue(ji.getQueue());
h.setMessages(new ArrayList<Message>());
h.setEnqueueDate(ji.getCreationDate());
h.setUserName(ji.getUserName());
h.setEmail(ji.getEmail());
h.setParentJobId(ji.getParentId());
h.setApplication(ji.getApplication());
h.setModule(ji.getModule());
h.setKeyword1(ji.getKeyword1());
h.setKeyword2(ji.getKeyword2());
h.setKeyword3(ji.getKeyword3());
h.setProgress(ji.getProgress());
h.setParameters(new ArrayList<JobHistoryParameter>());
h.setStatus(State.CANCELLED);
h.setNode(ji.getNode());
em.persist(h);
em.createQuery("DELETE FROM MessageJi WHERE jobInstance = :i").setParameter("i", ji).executeUpdate();
em.createQuery("DELETE FROM JobParameter WHERE jobInstance = :i").setParameter("i", ji).executeUpdate();
em.createQuery("DELETE FROM JobInstance WHERE id = :i").setParameter("i", ji.getId()).executeUpdate();
em.getTransaction().commit();
}
catch (Exception e)
{
throw new JqmClientException("could not cancel job instance", e);