Mule Kernel 4 – Anypoint Studio

Last Updated on 30/03/2020 by Patryk Bandurski

In the last article I have 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 environment that we should know pretty well … Anypoint Studio. After that I will deploy this application on Kernel application server to test how it works. If you are interested with basic information’s about Mule Kernel 4 look at this article where I have revealed some basic news.

Anypoint Studio

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 MuleSoft pages I have noticed information that Kernel has or will have support of Anypoint Studio. 

Demo application

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

Application's API
Application’s API

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.

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

Complete code you can find 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 new Mule project. In first place select runtime. Although we are developing 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
GET method
GET method

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 following value

Setting payload in GUI
Setting payload in GUI

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 structure that will be translated into an appliction/json object.

Transformation JSON into XML

This time in Set Payload I have provided following expression:

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

As a result I will have XML document with 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 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 your should see status of deployment like:

Deployment status
Deployment status

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 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 hanlding
  • required product needs to be set in mule-artifact.json

Mule Kernel 4 – Anypoint Studio

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top