1
votes

I'm using Mule ESB CE 3.4. I have a requirement where I'm reading the configuration information from database and using it as the file name for the file outbound endpoint. Here is an example code (the code may not work as I have only given an outline)

<file:connector name="File-Data" autoDelete="false" streaming="true" validateConnections="true" doc:name="File" />
.....
<!-- Gets the configuration from database using a transformer. The transformer populates the configuration entries in a POJO and puts that in a session.  -->
<custom-transformer  class="com.test.DbGetConfigsTransformer" doc:name="Get Integration Configs"/>
....<!-- some code to process data -->
<logger message="$$$: #[sessionVars['currentFeed'].getFilePattern()]" doc:name="Set JSON File Name" /> -->
<file:outbound-endpoint path="/temp" outputPattern="#[sessionVars['currentFeed'].getFilePattern()]" responseTimeout="10000" mimeType="text/plain" connector-ref="File-Data" doc:name="Save File"/>

The above code throws the following error:

1. The filename, directory name, or volume label syntax is incorrect (java.io.IOException)
  java.io.WinNTFileSystem:-2 (null)
2. Unable to create a canonical file for /temp/Test_User_#[function:datestamp:YYYYMMddhhmmss.sss] (org.mule.api.MuleRuntimeException)
  org.mule.util.FileUtils:354 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MuleRuntimeException.html)
3. Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=file:///temp, connector=FileConnector

In the database table, the field name is called FilePattern and it has the value 'Test_User_#[function:datestamp:YYYYMMddhhmmss.sss]. If I hardcode the value or move this value to the mule configuration file

file.name=Test_User_#[function:datestamp:YYYYMMddhhmmss.sss]

and use the configuration property syntax (for e.g. ${file.name} in the 'outputpattern'), it works. But if I read the same from db and use it, it is not working and throwing the error. The logger displays as (which is read from the db)

$$$: Test_#[function:datestamp:YYYYMMddhhmmss.sss]    

Any help is much appreciated.

1
You're using the super old expression syntax. Try MEL instead. See: mulesoft.org/documentation/display/current/…David Dossot
David - I may have misunderstood that - but how do I use that here, because the actual pattern (Test_User_#[function:datestamp:YYYYMMddhhmmss.sss]) is stored in db and I'm assigning this pattern from a session variable in the 'outputpattern attribute'. I tried #[server.dateTime.format(#[sessionVars['currentFeed'].getFilePattern()])], but this gave me an error for the obvious reason that the patter has a text ('Test_').Tech Matrix
No need to nest expressions: #[server.dateTime.format(sessionVars['currentFeed'].getFilePattern())]David Dossot
Got that. But this expression failed with the error message 'Exception stack is: 1. Illegal pattern character 'T' (java.lang.IllegalArgumentException) java.text.SimpleDateFormat:-1 (null)'. The reason it has to come from database instead of mule configuration file is because, the prefix to this file pattern (in this example - 'Test_') will vary and it will be configured as and when a new entry comes in. The flow will read these records and loop it and create those files. So, what I store in the db will indicate the environment_<datetime> to uniquely distinguish the files.Tech Matrix

1 Answers

2
votes

If your datestamp format does not vary, you should just store the environment prefix in your db and use something like:

outputPattern="#[sessionVars['prefix']+server.dateTime.format('YYYYMMddhhmmss.sss')]"

If you need to use your current database values, you can use basic Java string methods to find the correct substrings. For example:

#[sessionVars['currentFeed'].getFilePattern().substring(0,sessionVars['currentFeed'].getFilePattern().indexOf('function')-2)+server.dateTime.format('YYYYMMddhhmmss.sss')]

If you use different datestamp formats, you can find that part as well using similar String methods. However, I still suggest you come up with an implementation that only stores the environment prefix in the db.