kleht8
kleht8

Reputation: 337

WSO2 ESB blocking vfs possible?

I'm building WSO2 ESB 4.8.1 to receive incoming binary message and store it to the disk with VFS. I'm able to store the message to file with Send mediator but since this is non-blocking mediator the rest of the sequences will continue simultanously. This causes problem with large files where storing the file takes more time.

Question: Is it possible to store file with Callout mediator or other blocking mechanism so that ESB will continue processing the sequence after VFS has stored the file completely ? I've tried callout mediator but it does not support vfs endpoint url e.g "vfs:file:///tmp"

Thanks for any tips.

Upvotes: 0

Views: 206

Answers (1)

kleht8
kleht8

Reputation: 337

Solved this by creating a custom Class mediator which takes the data and streams it to file. Sequence will be continued after class mediator has finished the job.

Here the class mediator code:

public class FileSaveMediator extends AbstractMediator {

    boolean traceOn = false;
    boolean traceOrDebugOn = false;


    public boolean mediate(MessageContext context) {

        traceOn = isTraceOn(context);
        traceOrDebugOn = isTraceOrDebugOn(traceOn);

        traceOrDebug(traceOn, "Start : FileSaveMediator");

        // Get property "fileuri" from synapse context
        String fileuri = (String)context.getProperty("fileuri");

        SOAPBody body = context.getEnvelope().getBody();

           OMText binaryNode = (OMText) (body.getFirstElement()).getFirstOMChild();
           DataHandler actualDH;
           actualDH = (DataHandler) binaryNode.getDataHandler();

           FileOutputStream fos = null;
           try {
               fos = new FileOutputStream(fileuri);
               actualDH.writeTo(fos);          

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

           // Clear Body after saving file by setting dummy tags.

           try {
            body.setFirstChild(AXIOMUtil.stringToOM("<p></p>"));
        } catch (XMLStreamException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            // Finally make sure that fileoutstream is closed
            if (fos!=null)
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

        traceOrDebug(traceOn, "End : FileSaveMediator");

        return true;

    }

Upvotes: 1

Related Questions