DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Refer to a Connector Configuration From a Java Component [Snippet]

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.

Sanjeet Pandey user avatar by
Sanjeet Pandey
CORE ·
Dec. 08, 17 · Java Zone · Code Snippet
Like (2)
Save
Tweet
6.38K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Connector (mathematics) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Do Microcontainers Matter in Your Enterprise?
  • CSS Position: Relative vs Position Absolute
  • Cypress: The Future of Test Automation! Advantages and Disadvantages
  • Functional vs. Non-Functional Requirements: The Full Guide, Definitions, and Technical Examples

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo