001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.oozie.executor.jpa; 020 021import java.sql.Timestamp; 022import java.util.List; 023 024import javax.persistence.EntityManager; 025import javax.persistence.Query; 026 027import org.apache.oozie.ErrorCode; 028 029/** 030 * Load the list of completed BundleJob for purge ready. 031 */ 032public class BundleJobsGetForPurgeJPAExecutor implements JPAExecutor<List<String>> { 033 034 private static final long DAY_IN_MS = 24 * 60 * 60 * 1000; 035 private long olderThanDays; 036 private int limit; 037 private int offset; 038 039 public BundleJobsGetForPurgeJPAExecutor(long olderThanDays, int limit) { 040 this(olderThanDays, 0, limit); 041 } 042 043 public BundleJobsGetForPurgeJPAExecutor(long olderThanDays, int offset, int limit) { 044 this.olderThanDays = olderThanDays; 045 this.offset = offset; 046 this.limit = limit; 047 } 048 049 /* (non-Javadoc) 050 * @see org.apache.oozie.executor.jpa.JPAExecutor#getName() 051 */ 052 @Override 053 public String getName() { 054 return "BundleJobsGetForPurgeJPAExecutor"; 055 } 056 057 /* (non-Javadoc) 058 * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager) 059 */ 060 @Override 061 @SuppressWarnings("unchecked") 062 public List<String> execute(EntityManager em) throws JPAExecutorException { 063 List<String> bundleJobs = null; 064 try { 065 Timestamp lastModTm = new Timestamp(System.currentTimeMillis() - (olderThanDays * DAY_IN_MS)); 066 Query jobQ = em.createNamedQuery("GET_COMPLETED_BUNDLE_JOBS_OLDER_THAN"); 067 jobQ.setParameter("lastModTime", lastModTm); 068 jobQ.setMaxResults(limit); 069 jobQ.setFirstResult(offset); 070 bundleJobs = jobQ.getResultList(); 071 } 072 catch (Exception e) { 073 throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e); 074 } 075 return bundleJobs; 076 } 077 078}