Question

I wanted to do some stuff before any mule application starts. As per the Mule documentation we can write a custom Agent for this. However, my agent is not loading when mule starts.

Can anyone help me in providing an example on how to create a custom agent.

I have created a test program for the same as shown below

CustomAgent Class

package agent;

import org.mule.api.MuleException;
import org.mule.api.agent.Agent;
import org.mule.api.lifecycle.InitialisationException;

public class CustomAgent implements Agent{

    public CustomAgent() {

    }

    private String port;

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    @Override
    public void initialise() throws InitialisationException {
        System.out.println("Initialize on port: " + port);

    }

    @Override
    public void start() throws MuleException {
        System.out.println("Started on port: " + port);

    }

    @Override
    public void stop() throws MuleException {
        System.out.println("Stopping the agent");

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

    @Override
    public void setName(String name) {
        this.port = name;       
    }

    @Override
    public String getName() {
        return port;
    }

    @Override
    public String getDescription() {
        // TODO Auto-generated method stub
        return getName();
    }


}

This is my CustomAgent.xml file

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.3.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
    <custom-agent name="mycustomagent" class="agent.AgentTest">
        <spring:property name="port" value="8899"></spring:property>
    </custom-agent>
</mule>

I tried exporting a jar file and putting in

MULE_HOME\lib\opt

and

MULE_HOME\lib\user

but its not getting loaded as an agent.

Please help!

Was it helpful?

Solution

You have two options to run the agent in Mule, depending on what's your actual use case:
a. You deploy it as a regular Mule application, i.e., a zip file with the appropriate structure, containing the XML configuration and the agent's class.
b. You package it as a jar and you declare the agent configuration in another Mule application, i.e., including this in that other Mule app:

<custom-agent name="mycustomagent" class="agent.AgentTest">
    <spring:property name="port" value="8899"></spring:property>
</custom-agent>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top