Skip to content

Commit

Permalink
JobManager: add hibernate clean-up, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCharlier committed Feb 26, 2017
1 parent f938927 commit b850d0a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
import org.mapfish.print.servlet.job.PrintJobResult;
import org.mapfish.print.servlet.job.PrintJobStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
*
Expand All @@ -18,30 +28,54 @@
*/
@Transactional
public class HibernateJobQueue implements JobQueue {


private static final int DEFAULT_TIME_TO_KEEP_AFTER_ACCESS = 30; /* minutes */

private static final long DEFAULT_CLEAN_UP_INTERVAL = 300; /* seconds */

@Autowired
private PrintJobDao dao;

@Autowired
private PlatformTransactionManager txManager;

private ScheduledExecutorService cleanUpTimer;

/**
* The interval at which old records are deleted (in seconds).
*/
private long cleanupInterval = DEFAULT_CLEAN_UP_INTERVAL;

/**
* The minimum time to keep records after last access.
*/
private int timeToKeepAfterAccessInMinutes = DEFAULT_TIME_TO_KEEP_AFTER_ACCESS;

public final void setTimeToKeepAfterAccessInMinutes(final int timeToKeepAfterAccessInMinutes) {
this.timeToKeepAfterAccessInMinutes = timeToKeepAfterAccessInMinutes;
}

@Override
public final long getTimeToKeepAfterAccessInMillis() {
return -1;
return TimeUnit.MINUTES.toMillis(this.timeToKeepAfterAccessInMinutes);
}

@Override
public final int getLastPrintCount() {
return this.dao.count(PrintJobStatus.Status.FINISHED, PrintJobStatus.Status.CANCELLED, PrintJobStatus.Status.ERROR);
return this.dao.count(PrintJobStatus.Status.FINISHED, PrintJobStatus.Status.CANCELLED,
PrintJobStatus.Status.ERROR);
}

@Override
public final int getWaitingJobsCount() {
return this.dao.count(PrintJobStatus.Status.WAITING, PrintJobStatus.Status.RUNNING);
}

@Override
public final int getNumberOfRequestsMade() {
return this.dao.count();
}

@Override
public final long getAverageTimeSpentPrinting() {
return this.dao.getTotalTimeSpentPrinting() / Math.max(1, getLastPrintCount());
Expand Down Expand Up @@ -72,9 +106,9 @@ public final PrintJobStatus get(final String referenceId, final boolean external
public final synchronized void add(final PrintJobEntry jobEntry) {
this.dao.save(new PrintJobStatusExtImpl(jobEntry, getNumberOfRequestsMade()));
}

@Override
public final synchronized void cancel(final String referenceId, final String message, final boolean forceFinal)
public final synchronized void cancel(final String referenceId, final String message, final boolean forceFinal)
throws NoSuchReferenceException {
PrintJobStatusExtImpl record = this.dao.get(referenceId, true);
if (record == null) {
Expand All @@ -90,9 +124,8 @@ public final synchronized void cancel(final String referenceId, final String mes
this.dao.save(record);
}


@Override
public final synchronized void fail(final String referenceId, final String message)
public final synchronized void fail(final String referenceId, final String message)
throws NoSuchReferenceException {
PrintJobStatusExtImpl record = this.dao.get(referenceId, true);
if (record == null) {
Expand All @@ -116,13 +149,14 @@ public final synchronized void start(final String referenceId) throws NoSuchRefe
}

@Override
public final synchronized void done(final String referenceId, final PrintJobResult result) throws NoSuchReferenceException {
public final synchronized void done(final String referenceId, final PrintJobResult result)
throws NoSuchReferenceException {
PrintJobStatusExtImpl record = this.dao.get(referenceId, true);
if (record == null) {
throw new NoSuchReferenceException(referenceId);
}
record.setStatus(record.getStatus() == PrintJobStatus.Status.CANCELING ? PrintJobStatus.Status.CANCELLED :
PrintJobStatus.Status.FINISHED);
record.setStatus(record.getStatus() == PrintJobStatus.Status.CANCELING ? PrintJobStatus.Status.CANCELLED
: PrintJobStatus.Status.FINISHED);
record.setResult(result);
record.setCompletionTime(System.currentTimeMillis());
this.dao.save(record);
Expand All @@ -149,5 +183,44 @@ public final synchronized List<? extends PrintJobStatus> start(final int number)
public final List<? extends PrintJobStatus> toCancel() {
return this.dao.get(PrintJobStatus.Status.CANCELING);
}


/**
* Called by spring on initialization.
*/
@PostConstruct
public final void init() {
this.cleanUpTimer = Executors.newScheduledThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(final Runnable timerTask) {
final Thread thread = new Thread(timerTask, "Clean up old job records");
thread.setDaemon(true);
return thread;
}
});
this.cleanUpTimer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
cleanup();
}
}, this.cleanupInterval, this.cleanupInterval, TimeUnit.SECONDS);
}

/**
* Called by spring when application context is being destroyed.
*/
@PreDestroy
public final void shutdown() {
this.cleanUpTimer.shutdownNow();
}

private void cleanup() {
TransactionTemplate tmpl = new TransactionTemplate(this.txManager);
tmpl.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(final TransactionStatus status) {
HibernateJobQueue.this.dao.deleteOld(System.currentTimeMillis() - getTimeToKeepAfterAccessInMillis());
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ public final void cancelOld(final long starttimeThreshold, final long checkTimeT
query.executeUpdate();
}

/**
* Delete old jobs.
*
* @param checkTimeThreshold
* threshold for last check time
*/
public final void deleteOld(final long checkTimeThreshold) {
Query query = getSession()
.createQuery("delete from PrintJobStatusExtImpl " + "where lastCheckTime < :checktimethreshold)");
query.setParameter("checktimethreshold", checkTimeThreshold);
query.executeUpdate();
}

/**
* Poll for the next N waiting jobs in line.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<property name="timeToKeepAfterAccessInMinutes" value="30" />
</bean>

<bean id="jobQueue" class="org.mapfish.print.servlet.job.impl.RegistryJobQueue"></bean>
<bean id="jobQueue" class="org.mapfish.print.servlet.job.impl.RegistryJobQueue"></bean>
<bean id="jobManager" class="org.mapfish.print.servlet.job.impl.ThreadPoolJobManager">
<property name="maxNumberOfRunningPrintJobs" value="${maxNumberOfRunningPrintJobs}" />
<property name="maxNumberOfWaitingJobs" value="5000" />
Expand Down

0 comments on commit b850d0a

Please sign in to comment.