kash
kash

Reputation: 199

Is it possible to programmatically add a new Mule Flow after the context has been initialized?

I would like to programmatically add new RSS Connector flows while Mule is running (after the context has been initialized). When I try to do this, I get a Lifecycle Exception saying that the context is already initialized.

Is there a way to do this without restarting the whole context?

Upvotes: 1

Views: 578

Answers (1)

kash
kash

Reputation: 199

I figured out a solution on my own. It turned out that creating a new Mule context, adding my flow, and then starting the context worked just fine. In fact, this ended up being simpler, faster, and cleaner than the other path I was going down.

Creating a default Mule context worked just fine for me. You might need to add a ConfigurationBuilder to yours if you have special needs.

MuleContext newMuleContext = new DefaultMuleContextFactory().createMuleContext();
MuleRegistry registry = newMuleContext.getRegistry();
Flow flow = createFlow();
registry.registerFlowConstruct(flow);
newMuleContext.start();

Edit. Here's the createFlow method. Your specifics will be different based on the needs of your app.

protected Flow createFlow(MuleContext context, RssBean feed) throws Exception {
  MuleRegistry registry = context.getRegistry();
  String feedName = feed.getName();
  HttpPollingConnector connector = getHttpPollingConnector(context, registry, feedName);
  EndpointURIEndpointBuilder endpointBuilder = getEndpointBuilder(context, feed, registry, shortName, connector);
  registry.registerEndpointBuilder(feedName + ".in", endpointBuilder);
  MessagePropertiesTransformer transformer = getTransformer(context, feedName);
  MessageProcessor mp = getOutboundFlowRef(context);
  Flow flow = getFlow(context, shortName, endpointBuilder, transformer, mp);
  registry.registerFlowConstruct(flow);
  return flow;
}

Upvotes: 1

Related Questions