For BE/B.Tech/BCA/MCA/ME/M.Tech Major/Minor Project for CS/IT branch at minimum price Text Message @ 9424820157

Custom Policy in Mule 4

Custom Policy in Mule 4


Implementation of custom policy in Mule 4 is a bit different from Mule 3.

First of all, install Maven, if you don't have one.
Put settings.xml in,m2 folder of the c drive.

In settings.xml, in servers tag, put repository id, Anypoint platform username, and password as shown below:

  <servers>
    <server>
      <id>Repository</id>
      <username>Anypoint username</username>
      <password>Anypoint Password</password>
    </server>
  </servers>


Creating a policy requires creating several files like <policy>.yaml and template.xml, etc. We also need to have a proper structure for the policy project.

The easiest way to create the proper structure along with the files is by using the maven archetype provided by Mulesoft.

Include the following section in your settings.xml to make the archetype available:


<profiles>
   <profile>
     <id>archetype-repository</id>
     <repositories>
       <repository>
         <id>archetype</id>
         <name>Mule Repository</name>
         <url>https://repository-master.mulesoft.org/nexus/content/repositories/public</url>
         <releases>
           <enabled>true</enabled>
           <checksumPolicy>fail</checksumPolicy>
         </releases>
         <snapshots>
           <enabled>true</enabled>
           <checksumPolicy>warn</checksumPolicy>
         </snapshots>
       </repository>
     </repositories>
   </profile>

 </profiles>


To generate the skeleton using the archetype, use the following command: 

mvn -Parchetype-repository archetype:generate -DarchetypeGroupId=org.mule.tools -DarchetypeArtifactId=api-gateway-custom-policy-archetype -DarchetypeVersion=1.1.0 -DgroupId=${orgId} -DartifactId=${policyName} -Dversion=1.0.0 -Dpackage=mule-policy

In place of ${orgId} write your organization Id.
for Organization id, you can find it by navigating to:
Access Management > Organization > Organization Id

In place of ${policyName} write the name of your custom policy

Once the command executed successfully, it will generate
1. pom.xml
2. YAML file
3. template file

In pom.xml, the repository id should be the same as of settings.xml

I already made the changes with the color blue in pom.xml
Refer below pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>e38d5c94-0642-45a9-aca0-2c2e36d4242e</groupId>
    <artifactId>custpol</artifactId>
    <version>1.0.0</version>

    <name>custpol</name>
    <description>custpol</description>

    <packaging>mule-policy</packaging>

    <properties>
        <mule.maven.plugin.version>3.2.7</mule.maven.plugin.version>
        <exchange.url>https://maven.anypoint.mulesoft.com/api/v1/organizations/e38d5c94-0642-45a9-aca0-2c2e36d4242e/maven</exchange.url>
    </properties>


<repositories>
<repository>
<id>Repository</id>
<name>MuleSoft Exchange Environment</name>
<url>${exchange.url}</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.mule.tools.maven</groupId>
                <artifactId>mule-maven-plugin</artifactId>
                <version>${mule.maven.plugin.version}</version>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <executions>
                    <execution>
                        <id>upload-template</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy-file</goal>
                        </goals>
                        <configuration>
                            <repositoryId>exchange-server</repositoryId>
                            <url>${exchange.url}</url>
                            <file>${project.basedir}/${project.artifactId}.yaml</file>
                            <generatePom>false</generatePom>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <packaging>yaml</packaging>
                            <classifier>policy-definition</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>Repository</id>
            <name>Corporate Repository</name>
            <url>${exchange.url}</url>
            <layout>default</layout>
        </repository>
    </distributionManagement>

    <pluginRepositories>
        <pluginRepository>
            <id>mule-plugin</id>
            <name>Mule Repository</name>
            <url>https://repository.mulesoft.org/nexus/content/repositories/public/</url>
        </pluginRepository>
    </pluginRepositories>
</project>

Save it.

Leave YAML file and template file as it is.

This policy will print Hello World! on successful execution.(This configuration you can see in src/main/mule/template.xml)

for packaging of policy project:
execute maven command: mvn clean install

On successful execution of command target folder is created with jar file in it.

Now for deployment to Anypoint Exchange execute maven command: mvn deploy
On successful execution of the command, you can see policy project in the Exchange portal.

Now, you can see your custom policy in the policy section and you can apply the policy in your project.







Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER