DIY Exercise 12-1 - Read and Write Files Using The File, FTP, and Database Connectors
DIY Exercise 12-1 - Read and Write Files Using The File, FTP, and Database Connectors
Database connectors
Objectives
In this exercise, you connect Mule applications and other systems together using various
connectors. You will:
• Insert files into a database using the Database connector.
• Write files using the FTP connector.
• Write files using the File connector.
Scenario
You are assigned to a project involving the company's banking branch, Mule Bank. The bank
has partnered with several retail partners to distribute gift cards that are funded by the bank but
handled and processed by Visa. The bank has decided to use tightly coupled solutions for their
partners who are not yet supporting REST or SOAP.
Gift card numbers are generated from your credit card processing partner Visa and are then
uploaded in bulk as CSV files to the Mule bank's middleware server. Each partner's new Visa
gift card numbers need to be stored in a separate CSV file for that partner. Your task is to
develop a network of Mule applications that will retrieve these Visa gift card CSV files and then
route and process them to the correct partner's receiving servers.
Each partner will use a different technology to receive their lists of Visa gift cards:
• The Food-N-Savings retail partner has set up a database table named Giftcards to
receive gift cards. In order to successfully insert each row in the CSV file into the
database, you will need to property map each Visa gift card row in the CSV file to the
correct database columns.
• The Meals-N-Go retail partner has set up an FTP server in their DMZ (demilitarized
zone) to receive gift cards. They have an existing processing system that expects gift
cards in a specific CSV format that is different from the Visa gift card CSV format, so you
will have to map from the Visa gift card CSV file format to the Meals-N-Go CSV format.
• The Online Retail Plus partner is a little more modern. They already have an Enterprise
Service Bus implementation with a JMS messaging bus. They also already have a JMS
message format set up to receive and process gift cards, so you will have to map from
the Visa gift card CSV format to the JMS message format.
Note: For ease of development and testing, you will first create all the flows for all Mule
applications in one Mule application. This helps you rapidly create and test the Process API
logic and data mappings. In the next exercise, you will refactor these flows into separate Mule
applications to be distributed to different partners, which can communicate with each other
using an Enterprise messaging service like JMS.
Install the mock-servers dependency located in MUFundamentals4.x DIY Files zip into your
local m2 repository to allow the project to run the mock FTP and mock Database servers that
you will use for this project. For help, follow the step-by-step Install a Mule application as a
dependency walkthrough in module 6.
Use property placeholders to configure the file reader, so properties including the input and
output folder can be changed when the Mule application is deployed to a different environment,
such as to a production environment.
Note: Your operations team will set up a folder in a server where Visa can upload files. The
server will host this Mule application in a customer-hosted Mule runtime. To learn how to deploy
Mule applications to customer-hosted Mule runtimes, take the Anypoint Platform Operations:
Customer-Hosted Runtimes course.
Add an environment variable to your Mule application and then add logic to the Food-N-Savings
Mule configuration file's flow to use the Java Derby database if the environment is not set to
prod.
Note: Remember that each flow that sends files to a partner must ultimately be an individual
Mule application that can be deployed in the same region as the partner's server.
Add logic to the processVisaGiftCards flow to test the CSV file's partner column. If the partner
value is "Food N Savings", route the event to the processing flow in foodnsavings.xml using a
Flow Reference component.
Note: In the next exercise, you will replace the Flow Reference with a JMS Publish operation to
a JMS queue and add a corresponding JMS Listener to the foodnsavings.xml file's flow.
In the foodnsavings.xml flow, use the following information and insert each CSV file row into the
Giftcards table in the database:
Note: To perform the database operations safely (to avoid SQL injection), be sure to use
parameterized queries to insert data into the table.
Add logic to the processVisaGiftCards flow to test the CSV file's partner column. If the partner
value is "Meals n Go", route the event to the processing flow in mealsngo.xml using a Flow
Reference component.
Note: In the next exercise, you will replace the Flow Reference with a JMS Publish operation to
a JMS queue and add a corresponding JMS Listener to the mealsngo.xml file's flow.
In the mealsngo.xml flow, use the following information and insert each row of the CSV file into
the FTP server:
The uploaded files must follow the naming pattern: MULEBANK-gc-{datetime}.csv, where
{datetime} is the current datetime when the file is received by the process-visa-giftcards Mule
application and must be in milliseconds since 1970 January 1 (Unix Epoch).
Add logic to the processVisaGiftCards flow to test the CSV file's partner column. If the partner
value is "Online Retail Plus", route the event to the processing flow in oretailplus.xml using a
Flow Reference component.
Note: In the next exercise, you will replace the Flow Reference with a JMS Publish operation to
a JMS queue and add a corresponding JMS Listener to the oretailplus.xml file's flow.
In the oretailplus.xml flow, use the following information and convert the entire Visa CSV file
with the following mapping:
Finally, write out a copy of the payload to a local CSV file. The CSV files should be named
MULEBANK-{partner3.name}-{datetime}-{count}.csv, where {count} is the record count for the
CSV file and {datetime} is the current date and time in milliseconds since Unix Epoch.