Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Refer to a Connector Configuration From a Java Component [Snippet]

DZone's Guide to

Refer to a Connector Configuration From a Java Component [Snippet]

When using a Java component in Mule to send messages, you can run into trouble if there is more than one connector configuration. Here's the solution.

· Java Zone ·
Free Resource

Start coding something amazing with our library of open source Cloud code patterns. Content provided by IBM.

When using a Java component to send a message to a VM inbound endpoint, if there is more than one connector configuration defined, then it is necessary to specify the connector reference to use. Otherwise, an error similar to the one below will be shown:

org.mule.transport.service.TransportFactoryException: 
There are at least 2 connectors matching protocol "vm", 
so the connector to use must be specified on the endpoint 
using the 'connector' property/attribute. Connectors in your 
configuration that support "vm" are: VM-1, VM-2, 
(java.lang.IllegalStateException). Component that caused 
exception is: DefaultJavaComponent{vm-javaFlow.component.2140635066} 


When sending a message via the LocalMuleClient class, you can specify the connector configuration reference via the URL parameter to the dispatch method. For example, assuming a connector configuration reference named "VM-1", then the code to send a message to a VM queue using that configuration would look like below:

package com.mulesoft.support;

import java.util.HashMap;
import java.util.Map;

import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.client.LocalMuleClient;
import org.mule.api.lifecycle.Callable;
import org.mule.api.transport.PropertyScope;

public class VMTester implements Callable {

    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {

        MuleMessage message = eventContext.getMessage();
        LocalMuleClient client = eventContext.getMuleContext().getClient();

        Map < String, Object > map = new HashMap < String, Object > ();
        map.put("flowname", "myFlow");
        map.put("messageid", "myID");
        map.put("input", "myInput");

        message.addProperties(map, PropertyScope.OUTBOUND);
        client.dispatch(("vm://messageQueue?connector=VM-1"), message);
        return message;

    }
}


Here is a link to a sample Mule Deployable Archive application using the code above.

Use this tool to look at the contents of GitHub and classify code based on the programming language used.  Content provided by IBM Developer.

Topics:
mulesoft ,java ,connector

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}