« Return to Thread: How to send PDF FileStream directly as response???

Re: How to send PDF FileStream directly as response???

by Andrew Perepelytsya :: Rate this Message:

Reply to Author | View in Thread

Unless PDF.getWriter() factory method is not thead-safe, you're fine (guard it otherwise). I see no other concurrency concerns. Thus, it's only the client terminating the connection unexpectedly that is a problem. Actually, not a problem, any server will print the same error when a client requested data, but then disappeared without properly closing the tcp session. The server has data for the client, but nowhere to send it.

HTH,
Andrew

On Jul 2, 2009 6:27 AM, "pocketom" <birnexxx@...> wrote:


Hi, and thank you for your replies.

Here is the demo source code:




Service Interface:

package com.mycompany.pdfservice;

import java.io.IOException;

import com.lowagie.text.DocumentException;

public interface OnDemandFileService {

       public byte[] serve2File(String request) throws DocumentException,
IOException;
}


Service Component

package com.mycompany.pdfservice;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.mwgbiotechag.pdfengine.Graph2D;
import com.mwgbiotechag.pdfengine.Point2D;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

public class PDFServiceComponent implements OnDemandFileService
{
       @Override
       public byte[] serve2File(String request) throws DocumentException,
IOException
       {
               System.out.println("PDFService:Request="+request);
               System.out.println("PDFService:\tNew PDF Document Requested");

               //benchmark
               long start = System.currentTimeMillis();

               //pages
               int numberpages = 100;

               ByteOutputStream bops = new ByteOutputStream();
               Document document = new Document();
               PdfWriter writer = PdfWriter.getInstance(document, bops);
               document.open();

               for (int i = 1; i <= numberpages; i++)
               {
                               document.add(new Chunk("Hello PDF Service - Page "+i));
                               document.newPage();
               }
               document.close();


               long end = System.currentTimeMillis();
               long time= end-start;

               System.out.println("PDFService:\tPDF Generation finished");
               System.out.println("PDFService:\tOverall generation time: "+time+"
ms+\t("+((float)time/numberpages)+"ms/page)");
               System.out.println("\n");

               return bops.getBytes();
       }



Here the Mule conf.xml:


<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:http="http://www.mulesource.org/schema/mule/http/2.2"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.mulesource.org/schema/mule/core/2.2
http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
      http://www.mulesource.org/schema/mule/http/2.2
http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd">


   <description>
   </description>


   <http:connector name="HttpConnector" enableCookies="true"
keepAlive="true"/>
   <model name="pdfonthefly">
        <service name="pdftest">

<inbound> <http:inbound-endpoint address="http://localhost:9090/pdf"

method="GET" synchronous="true" contentType="attachment/pdf"  />
           </inbound>
           <component
class="com.mycompany.pdfservice.PDFServiceComponent"/>
               </service>
   </model>
</mule>

--
View this message in context: http://www.nabble.com/How-to-send-PDF-FileStream-directly-as-response----tp24256692p24304324.html

Sent from the Mule - User mailing list archive at Nabble.com. ------------------------------------...

 « Return to Thread: How to send PDF FileStream directly as response???