Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Mule Kernel 4: Anypoint Studio

DZone's Guide to

Mule Kernel 4: Anypoint Studio

Look at writing a simple application in an environment that we should know pretty well...Anypoint Studio.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

In the last article, I described my attempts with writing Mule apps in IntelliJ for Kernel edition. With no luck. Here, I will focus on writing a simple application in an environment that we should know pretty well...Anypoint Studio. After that, I will deploy this application on the Kernel application server to test how it works. If you are interested in basic information about Mule Kernel 4, look at this article where I have revealed some basic news.

Anypoint Studio

The newest version of Anypoint Studio can be downloaded from here. If you have already installed Studio, I encourage you to compare versions. It happens that slightly older versions like 7.2.2 won't get upgrades to version 7.3. It is good to check this, especially when rapid development of this IDE is made.

Anypoint Studio is a platform for developing Mule applications. From version 7, however, it is only dedicated to enterprise runtimes. On the other hand, on one of the MuleSoft pages, I have noticed information that Kernel has or will have the support of Anypoint Studio.

Demo Application

The application will be a really simple one. HTTP listener will be accepting only GET and POST request on a single URI. Like in the diagram below.

The first operation will return static service's status. Below you can find the output.

{
"status": "UP",
"details": {
"serviceType": "http",
"status": "UP",
"statusCode": 200
}
}

Health

It is a good practice to embed a health endpoint into an application. More on that, I have written in the following article.

The second operation will accept JSON input and produce from this an XML document. Response will look like below.

<health>
//your input here as an XML
</health>

Development

You can find the complete code on GitHub. If you would like to do it on your own, please follow closely this chapter.

Project Creation

In Anypoint Studio, you may create a new Mule project. First, select runtime. Although we are developing an application for Kernel runtime, we should select EE in studio.

API Preparation

We will use APIKit to generate flows. In order to do it we need to prepare RAML API specification. Below you can find a really simple one, but sufficient for the purpose of this demo.

#%RAML 1.0
title: Health Service
mediaType: application/json

/health:
  get: 
  post:

Once we have this file in src/main/resources/api you may click the file and select Mule > Generate Flows from REST API.

Warning — EE Namespace

By default, APIKit will generate error handling with Transform Messages components. In order to use it on Kernel runtime, we need to refactor it to Set Payload and Set Variable processors.

Here is a sample transformation prepared for APIKIT:BAD_REQUEST error

<ee:transform>
  <ee:message>
    <ee:set-payload><![CDATA[%dw 2.0
       output application/json 
       ---
       {message: "Bad request"}]]>
    </ee:set-payload>
  </ee:message>
  <ee:variables>
    <ee:set-variable variableName="httpStatus">400</ee:set-variable>
  </ee:variables>
</ee:transform>

As you can see transformation set a simple JSON body with message property and variable httpStatus that will be used to set HTTP status returned to the client. In the example 400 error code, we need to rewrite it using Set Payload and Set Variable components like below

<set-payload value='#[output application/json
---
{message: "Bad request"}]' doc:name="Set Eror Payload"/>

<set-variable value="400" doc:name="Set HTTP status" variableName="httpStatus"/>

Methods' Implementation

So far, we should have an application, API, and Mule configuration file with generated flows. We should have two private flows:

  • get:\health:health-config
  • post:\health:health-config

We need to omit all component that are in EE namespace. Therefore I will use Set Payload processor to perform simple transformations.

EE Processors

When you drag any processor form EE namespace like Transform Message you won't be able to run that application on Kernel runtime. You need to remove that processor and take care of namespaces in Configuration XML.
Example to remove:
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
and a corresponding line form xsi:schemaLocation
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd

In Set Payload, I have set the following value:

Anypoint Studio — DataWeave editor

As of version 7.3 of Anypoint Studio new editor has been introduced for writing DataWeave expressions in other components like Set Payload, Logger etc. It will look like a standard Transform Message's editor — you can see it above.
To enable that view you need to click button f(x) next to the field.

As you can see, I have specified, using output directive, that I expect JSON. In the body section, I have provided a structure that will be translated into an appliction/JSON object.

This time in Set Payload I have provided the following expression:

output application/xml
---
{
"health": payload
}

As a result, I will have an XML document with a root element called health and sub-elements coming from input request.

Kernel 4 enablement

In order to make sure that you are making your application for Kernel runtime you should edit mule-artifact.json file.

In the JSON body, you need to add a property called requiredProduct like in the example below.

{
   "requiredProduct":"MULE",
   "minMuleVersion":"4.1.1"
}

According to the documentation, MULE value means that application can be deployed on the Kernel runtime. When we specify MULE_EE that means that application can be deployed on Enterprise runtime.

Deployment

In order to deploy the application you need to build it first. You may do this using following command

mvn clean install

Deployable artifacts are now packed into a JAR file. As a result, in the target directory, you will find jar file instead of a zip archive.

You need to move that file into an apps folder of your runtime. In your runtime, you should see the status of deployment like:

Now you are ready to test that.

Summary

Developing application for Kernel runtime is rather straightforward but you need to keep in mind that currently, Anypoint Studio does not sport that runtime. However, we may develop an application using EE runtime for now.

There are some things that you need to remember such as:

  • not using enterprise components like Transform Message
  • remove ee namespace
  • APIKit use Transform Message by default for error handling
  • required product needs to be set in mule-artifact.json

Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

Topics:
integration ,mule 4 kernel ,anypoint studio ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}