Thursday, 2 July 2020

Notes: Module 11: Mule 3 Fundamentals: Connecting to Additional Resources



Objectives:
- Connect to SaaS applications
- Connect to files
- Poll resources
- Connect to JMS queues
- Discover and install connectors not bundled with Anypoint Studio

11-1) Connecting to SaaS Applications (Salesforce)


Walkthrough 11-1: Connect to a Saas application (Salesforce)
- Use the Salesforce connector to retrieve accounts for a postal code
- Use the Query Builder to write a query

Image: Add Saleforce connector to canvas and Salesforce libraries get added too (also added to POM file in Maven)

Many operations are available with the Salesforce connector.

11-2) The File Connector

Gives Mule applications the ability to read/write files in the local file system:
- Read input files
- Create new files
- Copy (backup) files
- Append to existing files
- Read periodically and delete/move/leave once processed

By default it uses streaming:
- Payload is a FileInputStream
- Streams are closed by transformers reading the input stream

Can turn off streaming:
- Payload is a byte array

Connector configuration:
- Not required (standalone)
- Use for reusability and setting properties

File connector as outbound endpoint:
- Passes files to the connected file system
- File name can be set at runtime

Walkthrough 11-2: Connect to a file (CSV)
- Add and configure a File endpoint to watch an input directory, read the contents of any added files, and then rename and move the files
- Use DataWeave to convert a CSV file to a string
- Add a CSV file to the input directory and watch it renamed and moved
- Restrict the type of file read
- Add payload metadata to a file endpoint

We have an input and output folder under:

src/main/resources
    input
    output

Image: Configuring the File connector

Use regex to search for just CSV say.
Use ‘Move to Pattern’ to rename with say .backup.
Use Transform.

11-3) Polling Resources

Some connectors use or can use a polling process to actively retrieve messages e.g. File, FTP, SMTP.
For other message processors, use a Poll scope element to actively call a resource at regular intervals.

Image: Polling Information (Frequency is milliseconds)

Use a watermark to only retrieve newly created or updated data.
- The value must persist across flows:
--- Mule uses a build-in object store for persistent storage and exposes the value as a flow variable
----- Saved to file for embedded Mule and standalone Mule runtime
----- Saved to data storage for CloudHub
----- Saved to shared distributed memory for clustered Mule runtimes

Walkthrough 11-3: Poll a resource
- Create a flow with a Poll scope as the message source
- Poll a database every 5 seconds for records with a specific postal code
- Use a poll watermark to track the ID of the latest record retrieved
- Use the watermark to only retrieve new records with that postal code

Image: Configuring Poll Scope and Watermark

Image: Configuring pollDatabaseFlow

Image: Prompting to clear the watermark

11-4) Connecting to JMS (Java Messaging Service) Queues

JMS is an API for enabling applications to communicate through the exchange of messages, it provides a standard interface for creating/sending/receiving messages. Supports two messaging models:

1) Queues: PTP (point-to-point)
One-to-one! A sender delivers messages to a queue and a single receiver pulls the message off. The receiver does not need to be listening at the time the message is sent.

2) Topics: Pub-Sub (publish/subscribe)
One-to-many! A publisher sends a message to a topic and all active subscribers of the topic receive the message. Subscribers not actively listening will miss the published message (unless messages are made durable).

The Mule JMS connector can connect to any JMS messaging service that implements JMS spec.

Supported JMS providers:
- Out-of-the-box support for ActiveMQ and WebLogic JMS.
- Others (see the documentation) are supported by a generic JMS or custom JMS configuration.

Walkthrough 11-4: Connect to a JMS queue (ActiveMQ)
- Create a flow accessible at ‘http://localhost:8081/jms’
- Add and configure an Active MQ connector
- Use a JMS endpoint to retrieve messages from a JMS topic
- Add messages to the topic using a web form
- Use a JMS endpoint to send messages to a JMS topic

Image: Connect to a JMS queue

General recommendation: do not use the ALL jar library; use just what you need in your application (to avoid bloat).

Image: Configuration of JMS connectors

Configuration of Logger: message: #[payload]
Configuration of Set Payload: value:
#[message.InboundProperties.’http.query.params’.name] : [message.InboundProperties.’http.query.params’.messageBody]

Image: Using the Mule JMS HTTP app in Postman

11-5) Discovering and using additional connectors

Anypoint Exchange:

11-6) Introducing Anypoint Connector DevKit for creating custom connectors

You can create custom connectors with Anypoint Studio and Anypoint Connector DevKit.
Note: Use Maven for development and building.

No comments:

Post a comment