1
package com.highcharts.export.pool;
3
import com.highcharts.export.util.TempDir;
5
import java.io.IOException;
6
import java.nio.file.Files;
7
import java.nio.file.Path;
8
import java.nio.file.Paths;
9
import java.nio.file.attribute.BasicFileAttributes;
10
import java.util.Collection;
11
import java.util.Date;
12
import java.util.Queue;
13
import java.util.concurrent.atomic.AtomicInteger;
14
import org.apache.commons.io.FileUtils;
15
import org.apache.commons.io.filefilter.IOFileFilter;
17
import org.apache.log4j.Logger;
18
import org.springframework.scheduling.annotation.Scheduled;
20
public abstract class AbstractPool<T> implements ObjectPool<T> {
22
final ObjectFactory<T> objectFactory;
24
final AtomicInteger poolSize = new AtomicInteger(0);
27
final long retentionTime;
28
protected static Logger logger = Logger.getLogger("pool");
30
public AbstractPool(ObjectFactory<T> objectFactory, int number, int maxWait, Long retentionTime) throws PoolException {
31
this.objectFactory = objectFactory;
32
this.capacity = number;
33
this.maxWait = maxWait;
34
this.retentionTime = retentionTime;
38
public void createObject() {
39
T object = objectFactory.create();
41
poolSize.getAndIncrement();
45
public void destroyObject(T object) {
46
objectFactory.destroy(object);
50
@Scheduled(initialDelay = 10000, fixedRate = 60000)
51
public void poolCleaner() throws InterruptedException, PoolException {
53
logger.debug("HC: queue size: " + queue.size() + " poolSize " + poolSize.get());
55
int size = poolSize.get();
56
// remove invalid objects
57
for (int i = 0; i < size; i++) {
58
T object = borrowObject();
60
logger.debug("HC: object is null");
63
logger.debug("HC: validating " + object.toString());
64
if (!objectFactory.validate(object)) {
65
logger.debug("HC: destroying " + object.toString());
66
destroyObject(object);
68
returnObject(object, false);
73
int number = poolSize.get() - capacity;
74
logger.debug("in cleanpool, the surplus or shortage is: " + number);
76
int iterations = Math.abs(number);
77
for (int i = 0; i < iterations; i++) {
81
T object = borrowObject();
82
this.destroyObject(object);
89
@Scheduled(initialDelay = 15000, fixedRate = 60000)
90
public void tempDirCleaner() {
91
IOFileFilter filter = new IOFileFilter() {
94
public boolean accept(File file) {
96
Long now = new Date().getTime();
97
Path path = Paths.get(file.getAbsolutePath());
98
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
99
Long inBetween = now - attrs.lastAccessTime().toMillis();
101
if (inBetween > retentionTime) {
105
} catch (IOException ioex) {
106
logger.error("Error: while selection files for deletion: " + ioex.getMessage());
112
public boolean accept(File file, String string) {
113
throw new UnsupportedOperationException("Not supported yet.");
117
Collection<File> oldFiles = FileUtils.listFiles(TempDir.outputDir.toFile(),filter, null);
118
for (File file : oldFiles) {
124
/*Getter and Setters*/
125
public int getMaxWait() {
129
public void setMaxWait(int maxWait) {
130
this.maxWait = maxWait;