How to install the JSON Logger compatible with Java 17


MuleSoft is adopting Java’s long-term support (LTS) release, beginning with the Mule Runtime 4.6 which will add support for Java 17 LTS. This has an impact on our Mule Apps, especially when it comes to our connectors, as all of our connectors will have to be updated to a new version compatible with Java 17. That’s the case for the JSON Logger. 

There’s a new version of the JSON Logger compatible with Java 17. In this post, we’ll see how to install this new version of the JSON Logger and use it with Java 17 and a compatible Mule Runtime (4.6+)

These are the steps to follow:


Download the updated version of JSON Logger

  • Create a folder in your laptop to use it as working directory
  • The new version of the JSON Logger updated for Java 17 can be found here. 
  • Clone the repo or download it into your folder

git clone https://github.com/mtransier/json-logger


Prerequisites - Versions

In this tutorial, we’ll use the following versions:
  • Anypoint Studio 7.19
  • Maven version 3.6+ - In this tutorial, we’re using Maven version 3.9.9
  • JDK - OpenJDK 17
  • Mule Runtime - 4.6.x


Create a Connected App

To make the JSON Logger available in our Anypoint Org we need to publish it to our Private Exchange. We will deploy our JSON Logger via the Mule Maven Plugin (MMP) so, for that, we’ll need to provide the MMP credentials and permissions to publish to our Exchange.

Technically speaking, the MMP can use User Credentials or a Connected app to deploy applications. But, as a best practice, it’s always recommended to use a Connected App. So, the first thing we’ll do is to create a Connected App with the minimum permissions required to publish artifacts to our Exchange.

Head over to Anypoint > Access Management > Connected Apps and click on Create app



Then, provide a name for your Connected App and choose the App act on its behalf (client credentials) option. 


As a best practice, we want to follow the least-privileged principle and grant only the minimum permissions for our Connected App to publish artifacts to or Private Exchange. To do so, we need to add only 1 Scope to our Connected App: the Exchange Contributor. 

Click on Add Scopes and add the following:
  • Under Exchange:
    • Exchange Contributor

Select the Business Groups and the Environments on which you are planning to deploy with the Mule Maven plugin. 


Click on Save



You should now see our new Connected App in the list with its client id and secret that we will use in the next step.

Notice that this Connected App has the minimum permissions to publish to Exchange. If you want to use an existing Connected App just make sure the Exchange Contributor scope is in the list of the scopes of your Connected App.


Modify the settings of your Maven Installation

Once we’ve got the required credentials, now we need to provide them to our Maven installation. 
Head over to your laptop or to the server from which you will run the Mule Maven Plugin and open the settings.xml file. It should be located at ~/.m2 (Linux) or C:\Users\[USER]\.m2. 
In Maven, credentials for a repository are added as a <server> element. We need to add the following:

<settings>

<servers>

<server>
<id>anypoint-exchange-v3</id>
<username>~~~Client~~~</username>
<password>{CLIENT_ID}~?~{SECRET}</password>
</server>

</servers>

</settings>
Where:
  • ~~~Client~~~ tells Maven these are Connected App credentials
  • {CLIENT_ID} and {SECRET} are the client id and secret of our Connected app.


Modify the POM file of json logger

Now, open the project you’ve downloaded from the JSON Logger - Java 17 repo from Anypoint Studio and open the pom file.
We need to modify the Maven coordinates of this project and provide the Anypoint Org or Business Group ID where you want to make the JSON Logger component available.
You can copy that ID from Anypoint > Access Management > Business Groups > [YOUR BUSINESS GROUP].
Then past that ID in the groupID tag:

<project>
...
<groupId>[BUSINESS_GROUP_ID]</groupId>
<artifactId>json-logger</artifactId>
<version>2.2.0</version>
<packaging>mule-extension</packaging>
<name>JSON Logger - Mule 4</name>
...
</project>

Make sure as well you’ve got the distributionManagement element in the pom file, and that the id of the repository element matches the id of the server element in your settings.xml file that we’ve set up in the previous step.

<distributionManagement>
<repository>
<id>anypoint-exchange-v3</id>
<name>Anypoint Exchange V3</name>
<url>https://maven.anypoint.mulesoft.com/api/v3/organizations/${project.groupId}/maven</url>
<layout>default</layout>
</repository>
</distributionManagement>


Publish to Exchange

Open a terminal from the root folder of the Mule Project. For that, in Anypoint Studio, right-click on the project name > Show In > System Explorer. That will open the root folder of the JSON Logger project. From there, open a terminal and run the following command:

mvn clean deploy

This will publish the app to Exchange (it won’t deploy it to Runtime manager)


Verify the Installation

Lastly, let’s verify that the JSON Logger has been successfully published and is available in your Private Exchange. For that, go to Anypoint > Exchange and click on your Org or the Business Group you’ve published the asset. You should see it in the list of assets.


Test the JSON Logger

As mentioned in this post, we’ve just installed the JSON Logger version that is compatible with Java 17. So, the first step, when creating an app with Java 17 is to verify that the connectors, libraries or any other component in your project is compatible with Java 17. Check out this link with the list of compatible connectors.

In our case, to verify the JSON Logger works as expected, let’s create a Mule App and use the JSON Logger we’ve just published.
From the Mule Palette drag and drop:

  • An HTTP listener - create a GET /hello endpoint
  • A Set Payload processor - Include a custom message as a response of the endpoint

Next, let’s import the JSON Logger from our org. There are two ways of doing it:


Option 1: From the Mule Palette

Click on Search in Exchange and then click on Please Login if you’re not logged in to your Anypoint Org. Provide your credentials.
Then, type json logger in search box. It should now display your JSON Logger in the left panel. Make sure the JSON Logger is 2.2.0



Click on Add and Finish


Option 2: Use the Maven coordinates of the JSON Logger artifact

A quicker way to import the JSON Logger is by adding the dependency directly in the POM file. For that, open the pom.xml file of our app and, within the dependencies section, paste the following:

<dependency>
<groupId>[YOUR_BG_ID]</groupId>
<artifactId>json-logger</artifactId>
<version>2.2.0</version>
<classifier>mule-plugin</classifier>
</dependency>

Save the POM file and automatically Anypoint Studio will download the dependency and make it available for your project



Make sure, in the tree structure of your project, that you’re using Java 17, the JSON Logger 2.2.0 and the Mule Runtime version 4.6 or later.

If that does not work, double-check the maven coordinates of your artifact. Go to Exchange and Open the JSON Logger asset. From there, you will find the Dependency Snippets button which tells you the coordinates of your JSON Logger.



Add the JSON Logger

Once your JSON Logger is in your Mule Palette, from its list of operations drag and drop a Logger to our flow, between the listener and the Set Payload processor.


Click on it and then create a new configuration. We will just provide hard-coded values for simplicity:


Next, add a Message for your Logger


Save the file and Run the project. If everything works, the project should be compiled well and successfully deployed to the Anypoint Studio embedded runtime. 
When the Mule Runtime starts in Studio, double-check the versions of the project in your Console:



Once your app is deployed, send a GET request to our test endpoint and see the Console. 
We should finally see our custom log message in JSON!




Previous Post Next Post