简体   繁体   中英

How to make VSF inbound endpoint fail according to some conditions

There is my case to waiting for TXT files and then calling a REST api according to the file. Hence, I create a proxy service such as:

<proxy name="vsf-to-rest" startOnLoad="true" transports="vfs" xmlns="http://ws.apache.org/ns/synapse">
<target>
    <inSequence>
        <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
        <property expression="$trp:FILE_NAME" name="filename" scope="default" type="STRING"/>
        <script language="js"><![CDATA[var filename = mc.getProperty('filename');
            var s = filename.split('_');
            mc.setProperty('a', s[0]);
            mc.setProperty('b', s[1]);]]></script>
        <payloadFactory media-type="json">
            <format>{"a":"$1", "b":"$2"}</format>
            <args>
                <arg evaluator="xml" expression="get-property('a')" literal="true"/>
                <arg evaluator="xml" expression="get-property('b')" literal="true"/>
            </args>
        </payloadFactory>
        <call>
            <endpoint key="mock-task-endpoint"/>
        </call>
        <drop/>
    </inSequence>
    <outSequence>
    </outSequence>
    <faultSequence/>
</target>
<parameter name="transport.PollInterval">5</parameter>
<parameter name="transport.vfs.FileURI">vfs:file:////Users/me/vsf/in</parameter>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.vfs.MoveAfterFailure">vfs:file:////Users/me/vsf/failed</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
<parameter name="transport.vfs.FileNamePattern">.*\.txt</parameter>
<parameter name="transport.vfs.MoveAfterProcess">vfs:file:////Users/me/vsf/proceeded</parameter>
</proxy>

The problem is how to make the sequence fail according to the HTTP status of REST api (viz. 4xx and 5xx). I tried to use switch and makefault mediation, but all input files are moved to proceeded folder and there are no files in failed even if REST api returns 4xx or 5xx.

This is the expected behavior in the EI. The MoveAfterFailure occurs only in a situation where an error occurs while reading the file or processing the file. If any error occurs in the mediation such as an endpoint call failure it will not be considered a failure for VFS.

If you want to move the file from the processed folder to the failed folder based on the endpoint call, you can implement a file connector [1] within a switch mediator (to evaluate the endpoint response code) after the endpoint call to move the file from processed folder to failed folder (I have not tested this locally).

[1]-https://docs.wso2.com/display/ESBCONNECTORS/Working+with+the+File+Connector#WorkingwiththeFileConnector-move

Move after failure only triggers if axis2 fault/ IO error thrown in the mediation sequence.

Hence, you can do the following to achieve your requirement.

Move the file based on the status code

<filter source="$ctx:RESPONSE_STATUS" regex="500">
    <then>
      <send>
        <endpoint>
          <address uri="vfs:file:////Users/me/vsf/failed"/>
        </endpoint>
      </send>
    </then>
</filter>

For actual faulty messages

  1. Define a sequence and reference the fault sequence in the proxy as follows.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="errorSequence">
  <log level="custom">
    <property name="MESSAGE" value="ERROR OCCURRED"/>
  </log>
  <property name="ERROR_STATUS" expression="$ctx:ERROR_CODE"/>
  <!-- log some data -->
  <log level="custom">
    <property name="FILE_NAME" expression="$ctx:FILE"/>
    <property name="FILE_CONTENT" expression="$ctx:CONTENT"/>
    <property name="STATUS" expression="$ctx:ERROR_STATUS"/>
  </log>
  <property name="OUT_ONLY" value="true"/>
      <send>
        <endpoint>
          <address uri="vfs:file:////Users/me/vsf/failed"/>
        </endpoint>
      </send>
    </then>

</sequence>
  1. Reference the error sequence in the proxy.

Can you give a try with the above approach?

Thanks, Dileepa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM