3
votes

I am trying to extract attachments using EmbeddedObjects, I am able to extract attachments but create em*tm temp files in system temp folder.

 EmbeddedObject embeddedObject=document.getAttachment(attachmentName);
 InputStream inputStream=embeddedObject.getInputStream();
 .....
 ......
 inputStream.close();
 embeddedObject..recycle();
 document..recycle();

After closing input Stream its not deleting temp file form system temp folder. Is it any thing wrong in my code or its setting issue with lotus notes.

Can you please help me in this?

Thanks for the help.

2
embeddedObject.recycle(); document.recycle(); Sorry for that, Its .recycle(); Its not ..recycle(); - Sarma
Hmmm... I ran into those temp files previously when I forgot to close the InputStream. In fact, I filed a report with IBM because at that time the documentation never mentioned the requirement to close the stream. Now it does. In fact, the doc now includes the exact language that I suggested to IBM. So this is quite mysterious. It would be a serious bug if this is a generally reproducible scenario. What version of Notes or Domino are you using? - Richard Schwartz
Thanks for the reply, I am using Lotus Notes 8. - Sarma
Is this an agent or stand-alone Java code? Single threaded, or multi-threaded? - Richard Schwartz
Its Stand-alone java program. Its single threaded. - Sarma

2 Answers

3
votes

This is a common issue, and it relates to the incorrect closure/recycle of objects (either missing or out of sequence). E0*TM files will be created while the objects are alive and cleaned up when recycled.

If they are correct then check to see if any Antivirus software running that is blocking deletion.

The following sample code I used to test this before works, so compare to yours.

  try { 

   System.out.println("Start"); 
   String path = "test.txt";    

   Session session = getSession();  
   AgentContext agentContext = session.getAgentContext();   

   System.out.println("Get DB");    
   Database db = session.getCurrentDatabase();  

   System.out.println("View + doc");    
   View vw = db.getView("main");    
   Document doc = vw.getFirstDocument();    

   System.out.println("Embedded object");   
   EmbeddedObject att = doc.getAttachment(path);    
   InputStream is = att.getInputStream();   
   ByteArrayOutputStream fos = new ByteArrayOutputStream(); 

   byte buffer[] = new byte[(int) att.getFileSize()];   
   int read;    
   do { 
    read = is.read(buffer, 0, buffer.length);   
    if (read > 0) { 
     fos.write(buffer, 0, read);    
    }   
   } while (read > -1); 

   fos.close(); 
   is.close();

   // recycle the domino variables  
   doc.recycle();   
   vw.recycle();    
   db.recycle();    
   att.recycle();   

  } catch (Exception e) {   
   e.printStackTrace(); 
  }
1
votes

My suggestion would be to first comment out all the code that you represented in your post as

.....
......

Does the temp file still get left behind? If so, it looks like it's a bug in the Notes back end classes for 8.x that needs to be reported to IBM.

If not, then something in the commented-out code is preventing the the close() call from succeeding. InputStream is an abstract class, so perhaps you are binding inputStream to another type of stream object that must be closed in order to prevent the file from staying open.