Mule: Exception Handling in File transfer Mule flow - mule

I have a simplistic mule configuration that takes in HTTP Query parameters, builds a URL & downloads a File from the URL. It does nothing much & does not use any kind of File (or FTP/SFTP) protocols. What kind of Exception handling mechanism do I need to think about for this?
Here is the snippet of code:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8048" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="${sync.host}" port="${sync.port}" doc:name="HTTP Request Configuration"/>
<file:connector name="output" doc:name="File"/>
<flow name="syncFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/og" allowedMethods="GET" doc:name="HTTP"/>
<set-variable variableName="year" value="#[message.inboundProperties.'http.query.params'.year]" doc:name="Variable"/>
<set-variable variableName="month" value="#[message.inboundProperties.'http.query.params'.month]" doc:name="Variable"/>
<http:request config-ref="HTTP_Request_Configuration" path= "/year/{year}/month/{month}/monthly.csv" method="GET" doc:name="HTTP">
<http:request-builder>
<http:uri-param paramName="year" value="#[flowVars.year]"/>
<http:uri-param paramName="month" value="#[flowVars.month]"/>
</http:request-builder>
</http:request>
Also this is the end of the flow & I just simply dump the file I receive , any suggestions on elegant ways of handling this part of the code?

What kind of Exception handling mechanism do I need to think about for
this?
I would use a choice exception strategy to catch, at a minimum, exceptions related to file permissions, disk space, and an already existing file.
any suggestions on elegant ways of handling this part of the code?
I would probably remove the variable declarations if you're not going to be re-using them and just put the values directly in the request builder. You can also collapse those into one "Message Properties" transformer using the invocation scope.

What kind of Exception handling mechanism do I need to think about for
this?
You can have a Rollback Exception Strategy in case the service you are sending the request to is down or fails to receive your request properly.

Related

How to use LoggingInteceptor in Mule

I'm trying to use LoggingInterceptor provided by Mule ESB. I assume the usage of this interceptor should be pretty straight forward, but somehow it's not working with the code that I'm using, is there something that I'm missing here?
<flow name="loggingInterceptor-demo">
<logging-interceptor doc:name="Log interceptor" />
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<logging-interceptor doc:name="Log interceptor"/>
</flow>
As per the documentation LoggingInterceptor is a simple interceptor that logs a message before and after the event processing. but it's not logging anything in the console. What am I missing here.
Runtime version: 3.8.2
I can't find that definition you quoted from the docs--so much stuff. If you haven't found docs about the Advanced tab of the Java Component Reference, it might hold a clue. Here are results of my novice approach using Studio to configure the interceptor you described.
<flow name="myprojectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<component class="org.mule.module.json.transformers.JsonToObject" doc:name="Java">
<logging-interceptor/>
</component>
</flow>
If I use the custom-transformer (Java Transformer), I don't see a way to add the interceptor.

Call a File Inbound Endpoint via Http endpoint - Set XML as payload and update in IBM MQ

I am designing an application flow where I have to read a file (XML File) and put the data into IBM MQ (Queue). Do I need to create an HTTP request that will trigger the File read and update queue, otherwise how do I perform this task.
Currently I am creating an HTTP Request and connecting it to WMQ but I am getting NULL data into the Queue. Basically the payload is NULL.
This is the data I read when I browse the Queue:
sr.org.mule.transport.NullPayload1.L5U���...xp
Try like this:
Whenever you use File connector at any other place than the beginning of the flow, then it becomes an outbound endpoint.
For using File as an inbound endpoint to retrieve any file, you must use it in the beginning of some flow & keep the flow in stopped state initially as:
<flow name="filePickupFlow" initialState="stopped">
<file:inbound-endpoint path="src/main/resources/input" responseTimeout="10000" doc:name="File"/>
<wmq:outbound-endpoint queue="gh" doc:name="WMQ" connector-ref="WMQ"/>
</flow>
For your case just change the path with your required file location.
Then for further calling it via http, create another flow with http endpoint & use an Expression component to start the flow containing file inbound endpoint call as :
<expression-component doc:name="Expression">
app.registry.filePickupFlow.start();
</expression-component>
Then if you wish to stop it after it completes processing, you can again use an Expression component as:
<expression-component doc:name="Expression">
Thread.sleep(5000);
app.registry.filePickupFlow.stop();
</expression-component>
Thread.sleep() is used here just to give some time gap between flow start & stop to let the flow operation complete. You can add some other thing for maintaing this time gap or set the time according to your own usage.
I guess this is what you were looking for.
Regards,
JJ
If you want to access the file component on-demand( Whenever you access the HTTP, only then File component needs to access), use Mule Requester in the place of file component which do the same work.
<mulerequester:config name="Mule_Requester" doc:name="Mule Requester"/>
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="test2" doc:name="HTTP Listener Configuration"/>
<flow name="httpFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<mulerequester:request config-ref="Mule_Requester" resource="file://C:/in" doc:name="Mule Requester"/>
<object-to-string-transformer doc:name="Object to String"/>
<logger message="**payload:*#[payload]" level="INFO" doc:name="Use WMQ here instead of Logger"/>
</flow>
Link to refer: https://github.com/mulesoft/mule-module-requester/blob/master/mulerequesterdemo/src/main/app/MuleRequesterDemo.xml
Instead of HTTP, you can also schedule job trigger using Poll component based on your requirement. Hope this helps.
You wanted to used file connector as inbound endpoint but actually using as outbound endpoint. Check your configuration xml file for file connector.
There are many ways for reading file as inbound like file connector, Poll scope or Quartz connector. You can use anyone of these as per you requirement. The simplest flow is as
<flow name="testfixedFlow">
<file:inbound-endpoint path="tmp" connector-ref="File" responseTimeout="10000" doc:name="File"/>
<wmq:outbound-endpoint queue="xyz" connector-ref="WMQ" doc:name="WMQ"/>
</flow>
But if you want to get resource in between the flow you can use Mule requester
Hope this helps.

