Watching a Directory and Sending Files with Universal File Mover (How To #11)

In this blog posting, I will show you how to configure Universal File Mover to watch a directory for new files and send the new files immediately. In this example, Universal File Mover will connect to the queue manager in “client mode”.

In the Universal File Mover’s Watch Action, the user can specify all files (‘*’) or select particular files by their file extension (i.e. txt) to have UFM actions done against the file (i.e. MQSend).


In this example, the following servers are used but they only have WebSphere MQ (WMQ) Client installed (no queue managers):
– aix002 is an AIX server with WMQ Client and UFM software installed
– linux002 is a Linux server with WMQ Client and UFM software installed

In this example, the following queue managers are used:
MQA1 is a queue manager residing on a AIX (aix001) server (sender)
MQL1 is a queue manager residing on a Linux (linux001) server (receiver)

TEST.LINUX.QL and TEST.LINUX.QL.BK are local queues defined in queue manager MQL1 (receiver)
TEST.LINUX.QR is a remote queue defined in queue manager MQA1 (sender)

If you do not know how to define/setup communication between 2 queue managers then follow the instructions in this blog posting:
https://www.capitalware.com/rl_blog/?p=509

Step #1: On the linux002 server, create a file in the mq directory called mql1.xml and copy the following into the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE UFM_MQ SYSTEM "UFM_MQ.dtd">
<UFM_MQ>
    <QMgrName>MQL1</QMgrName>
    <QueueName>TEST.LINUX.QL</QueueName>
    <Hostname>linux001</Hostname>
    <ChannelName>SYSTEM.DEF.SVRCONN</ChannelName>
    <Port>1414</Port>
    <UserID>tester</UserID>
</UFM_MQ>

The mql1.xml (UFM_MQ XML) file describes how to connect to the remote MQL1 queue manager on server linux001. The connection will use UserID of tester. If you do not have that UserID defined on the linux001 server then use a UserID that is defined.

Step #2: On the linux002 server, in the UFM install directory, create a file called ufm_receive_test_11.xml and copy the following into the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE UFM_Workflow SYSTEM "UFM_Workflow.dtd">
<UFM_Workflow>

  <Actions>
    <MQReceive run="D" >
      <MQ>
        <MQFile>mql1.xml</MQFile>
        <BackOutQName>TEST.LINUX.QL.BK</BackOutQName>
      </MQ>
      <Default>
         <Directory override="Y">/home/roger/UFM/</Directory>
      </Default>
    </MQReceive>
  </Actions>

</UFM_Workflow>

When UFM is started, it will run as a daemon (run=”D”) and use a backout queue called TEST.LINUX.QL.BK just in case there is an issue with a message. UFM will override the message’s specified directory and use the one provided. Either create /home/roger/UFM/ directory on your Linux server or use a directory that already exist on your Linux server.

Step #3: On the linux002 server, start UFM to receive the file transfers:

./ufm.sh ufm_receive_test_11.xml &

Step #4: On the aix002 server, create a file in the mq directory called mqa1.xml and copy the following into the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE UFM_MQ SYSTEM "UFM_MQ.dtd">
<UFM_MQ>
    <QMgrName>MQA1</QMgrName>
    <QueueName>TEST.LINUX.QR</QueueName>
    <Hostname>aix001</Hostname>
    <ChannelName>SYSTEM.DEF.SVRCONN</ChannelName>
    <Port>1414</Port>
    <UserID>tester</UserID>
</UFM_MQ>

The mqa1.xml (UFM_MQ XML) file describes how to connect to the remote MQA1 queue manager on server aix001. The connection will use UserID of tester. If you do not have that UserID defined on the aix001 server then use a UserID that is defined.

Step #5: On the AIX server, in the UFM install directory, create a file called ufm_watch_test_1.xml and copy the following into the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE UFM_Workflow SYSTEM "UFM_Workflow.dtd">
<UFM_Workflow>

  <Actions>
    <Watch pollinterval="5">

      <WatchItem type="F">
        <File>/apps/test/amce.txt</File>
        <Actions>
          <MQSend delete="Y" format="S">
            <File>${WATCH_FOUND_FILE}</File>
            <MQ>
              <MQFile>mqa1.xml</MQFile>
            </MQ>
            <Remote>
              <Directory>/apps/other/</Directory>
            </Remote>
          </MQSend>
        </Actions>
      </WatchItem>

      <WatchItem type="D">
        <Directory>/apps/test2/</Directory>
        <Actions>
          <MQSend delete="Y" format="S">
            <File>${WATCH_FOUND_FILE}</File>
            <MQ>
              <MQFile>mqa1.xml</MQFile>
            </MQ>
            <Remote>
              <Directory>/apps/other2/</Directory>
            </Remote>
          </MQSend>
        </Actions>
      </WatchItem>

      <WatchItem type="D">
        <Directory>/apps/test3/</Directory>
        <Extension>txt</Extension>
        <Extension>csv</Extension>
        <Actions>
          <MQSend delete="Y" format="S">
            <File>${WATCH_FOUND_FILE}</File>
            <MQ>
              <MQFile>mqa1.xml</MQFile>
            </MQ>
            <Remote>
              <Directory>/apps/other3/</Directory>
            </Remote>
          </MQSend>
        </Actions>
      </WatchItem>

    </Watch>
  </Actions>

</UFM_Workflow>

When UFM is started, the Watch Action will run forever. The poll interval for the Watch Action tells UFM to continuously scan (every 5 seconds) the files/directories given in the WatchItem elements of the Watch element.

(1) The first WatchItem element has UFM waiting for the file “/apps/test/amce.txt” to appear and when it does, UFM performs the Actions associated (i.e. MQSend) within the WatchItem element. After the MQSend action sends the file, the file is delete.

(2) The second WatchItem element has UFM watching a directory (/apps/test2/) for any file to appear in that directory. When a file appears, UFM performs the Actions associated (i.e. MQSend) within the WatchItem element.

(3) The third WatchItem element has UFM watching a directory (/apps/test3/) for any file with the file extension of “txt” or “csv” to appear in that directory. When a matching file appears, UFM performs the Actions associated (i.e. MQSend) within the WatchItem element.

Note: The ${WATCH_FOUND_FILE} variable is by the user as a place holder to the “found file” from the WatchItem. UFM will transform the variable to the actual file name when the Action is executed.

Step #6: On the aix002 server, start UFM to begin monitoring for files:

./ufm.sh ufm_watch_test_1.xml &

UFM will start, watch for files to arrive in the specified directory. Note: To stop the Watch Action, the user will need to use the Ctl-C or kill command.

Step #7: On the linux002 server, verify that the test file was put into the /home/roger/UFM/ directory or whatever directory you specified in the ufm_receive_test_11.xml file.

Step #8: Finally, we need to stop UFM daemon that is running on the linux002 server. In the UFM install directory, create a file called ufm_putquit_test_11.xml and copy the following into the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE UFM_Workflow SYSTEM "UFM_Workflow.dtd">
<UFM_Workflow>
  <Actions>
    <MQPutQuit>
      <MQ>
        <MQFile>mql1.xml</MQFile>
      </MQ>
    </MQPutQuit>
  </Actions>
</UFM_Workflow>

Step #9: On the linux002 server, run UFM with the MQPutQuit action:

./ufm.sh ufm_putquit_test_11.xml

This blog demonstrates how easy it is to watch a directory for files or for a single file to arrive then immediately send the files using UFM.

Remember, when possible, use the compression option for any MQSend Action used within an WatchItem element to speed up the transfer time and reduce the load placed on MQ. You can combine both encryption and compression when sending files (any size) using UFM.

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in Capitalware, IBM i (OS/400), IBM MQ, Java, Linux, macOS (Mac OS X), Open Source, Universal File Mover, Unix, Windows.

Comments are closed.