|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.imgscalr.AsyncScalr
public class AsyncScalr
Class used to provide the asynchronous versions of all the methods defined in
Scalr for the purpose of efficiently handling large amounts of image
operations via a select number of processing threads asynchronously.
Scalr
class in new Callable instances that are submitted to an internal
ExecutorService for execution at a later date. A Future is
returned to the caller representing the task that is either currently
performing the scale operation or will at a future date depending on where it
is in the ExecutorService's queue. Future.get() or
Future.get(long, TimeUnit) can be used to block on the
Future, waiting for the scale operation to complete and return
the resultant BufferedImage to the caller.
This design provides the following features:
THREAD_COUNT) so as best to optimize the host system (e.g. 1 scale
thread per core).get() or get(long, TimeUnit) immediately on
the returned Future from any of the methods below.BufferedImage and stored in the JVM Heap space (decoded image
instances are always larger than the source images on-disk). For larger
images, that can use up quite a bit of memory. You will need to benchmark
your particular use-cases on your hardware to get an idea of where the sweet
spot is for this; if you are operating within tight memory bounds, you may
want to limit simultaneous scaling operations to 1 or 2 regardless of the
number of cores just to avoid having too many BufferedImage instances
in JVM Heap space at the same time.
These are rough metrics and behaviors to give you an idea of how best to tune
this class for your deployment, but nothing can replacement writing a small
Java class that scales a handful of images in a number of different ways and
testing that directly on your deployment hardware.
ExecutorService utilized by this class won't be initialized until
one of the operation methods are called, at which point the
service will be instantiated for the first time and operation
queued up.
More specifically, if you have no need for asynchronous image processing
offered by this class, you don't need to worry about wasted resources or
hanging/idle threads as they will never be created if you never use this
class.
Threads created by the internal
ThreadPoolExecutor do not run in daemon mode; which
means they will block the host VM from exiting until they are explicitly shut
down in a client application; in a server application the container will shut
down the pool forcibly.
If you have used the AsyncScalr class and are trying to shut down a
client application, you will need to call getService() then
ExecutorService.shutdown() or ExecutorService.shutdownNow()
to have the threads terminated; you may also want to look at the
ExecutorService.awaitTermination(long, TimeUnit) method if you'd like
to more closely monitor the shutting down process (and finalization of
pending scale operations).
shutdown on the underlying service
utilized by this class, subsequent calls to any of the operations this class
provides will invoke the internal checkService() method which will
replace the terminated underlying ExecutorService with a new one via
the createService() method.
ExecutorService or
AsyncScalr.DefaultThreadFactory used under the covers, this can be done by overriding
the createService() method which is invoked by this class anytime a
new ExecutorService is needed.
By default the createService() method delegates to the
createService(ThreadFactory) method with a new instance of
AsyncScalr.DefaultThreadFactory. Either of these methods can be overridden and
customized easily if desired.
TIP: A common customization to this class is to make the
Threads generated by the underlying factory more server-friendly, in
which case the caller would want to use an instance of the
AsyncScalr.ServerThreadFactory when creating the new ExecutorService.
This can be done in one line by overriding createService() and
returning the result of:
return createService(new ServerThreadFactory());
By default this class uses an ThreadPoolExecutor internally to handle
execution of queued image operations. If a different type of
ExecutorService is desired, again, simply overriding the
createService() method of choice is the right way to do that.
| Nested Class Summary | |
|---|---|
protected static class |
AsyncScalr.DefaultThreadFactory
Default ThreadFactory used by the internal
ExecutorService to creates execution Threads for image
scaling. |
protected static class |
AsyncScalr.ServerThreadFactory
An extension of the AsyncScalr.DefaultThreadFactory class that makes two
changes to the execution Threads it generations:
Threads are set to be daemon threads instead of user threads.
Threads execute with a priority of Thread.MIN_PRIORITY to
make them more compatible with server environment deployments.
This class is provided as a convenience for subclasses to use if they
want this (common) customization to the Threads used internally
by AsyncScalr to process images, but don't want to have to write
the implementation. |
| Field Summary | |
|---|---|
protected static ExecutorService |
service
|
static int |
THREAD_COUNT
Number of threads the internal ExecutorService will use to
simultaneously execute scale requests. |
static String |
THREAD_COUNT_PROPERTY_NAME
System property name used to set the number of threads the default underlying ExecutorService will use to process async image
operations. |
| Constructor Summary | |
|---|---|
AsyncScalr()
|
|
| Method Summary | |
|---|---|
static Future<BufferedImage> |
apply(BufferedImage src,
BufferedImageOp... ops)
|
protected static void |
checkService()
Used to verify that the underlying service points at an
active ExecutorService instance that can be used by this class. |
protected static ExecutorService |
createService()
|
protected static ExecutorService |
createService(ThreadFactory factory)
|
static Future<BufferedImage> |
crop(BufferedImage src,
int width,
int height,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
crop(BufferedImage src,
int x,
int y,
int width,
int height,
BufferedImageOp... ops)
|
static ExecutorService |
getService()
Used to get access to the internal ExecutorService used by this
class to process scale operations. |
static Future<BufferedImage> |
pad(BufferedImage src,
int padding,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
pad(BufferedImage src,
int padding,
Color color,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
rotate(BufferedImage src,
Scalr.Rotation rotation,
BufferedImageOp... ops)
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String THREAD_COUNT_PROPERTY_NAME
ExecutorService will use to process async image
operations.
Value is "imgscalr.async.threadCount".
public static final int THREAD_COUNT
ExecutorService will use to
simultaneously execute scale requests.
This value can be changed by setting the
imgscalr.async.threadCount system property (see
THREAD_COUNT_PROPERTY_NAME) to a valid integer value > 0.
Default value is 2.
protected static ExecutorService service
| Constructor Detail |
|---|
public AsyncScalr()
| Method Detail |
|---|
public static ExecutorService getService()
ExecutorService used by this
class to process scale operations.
NOTE: You will need to explicitly shutdown any service
currently set on this class before the host JVM exits.
You can call ExecutorService.shutdown() to wait for all scaling
operations to complete first or call
ExecutorService.shutdownNow() to kill any in-process operations
and purge all pending operations before exiting.
Additionally you can use
ExecutorService.awaitTermination(long, TimeUnit) after issuing a
shutdown command to try and wait until the service has finished all
tasks.
ExecutorService used by this class to process
scale operations.
public static Future<BufferedImage> apply(BufferedImage src,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.apply(BufferedImage, BufferedImageOp...)
public static Future<BufferedImage> crop(BufferedImage src,
int width,
int height,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.crop(BufferedImage, int, int, BufferedImageOp...)
public static Future<BufferedImage> crop(BufferedImage src,
int x,
int y,
int width,
int height,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.crop(BufferedImage, int, int, int, int, BufferedImageOp...)
public static Future<BufferedImage> pad(BufferedImage src,
int padding,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.pad(BufferedImage, int, BufferedImageOp...)
public static Future<BufferedImage> pad(BufferedImage src,
int padding,
Color color,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.pad(BufferedImage, int, Color, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
int targetSize,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.resize(BufferedImage, int, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetSize,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.resize(BufferedImage, Method, int, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.resize(BufferedImage, Mode, int, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.resize(BufferedImage, Method, Mode, int, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.resize(BufferedImage, int, int, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Scalr.resize(BufferedImage, Method, int, int, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.resize(BufferedImage, Mode, int, int, BufferedImageOp...)
public static Future<BufferedImage> resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.resize(BufferedImage, Method, Mode, int, int,
BufferedImageOp...)
public static Future<BufferedImage> rotate(BufferedImage src,
Scalr.Rotation rotation,
BufferedImageOp... ops)
throws IllegalArgumentException,
ImagingOpException
IllegalArgumentException
ImagingOpExceptionScalr.rotate(BufferedImage, Rotation, BufferedImageOp...)protected static ExecutorService createService()
protected static ExecutorService createService(ThreadFactory factory)
throws IllegalArgumentException
IllegalArgumentExceptionprotected static void checkService()
service points at an
active ExecutorService instance that can be used by this class.
If service is null, has been shutdown or
terminated then this method will replace it with a new
ExecutorService by calling the createService() method
and assigning the returned value to service.
Any subclass that wants to customize the ExecutorService or
AsyncScalr.DefaultThreadFactory used internally by this class should override the
createService().
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||