Thursday, 2 July 2020

Notes: Module 3: Mule 3 Fundamentals: Building APIs



3-1) Introducing Mule Applications

Mule applications accept and process messages through a series of message processors plugged together in a flow.

Message source ---> Message processor(s) ---> Message processor(s) ---> Connector endpoint

Mule runtime is a JVM server that:
- Can handle many concurrent requests for different Java (Mule) applications in a single JVM
- Decouples point-to-point integrations by having all (non-Mule) applications talk to the bus (to a Mule runtime) instead of directly to each other
- Enforces policies for API governance

Mule CE is open source.
Mule EE is a hardened code line with support and additional capabilities.

Mule EE is required for:
Visual Debugging, DataWeave and DataSense, Batch module, Additional connectors and more...

A typical flow has:
- A message source (external source triggers the execution of the flow)
- Message processors (transform/filter/enrich/process the message)

A Mule message:
Inbound properties <--- from="" message="" o:p="" set="" source="" the="">
Outbound properties <--- added="" by="" message="" o:p="" processor="">
Payload <--- core="" message="" o:p="" of="" the="">
Attachments <--- ancillary="" info="" message="" o:p="" the="" to="">

3-2) Creating Mule applications with Anypoint Studio

Image: Anatomy of a flow: Visual

Image: Anypoint Studio anatomy

How to test applications:
- A browser
- A cURL command-line utility
- A browser extension like Postman
- MUnit (a Mule application testing framework for building automated tests)

3-3) Understanding Mule application building blocks

Mule application building blocks are separated into categories in the Mule Palette:
- Connectors
- Scopes
- Components
- Transformers
- Filters
- Flow Control
- Error Handling

3-4) Connecting to Databases

- Can connect to almost any JDBC-compliant relational database.
- Supports operations including: SELECT, INSERT, UPDATE, DELETE, Stored Procedures, Bulk Execute, CREATE, ALTER...

Image: Example Database Query in Anypoint Studio (RAW output returned)

3-5) Transforming Data

DataWeave:
- is a full-featured and fully native framework for querying and transforming data on Anypoint Platform
- can be used for all your transformations (simple to complex)
- enables very high performance transformations
- is fully integrated with DataSense allowing a payload-aware development.

Image: DataWeave: Transform Message: Code Auto-Completion

Image: Converting SQL Output to JSON is as simple as adding an ‘Object to JSON’ transformer (but it is better to use DataWeave)

Image: Converting Database Output using ‘Transform Message’ (DataWeave)

Image: Transform Messsage

3-6) Creating RESTful Interfaces Manually for Mule Applications

Image: Adding {ID} to Path

{} states that we’re not just expecting /flights but also some type of dynamic value after flights in the URI.

Set Payload Example:
Set Payload > Settings > Value:
{"message": "Flight added (but not really)"}

3-7) Generating RESTful interfaces automatically using APIkit

APIkit is an open-source toolkit that includes:
- A Router element for Mule applications
- A Proxy element for Mule applications
- APIkit Mapping Exception Strategy
- APIkit Anypoint Studio plugin

The Anypoint Studio APIkit plugin can generate an interface automatically from a RAML API definition.
It generates a main routing flow (that uses APIkit Router) and flows for each of the resource / method pairs.
You add processors to the resource flows (usually Flow References) to hook up to your backend logic.

Image: The APIkit automatically generated interface

APIkit allows a seamless API design to implementation experience:
- When you create a project with APIkit components and a RAML file on Anypoint Platform, a connection is made to that RAML file
--- The remote RAML is polled periodically and compared to the local version
- Use the APISync view to pull/push/compare between Anypoint Studio and API Desginer

Image: Creating a New Mule Project and Adding APIkit Components

3-8) Connecting Interfaces to Implementations

Passing Messages to Other Flows:
- Flows can be broken into multiple flows.

Image: Copying Mule Flow XML from one project to another

Note: The red cross is because we need to copy the MySQL driver from ‘Referenced Libraries’ in the original project, to the new project.

Walkthrough: Implement a RESTful web service
- Pass a message from one flow to another
- Create new logic for nested resource call
- Call the backend flows
- Test the web service in the APIkit Consoles view and Postman

No comments:

Post a comment