Package com.enioka.jqm.jpamodel

Examples of com.enioka.jqm.jpamodel.JobInstance


            throw new JqmClientException("Could not do highlander analysis", e);
        }

        try
        {
            JobInstance ji = new JobInstance();
            ji.setJd(job);
            ji.setSessionID(jd.getSessionID());
            ji.setUserName(jd.getUser());
            ji.setState(State.SUBMITTED);
            ji.setQueue(job.getQueue());
            ji.setNode(null);
            // Can be null (if no email is asked for)
            ji.setEmail(jd.getEmail());
            ji.setCreationDate(Calendar.getInstance());
            if (jd.getParentID() != null)
            {
                ji.setParentId(jd.getParentID());
            }
            ji.setProgress(0);
            ji.setParameters(new ArrayList<JobParameter>());
            em.persist(ji);
            ji.setInternalPosition(ji.getId());

            for (JobParameter jp : jps)
            {
                jqmlogger.trace("Parameter: " + jp.getKey() + " - " + jp.getValue());
                em.persist(ji.addParameter(jp.getKey(), jp.getValue()));
            }
            jqmlogger.trace("JI just created: " + ji.getId());

            em.getTransaction().commit();
            return ji.getId();
        }
        catch (Exception e)
        {
            throw new JqmClientException("Could not create new JobInstance", e);
        }
View Full Code Here


    @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);
View Full Code Here

        try
        {
            em = getEm();

            // Two transactions against deadlock.
            JobInstance job = em.find(JobInstance.class, idJob);
            em.getTransaction().begin();
            em.refresh(job, LockModeType.PESSIMISTIC_WRITE);
            if (job.getState().equals(State.SUBMITTED))
            {
                job.setState(State.CANCELLED);
            }
            em.getTransaction().commit();

            if (!job.getState().equals(State.CANCELLED))
            {
                // Job is not in queue anymore - just return.
                return;
            }

            em.getTransaction().begin();
            em.createQuery("DELETE FROM MessageJi WHERE jobInstance = :i").setParameter("i", job).executeUpdate();
            em.createQuery("DELETE FROM JobParameter WHERE jobInstance = :i").setParameter("i", job).executeUpdate();
            em.createQuery("DELETE FROM JobInstance WHERE id = :i").setParameter("i", job.getId()).executeUpdate();
            em.getTransaction().commit();
        }
        catch (NoResultException e)
        {
            throw new JqmInvalidRequestException("An attempt was made to delete a job instance that did not exist.");
View Full Code Here

        EntityManager em = null;
        try
        {
            em = getEm();
            em.getTransaction().begin();
            JobInstance j = em.find(JobInstance.class, idJob, LockModeType.PESSIMISTIC_READ);
            jqmlogger.trace("The " + j.getState() + " job (ID: " + idJob + ")" + " will be marked for kill");

            j.setState(State.KILLED);

            MessageJi m = new MessageJi();
            m.setJobInstance(j);
            m.setTextMessage("Kill attempt on the job");
            em.persist(m);
View Full Code Here

    @Override
    public void setJobQueue(int idJob, int idQueue)
    {
        EntityManager em = null;
        JobInstance ji = null;
        Queue q = null;

        try
        {
            em = getEm();
            q = em.find(Queue.class, idQueue);
        }
        catch (NoResultException e)
        {
            closeQuietly(em);
            throw new JqmClientException("Queue does not exist");
        }
        catch (Exception e)
        {
            closeQuietly(em);
            throw new JqmClientException("Cannot retrieve queue", e);
        }

        try
        {
            em.getTransaction().begin();
            ji = em.find(JobInstance.class, idJob, LockModeType.PESSIMISTIC_WRITE);
            if (!ji.getState().equals(State.SUBMITTED))
            {
                throw new NoResultException();
            }
            ji.setQueue(q);
            em.getTransaction().commit();
        }
        catch (NoResultException e)
        {
            throw new JqmClientException("Job instance does not exist or has already started");
View Full Code Here

    @Override
    public void setJobQueuePosition(int idJob, int position)
    {
        EntityManager em = null;
        JobInstance ji = null;
        try
        {
            em = getEm();
            em.getTransaction().begin();
            ji = em.find(JobInstance.class, idJob, LockModeType.PESSIMISTIC_WRITE);
        }
        catch (Exception e)
        {
            closeQuietly(em);
            throw new JqmClientException(
                    "Could not lock a job by the given ID. It may already have been executed or a timeout may have occurred.", e);
        }

        if (!ji.getState().equals(State.SUBMITTED))
        {
            closeQuietly(em);
            throw new JqmInvalidRequestException("Job is already set for execution. Too late to change its position in the queue");
        }

        try
        {
            int current = ji.getCurrentPosition(em);
            int betweenUp = 0;
            int betweenDown = 0;

            if (current == position)
            {
                // Nothing to do
                em.getTransaction().rollback();
                return;
            }
            else if (current < position)
            {
                betweenDown = position;
                betweenUp = position + 1;
            }
            else
            {
                betweenDown = position - 1;
                betweenUp = position;
            }

            // No locking - we'll deal with exceptions
            List<JobInstance> currentJobs = em.createQuery("SELECT ji from JobInstance ji ORDER BY ji.internalPosition", JobInstance.class)
                    .setMaxResults(betweenUp).getResultList();

            if (currentJobs.isEmpty())
            {
                ji.setInternalPosition(0);
            }
            else if (currentJobs.size() < betweenUp)
            {
                ji.setInternalPosition(currentJobs.get(currentJobs.size() - 1).getInternalPosition() + 0.00001);
            }
            else
            {
                // Normal case: put the JI between the two others.
                ji.setInternalPosition((currentJobs.get(betweenUp - 1).getInternalPosition() + currentJobs.get(betweenDown - 1)
                        .getInternalPosition()) / 2);
            }
            em.getTransaction().commit();
        }
        catch (Exception e)
View Full Code Here

            {
                res = getJob(h);
            }
            else
            {
                JobInstance ji = em.find(JobInstance.class, idJob);
                if (ji != null)
                {
                    res = getJob(ji);
                }
                else
View Full Code Here

            {
                break;
            }

            // Get a JI to run
            JobInstance ji = dequeue();
            while (ji != null)
            {
                // We will run this JI!
                jqmlogger.trace("JI number " + ji.getId() + " will be run by this poller this loop (already " + actualNbThread + "/"
                        + dp.getNbThread() + " on " + this.queue.getName() + ")");
                actualNbThread++;

                em.getTransaction().begin();
                Helpers.createMessage("Status updated: ATTRIBUTED", ji, em);
View Full Code Here

    // ------------------ JOBINSTANCE --------------------------

    public static JobInstance createJobInstance(JobDef jd, List<JobParameter> jps, String user, String sessionID, State state,
            Integer position, Queue queue, Node node, EntityManager em)
    {
        JobInstance j = new JobInstance();
        jqmlogger.debug("Creating JobInstance with " + jps.size() + " parameters");
        for (JobParameter jp : jps)
        {
            jqmlogger.debug("Parameter: " + jp.getKey() + " - " + jp.getValue());
        }
        j.setJd(jd);
        j.setSessionID(sessionID);
        j.setUserName(user);
        j.setState(state);
        j.setQueue(queue);
        j.setNode(node);

        em.persist(j);
        j.setInternalPosition(position);
        j.setParameters(jps);

        return j;
    }
View Full Code Here

TOP

Related Classes of com.enioka.jqm.jpamodel.JobInstance

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.