List of usage examples for javax.persistence EntityManager createNamedQuery
public Query createNamedQuery(String name);
Query
for executing a named query (in the Java Persistence query language or in native SQL). From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * {@inheritDoc}// w w w.ja v a2 s. c om * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#countByHost(java.lang.String, java.lang.String, * org.opencastproject.job.api.Job.Status) */ @Override public long countByHost(String serviceType, String host, Status status) throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query = em.createNamedQuery("Job.countByHost"); query.setParameter("status", status); query.setParameter("serviceType", serviceType); query.setParameter("host", host); Number countResult = (Number) query.getSingleResult(); return countResult.longValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * Gets jobs of all types that are in the {@value Status#QUEUED} state and are dispatchable. * //from w ww .j a v a 2 s. c o m * @param em * the entity manager * @return the list of jobs waiting for dispatch * @throws ServiceRegistryException * if there is a problem communicating with the jobs database */ @SuppressWarnings("unchecked") protected List<Job> getDispatchableJobs(EntityManager em) throws ServiceRegistryException { Query query = null; try { query = em.createNamedQuery("Job.dispatchable.status"); List<Status> statuses = new ArrayList<Job.Status>(); statuses.add(Status.QUEUED); statuses.add(Status.RESTART); query.setParameter("statuses", statuses); return query.getResultList(); } catch (Exception e) { throw new ServiceRegistryException(e); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * {@inheritDoc}/* www .jav a2 s .com*/ * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#countByOperation(java.lang.String, java.lang.String, * org.opencastproject.job.api.Job.Status) */ @Override public long countByOperation(String serviceType, String operation, Status status) throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query = em.createNamedQuery("Job.countByOperation"); query.setParameter("status", status); query.setParameter("serviceType", serviceType); query.setParameter("operation", operation); Number countResult = (Number) query.getSingleResult(); return countResult.longValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * {@inheritDoc}/*from w w w . j av a 2s . com*/ * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getChildJobs(long) */ @SuppressWarnings("unchecked") @Override public List<Job> getChildJobs(long id) throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query = em.createNamedQuery("Job.root.children"); query.setParameter("id", id); List<Job> jobs = query.getResultList(); if (jobs.size() == 0) { jobs = getChildren(em, id); Collections.sort(jobs, new Comparator<Job>() { @Override public int compare(Job job1, Job job2) { return job1.getDateCreated().compareTo(job2.getDateCreated()); } }); } for (Job job : jobs) { setJobUri(job); } return jobs; } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * Gets a map of hosts to the number of jobs currently loading that host * /* www . j a v a 2s .c om*/ * @param em * the entity manager * @param activeOnly * if true, the map will include only hosts that are online and have non-maintenance mode services * @return the map of hosts to job counts */ @SuppressWarnings("unchecked") protected Map<String, Integer> getHostLoads(EntityManager em, boolean activeOnly) { // Initialize the list of hosts List<ServiceRegistration> services = em.createNamedQuery("ServiceRegistration.getAll").getResultList(); Map<String, Integer> loadByHost = new HashMap<String, Integer>(); for (ServiceRegistration s : services) { if (!loadByHost.containsKey(s.getHost())) loadByHost.put(s.getHost(), 0); } // Find all jobs that are currently running on any given host Query q = em.createNamedQuery("ServiceRegistration.hostload"); // Accumulate the numbers for relevant job statuses per host for (Object result : q.getResultList()) { Object[] resultArray = (Object[]) result; ServiceRegistrationJpaImpl service = (ServiceRegistrationJpaImpl) resultArray[0]; // Workflow related jobs are not counting. Workflows are load balanced by the workflow service directly if (TYPE_WORKFLOW.equals(service.getServiceType())) continue; Job.Status status = (Status) resultArray[1]; int count = ((Number) resultArray[2]).intValue(); if (activeOnly && (service.isInMaintenanceMode() || !service.isOnline())) { continue; } // Only queued and running jobs are adding to the load, so every other status is discarded if (status == null || !JOB_STATUSES_INFLUENCING_LOAD_BALANCING.contains(status)) { count = 0; } // Add the service registration if (loadByHost.containsKey(service.getHost())) { Integer previousServiceLoad = loadByHost.get(service.getHost()); loadByHost.put(service.getHost(), previousServiceLoad + count); } else { loadByHost.put(service.getHost(), count); } } return loadByHost; }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * {@inheritDoc}/*from www.ja v a 2s . com*/ * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getServiceStatistics() */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<ServiceStatistics> getServiceStatistics() throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query = em.createNamedQuery("ServiceRegistration.statistics"); Map<ServiceRegistration, JaxbServiceStatistics> statsMap = new HashMap<ServiceRegistration, JaxbServiceStatistics>(); List queryResults = query.getResultList(); for (Object result : queryResults) { Object[] oa = (Object[]) result; ServiceRegistrationJpaImpl serviceRegistration = ((ServiceRegistrationJpaImpl) oa[0]); Status status = ((Status) oa[1]); Number count = (Number) oa[2]; Number meanQueueTime = (Number) oa[3]; Number meanRunTime = (Number) oa[4]; // The statistics query returns a cartesian product, so we need to iterate over them to build up the objects JaxbServiceStatistics stats = statsMap.get(serviceRegistration); if (stats == null) { stats = new JaxbServiceStatistics(serviceRegistration); statsMap.put(serviceRegistration, stats); } // the status will be null if there are no jobs at all associated with this service registration if (status != null) { switch (status) { case RUNNING: stats.setRunningJobs(count.intValue()); break; case QUEUED: case DISPATCHING: stats.setQueuedJobs(count.intValue()); break; case FINISHED: stats.setMeanRunTime(meanRunTime.longValue()); stats.setMeanQueueTime(meanQueueTime.longValue()); stats.setFinishedJobs(count.intValue()); break; default: break; } } } // Make sure we also include the services that have no processing history so far List<ServiceRegistration> services = em.createNamedQuery("ServiceRegistration.getAll").getResultList(); for (ServiceRegistration s : services) { if (!statsMap.containsKey(s)) statsMap.put(s, new JaxbServiceStatistics((ServiceRegistrationJpaImpl) s)); } List<ServiceStatistics> stats = new ArrayList<ServiceStatistics>(statsMap.values()); Collections.sort(stats, new Comparator<ServiceStatistics>() { @Override public int compare(ServiceStatistics o1, ServiceStatistics o2) { ServiceRegistration reg1 = o1.getServiceRegistration(); ServiceRegistration reg2 = o2.getServiceRegistration(); int typeComparison = reg1.getServiceType().compareTo(reg2.getServiceType()); return typeComparison == 0 ? reg1.getHost().compareTo(reg2.getHost()) : typeComparison; } }); return stats; } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * {@inheritDoc}//from w w w . ja v a 2 s . co m * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#count(java.lang.String, * org.opencastproject.job.api.Job.Status) */ @Override public long count(String serviceType, Status status) throws ServiceRegistryException { EntityManager em = null; try { em = emf.createEntityManager(); Query query; if (status == null) { query = em.createNamedQuery("Job.count.nullStatus"); } else { query = em.createNamedQuery("Job.count"); query.setParameter("status", status); } query.setParameter("serviceType", serviceType); Number countResult = (Number) query.getSingleResult(); return countResult.longValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * {@inheritDoc}/*from ww w .jav a 2s . c o m*/ * * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getJobs(java.lang.String, * org.opencastproject.job.api.Job.Status) */ @SuppressWarnings("unchecked") @Override public List<Job> getJobs(String type, Status status) throws ServiceRegistryException { Query query = null; EntityManager em = null; try { em = emf.createEntityManager(); if (type == null && status == null) { query = em.createNamedQuery("Job.all"); } else if (type == null) { query = em.createNamedQuery("Job.status"); query.setParameter("status", status); } else if (status == null) { query = em.createNamedQuery("Job.type"); query.setParameter("serviceType", type); } else { query = em.createNamedQuery("Job"); query.setParameter("status", status); query.setParameter("serviceType", type); } List<Job> jobs = query.getResultList(); for (Job job : jobs) { setJobUri(job); } return jobs; } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * Find all undispatchable jobs and set them to CANCELED. *///from ww w . j a v a 2 s.c om private void cleanUndispatchableJobs() { EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); Query query = em.createNamedQuery("Job.undispatchable.status"); List<Status> statuses = new ArrayList<Job.Status>(); statuses.add(Status.INSTANTIATED); statuses.add(Status.RUNNING); query.setParameter("statuses", statuses); @SuppressWarnings("unchecked") List<JobJpaImpl> undispatchableJobs = query.getResultList(); for (JobJpaImpl job : undispatchableJobs) { logger.info("Marking undispatchable job {} as canceled", job); job.setStatus(Status.CANCELED); em.merge(job); } tx.commit(); } catch (Exception e) { logger.error("Unable to clean undispatchable jobs! {}", e.getMessage()); if (tx != null && tx.isActive()) { tx.rollback(); } } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java
/** * Gets the failed jobs history for the given service registration * /*from w ww . jav a2 s.c o m*/ * @param serviceRegistration * @return the failed jobs history size * @throws IllegalArgumentException * if parameter is null * @throws ServiceRegistryException */ private int getHistorySize(JaxbServiceRegistration serviceRegistration) throws IllegalArgumentException, ServiceRegistryException { if (serviceRegistration == null) throw new IllegalArgumentException("serviceRegistration must not be null!"); Query query = null; EntityManager em = null; logger.debug("Try to get the number of jobs who failed on the service {}", serviceRegistration.toString()); try { em = emf.createEntityManager(); query = em.createNamedQuery("Job.count.history.failed"); query.setParameter("serviceType", serviceRegistration.getServiceType()); query.setParameter("host", serviceRegistration.getHost()); Number number = (Number) query.getSingleResult(); return number.intValue(); } catch (Exception e) { throw new ServiceRegistryException(e); } finally { if (em != null) em.close(); } }