Can the new http connector be used with the http:static-resource-handler in Mule 3.6?

I have the following flow, attempting to use the new http connector in Mule 3.6 with the http:static-resource-handler:
<flow name="facebook-sources-apiFlow1">
<http:listener config-ref="HTTP_Listener_Configuration" path="/admin" doc:name="HTTP"/>
<http:static-resource-handler resourceBase="${app.home}/web" defaultFile="index.html" doc:name="HTTP Static Resource Handler"/>
</flow>
I have a directory called 'web' under src/main/app, and an index.html in the web directory. When I try this, though, I get the following error:
null (java.lang.NullPointerException). Message payload is of type: NullPayload
Before sinking too much time into the problem, I thought I'd ask if the new http connector even works with the static-resource-handler.
I'm afraid you can't. There is a bug around this: https://www.mulesoft.org/jira/browse/MULE-8317

Alternative way for dynamic inbound endpoint for Mule 3

I am working on migrating my Mule 2 project to Mule 3. However, since Mule 3.3 stop supporting dynamic inbound endpoint, I need to find an alternative way to rewrite my inbound endpoint.
Basically, I want to make http call to get some data from specific websites by using current system time as the query parameter. My codes in mule-config.xml is like the following
<flow name="RetrieveNewsService">
<http:inbound-endpoint host="www.awebsite.com" port="80" path="datacenter/someData.asp?category=1&date=[function:dateStamp:MMddyyyy]" connector-ref="RetrieveNewsPollingHttpConnector" exchange-pattern="one-way" />
//doing some process
</flow>
I feed current time for "path" part, and it works perfect in Mule 2, but get the exception mentioning about dynamic inbound endpoint is not supported anymore.
Anyone has any idea how to rewrite the dynamic path for inbound endpoint and what is the purpose they decide to stop this feature? Thanks for your time!
You can use a poll on an HTTP outbound-endpoint as demonstrated here:
<flow name="RetrieveNewsService">
<poll frequency="10000">
<http:outbound-endpoint method="GET" host="localhost"
port="8082" path="test?dtm=#[server.dateTime.format('MMddyyyy')]"
exchange-pattern="request-response" />
</poll>
//doing some process
</flow>
PS. No idea why.

Mule 3.2 Flow Initialization in a specific order

I have two flows in mule. I want to make one of them start , only after the other has been up and running for a while. how can I do that? Thanks
<flow name="newHttpClientRequestProcessor" doc:name="newHttpClientRequestProcessor">
<flow name="RestNewHttpClientRequestFlow" doc:name="RestNewHttpClientRequestFlow">
<http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8082" path="subscription" doc:name="HTTP"/>
<jersey:resources doc:name="REST">
<component class="com.citi.isg.rest.endpoints.RestNewHttpClientEndpoint"/>
</jersey:resources>
</flow>
This is similar to what I replied to this recent question: https://stackoverflow.com/a/14633484/387927
Configure the flow that must not be initially started with initialState="stopped"
Whenever you want to start it, use the following MEL expression: app.registry.FLOWNAME.start();, replacing FLOWNAME with the name of the flow you want to start.