-1

I want to create a new mule flow in runtime based on a flow-template. I found that Java module in mule 4 may help me with this, so in my main flow, I tried to invoke Java instance method:

<http:listener doc:name="Listener" doc:id="5b0f79ff-1a60-4c1a-bc9d-fbe350f4e595" config-ref="HTTP_Listener_config" path="/producer"/>

<java:new doc:name="New DynamicProducerFlowGenerator" doc:id="6c8cf087-0601-4b89-9d24-16650b9a6e6f" class="DynamicProducerFlowGenerator" constructor="DynamicProducerFlowGenerator()" target="DynamicProducerFlowGenerator"/>

<java:invoke doc:name="Invoke" doc:id="d5258c18-74e8-4c86-a0ae-758ad41d1b32" instance="#[vars.DynamicProducerFlowGenerator]" class="DynamicProducerFlowGenerator" method="initialise()"/>

To invoke this method, I created a Java class called DynamicProducerFlowGenerator, this is my Java code:

public class DynamicProducerFlowGenerator {

public DynamicProducerFlowGenerator() {

}



    public void initialise() {

            MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();                



    ConfigurationBuilder builder = new SpringXmlConfigurationBuilder("dynamic-producer-flow-template.xml");

            

    MuleContext muleContext = muleContextFactory.createMuleContext();

    builder.configure(muleContext);

    muleContext.start();

    }

}

However, when I tried to run this, I always got a error:

Message        : Invocation of Method 'initialise()' from Class 'DynamicProducerFlowGenerator' without any argument resulted in an error.

Cause: org.mule.api.MuleRuntimeException - Couldn't find config via SPI mechanism. Corrupted Mule core jar?

I think the problem lies in this line of code

"MuleContext muleContext = muleContextFactory.createMuleContext();"

But I do not know how to solve it. This is my some of dependencies in pom.xml:

 <dependency>

  <groupId>org.mule.tools.devkit</groupId>

  <artifactId>mule-devkit-annotations</artifactId>

  <version>3.9.7</version>

  <scope>provided</scope>

</dependency>

<dependency>

    <groupId>org.mule</groupId>

    <artifactId>mule-core</artifactId>

    <version>3.9.0</version>

</dependency>

    <dependency>

  <groupId>org.mule.module</groupId>

  <artifactId>mule-java-module</artifactId>

  <version>1.2.6</version>

  <classifier>mule-plugin</classifier>

</dependency>

<dependency>

    <groupId>org.mule.modules</groupId>

    <artifactId>mule-module-spring-config</artifactId>

    <version>3.9.0</version>

Could anyone help me with this? Is there another way that I can use to create a new flow in runtime in mule 4?

Thank you in advance

1 Answers1

0

First, your dependencies seem be mixing Mule 3 and Mule 4 dependencies. That will never work, they are incompatible. For example Devkit is a Mule 3 tool. If you are using a method intended for Mule 3, as it appears to be the case in your example, it will not work when used in Mule 3.

Secondly, I would ask what problem are you trying to resolve by creating flows dynamically. I'm not sure they are supported in Mule 4. I don't think it is good practice. Calling anything from the mule-core or similar Mule libraries inside a Java component is not a good practice for sure. For example accessing the Mule Context is not a good practice even in a connector.

I advise to define exactly what you need to achieve and try alternative methods that are better supported and recommended. For example, using the XML SDK, to create parametrized 'connectors' from flows, or Java SDK to create connectors and modules in Java.

aled
  • 21,330
  • 3
  • 27
  • 34