Search code examples
grailsjfreechartcontrollersoutputstream

How to use multiple with one OutputStream


I need to show four charts on a grails page in a grid layout with positions 11, 12, 21 and 22. Each chart is build with a code similar to:

<img src="${createLink(controller:'paretoChart', action:'buildParetoChart11')}"/>

The code for the chart building action is:

    def buildParetoChart11 = {
        def PlotService p11 = PlotService.getInstance()

        def poList = paretoChartService.getParetoidPO()
        def listCounter = 0 

        def idPO = poList[listCounter]
        idPO.toString()
        def String idPOvalue = idPO

        def out = response.outputStream

        out = p11.paretoPlot(out, idPOvalue)
        response.setContentType("image/jpg")
        session["idPOList11"] = poList
}

The Java p11.paretoPlot(out, idPOvalue) returns a BufferedImage of the chart inside the OutputStream, but it only works for one chart. The other three charts vary on the the order on each all pour actions are called.

PlotService was written by me, yes. In this implementation, I'm passing the OutputStream out I got from response.outputStream and the String idPOvalue to the Java method. plotPareto's implementation is as follows:

public OutputStream paretoPlot(OutputStream out, String po) throws IOException {
    chart = buildParetoChart(po);// here the chart is actually built
    bufferedImage = chart.createBufferedImage(350, 275);
    ChartUtilities.writeBufferedImageAsJPEG(out, bufferedImage);
}

So, is there a way to make sure one action is completed before firing up the next one?

Thanks in advance!


Solution

  • each request to get an image is handled asynchronously by the browser. Each request runs in its own thread on the server. With img tags, the browser controls the GET requests to get the images, so I don't think you can easily guarantee the order, and nor should you have to.

    Are you seeing any errors?

    I would look at the firebug or equivalent output to see if the browser is getting an error. for any of the image requests.

    I would also try attaching a debugger to your server.

    Did you write the PlotService? You need to make sure it is thread safe.

    Also, I dont see you reading any params, is there a separate action for each image?