Unfortunately that doesn't seem to work.  I see no evidence
that my code is actually being called, i.e. there is no log
output and the file is not deleted, but everything works
without errors like it did before I added this code.
Does CXF really call this method?  It seems possible
that it could be getting the streams directly from the
DataHandler.  I'm using CXF 2.1.0, JBoss 4.2.3, JDK 1.5.

This is what I am implemented:

public DataHandler getObjectsAsAttachment(...) throws WSException {
    try {
        final File tempFile = ...;
        ...
        return new DataHandler(new FileDataSource(tempFile)) {
            @Override
            public void writeTo(OutputStream out) throws IOException {
                super.writeTo(out);
                log.info("########### " + tempFile + " ###########");
                tempFile.delete();
            }
        };
    } catch (Exception e) {
        log.error("Exception thrown in getObjectsAsAttachment", e);
        throw new WSException(e);
    }
}


> -----Original Message-----
> From: Daniel Kulp
> Sent: Tuesday, December 02, 2008 2:38 PM
> 
> In general, in this type of case, I recommend doing something like:
> (pseudo
> code)
> 
>  return new DataHandler(new FileDataSource(tempFile)) {
>     public void writeTo(OutputStream out)  {
>         super.writeTo(out);
>         tempFile.delete();
>     }
> };
> 
> or similar.   Basically, when the datahandler is done being written
> out,
> delete the file.
> 
> Dan
> 
> 
> On Tuesday 02 December 2008 1:14:40 pm Daniel Lipofsky wrote:
> > I have a web-service that is returning a stream like this:
> >
> >   <xsd:element name="objects" type="xsd:base64Binary"
> >      xmime:expectedContentTypes="application/octet-stream"/>
> >
> > My implementation method works by creating a
> > temporary file and the returning like
> >
> >   return new DataHandler(new FileDataSource(tempFile));
> >
> > My understanding is that this is asynchronous.
> > So how can I trigger a deletion of this file
> > when the streaming is done?
> >
> > I am already calling tempFile.deleteOnExit() but it
> > could be months or years before the JVM actually exits,
> > and I want to delete it sooner.
> >
> > Thanks,
> > Dan

Reply via email to