xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexios Giotis <alex.gio...@gmail.com>
Subject Re: Interrupt/cancel/abort a render
Date Tue, 20 Sep 2011 16:43:49 GMT
Hi Bernard,

As far as I know there is no build-in support in FOP to cancel the rendering. In Java, the
only way to achieve it (except the deprecated stop()), is to get the thread that is executing
it and call the interrupt() method. The problem is that there is no guarantee that the thread
will interrupt it's execution and the behavior really depends on the code it is running. Typically
if the thread is making calculations (and is not blocking on I/O requests or is not executing
a method that throws InterruptedException, ....), then it will not respond to your interrupt.
In this case, the only way to interrupt is if you execute code like 
if(Thread.interrupted()) // clears the interrupted status
  throw SomeException();

A good reading on cancellation and shutdown is Chapter 7 of "Java Concurrency in Practice"
by  Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea.

In practice, you typically can't change code in existing libraries. *IF* you find that FOP
is not responding to the interrupt methods, then a workaround is to pass your own classes
that will (1) check for the interrupted status or (2) check for your own flag and throw an
exception, for example (2nd case):

public class InterruptAwareOutputStream extends BufferedOutputStream {
private boolean volatile shouldStop = false;
public void interruptProcessing() {
   shouldStop = true;
}

public void write(.....) {
    if(shouldStop)
       throw MyException();
   else
    super.write(....);
}
// other methods that need to be overwritten
}

For the XML to PDF example you pointed, you will also need to subclass like this the ContentHandler
given to the "new SAXResult".

If the thread does not respond to the interrupt, it's always helpful to know what this is
executing. I typically print the stacktrace with code like:

executingThread.interrupt();
		
StackTraceElement ste[] = executingThread.getStackTrace();
StringBuilder sb = new StringBuilder(LINE_SEP);
for (StackTraceElement s : ste) {
  String stacktrace = s.toString();
  sb.append(LINE_SEP).append("\t\t").append(stacktrace);
}

String stackTrace = sb.toString();		
logger.info("Interrupted ["+info.task.getId()+"]" + " executed by thread ["
				+ executingThread.getName() + "] with stacktrace:" + stackTrace);



Hope this helps,
Alexios Giotis


On Sep 18, 2011, at 4:42 PM, Bernard Giannetti wrote:

> Hi,
> 
> I'm wondering if it's possible to interrupt/cancel/abort a render once it has been started.
 I'm embedding FOP into a Java desktop application in a similar way which is described here:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2PDF.java?view=markup.
 
> 
> I've looked at the API and nothing is immediately obvious.  I found this old forum post
(http://www.mail-archive.com/fop-user@xml.apache.org/msg05146.html) which didn't help.
> 
> I am running the rendering process in a thread but not sure how I'd go about cleanly/gracefully
aborting a render.
> 
> 
> Thanks,
> 
> Bernard.


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


Mime
View raw message