📄 muleclient.java
字号:
/* * $Id: MuleClient.java 11968 2008-06-06 04:06:18Z dfeist $ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */package org.mule.module.client;import org.mule.DefaultMuleEvent;import org.mule.DefaultMuleMessage;import org.mule.DefaultMuleSession;import org.mule.MuleServer;import org.mule.api.FutureMessageResult;import org.mule.api.MessagingException;import org.mule.api.MuleContext;import org.mule.api.MuleEvent;import org.mule.api.MuleException;import org.mule.api.MuleMessage;import org.mule.api.MuleSession;import org.mule.api.config.ConfigurationBuilder;import org.mule.api.config.ConfigurationException;import org.mule.api.config.MuleConfiguration;import org.mule.api.config.MuleProperties;import org.mule.api.context.MuleContextBuilder;import org.mule.api.endpoint.EndpointBuilder;import org.mule.api.endpoint.EndpointURI;import org.mule.api.endpoint.ImmutableEndpoint;import org.mule.api.endpoint.InboundEndpoint;import org.mule.api.endpoint.OutboundEndpoint;import org.mule.api.lifecycle.Disposable;import org.mule.api.lifecycle.InitialisationException;import org.mule.api.registry.RegistrationException;import org.mule.api.service.Service;import org.mule.api.transport.DispatchException;import org.mule.api.transport.ReceiveException;import org.mule.config.DefaultMuleConfiguration;import org.mule.config.i18n.CoreMessages;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextBuilder;import org.mule.context.DefaultMuleContextFactory;import org.mule.endpoint.EndpointURIEndpointBuilder;import org.mule.endpoint.MuleEndpointURI;import org.mule.module.client.i18n.ClientMessages;import org.mule.security.MuleCredentials;import org.mule.transformer.TransformerUtils;import org.mule.transport.AbstractConnector;import org.mule.transport.NullPayload;import org.mule.util.StringUtils;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import edu.emory.mathcs.backport.java.util.concurrent.Callable;import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * <code>MuleClient</code> is a simple interface for Mule clients to send and * receive events from a Mule Server. In most Mule applications events are triggered * by some external occurrence such as a message being received on a queue or a file * being copied to a directory. The Mule client allows the user to send and receive * events programmatically through its API. * <p> * The client defines a {@link EndpointURI} which is used to determine how a message is * sent of received. The url defines the protocol, the endpointUri destination of the * message and optionally the endpoint to use when dispatching the event. For * example: * <p> * <code>vm://my.object</code> dispatches to a <code>my.object</code> destination * using the VM endpoint. There needs to be a global VM endpoint registered for the * message to be sent. * <p> * <code>jms://jmsProvider/orders.topic</code> dispatches a JMS message via the * globally registered jmsProvider over a topic destination called * <code>orders.topic</code>. * <p> * <code>jms://orders.topic</code> is equivalent to the above except that the * endpoint is determined by the protocol, so the first JMS endpoint is used. * <p> * Note that there must be a configured MuleManager for this client to work. It will * use the one available using <code>muleContext</code> * * @see org.mule.endpoint.MuleEndpointURI */public class MuleClient implements Disposable{ /** * logger used by this class */ protected static final Log logger = LogFactory.getLog(MuleClient.class); /** * the local UMOManager instance */ private MuleContext muleContext; private List dispatchers = new ArrayList(); private MuleCredentials user; private DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); private ConcurrentMap inboundEndpointCache = new ConcurrentHashMap(); private ConcurrentMap outboundEndpointCache = new ConcurrentHashMap(); /** * Creates a Mule client that will use the default serverEndpoint when connecting to a remote * server instance. * * @throws MuleException */ public MuleClient() throws MuleException { this(true); } public MuleClient(boolean startContext) throws MuleException { init(startContext); } public MuleClient(MuleContext context) throws MuleException { this.muleContext = context; init(false); } /** * Configures a Mule client instance using the the default * {@link SpringXmlConfigurationBuilder} to parse <code>configResources</code>. * * @param configResources a config resource location to configure this client * with * @throws ConfigurationException if there is a {@link MuleContext} instance already * running in this JVM or if the builder fails to configure the * Manager */ public MuleClient(String configResources) throws MuleException { this(configResources, new SpringXmlConfigurationBuilder(configResources)); } /** * Configures a new Mule client and either uses an existing Manager running in * this JVM or creates a new empty {@link MuleContext} * * @param user the username to use when connecting to a remote server instance * @param password the password for the user * @throws MuleException */ public MuleClient(String user, String password) throws MuleException { init(/* startManager */true); this.user = new MuleCredentials(user, password.toCharArray()); } /** * Configures a Mule client instance * * @param configResources a config resource location to configure this client * with * @param builder the configuration builder to use * @throws ConfigurationException is there is a {@link MuleContext} instance already * running in this JVM or if the builder fails to configure the * Manager * @throws InitialisationException */ public MuleClient(String configResources, ConfigurationBuilder builder) throws ConfigurationException, InitialisationException { if (builder == null) { logger.info("Builder passed in was null, using default builder: " + SpringXmlConfigurationBuilder.class.getName()); builder = new SpringXmlConfigurationBuilder(configResources); } logger.info("Initializing Mule..."); muleContext = muleContextFactory.createMuleContext(builder); } /** * Configures a Mule client instance * * @param configResources a config resource location to configure this client * with * @param builder the configuration builder to use * @param user the username to use when connecting to a remote server instance * @param password the password for the user * @throws ConfigurationException is there is a {@link MuleContext} instance already * running in this JVM or if the builder fails to configure the * Manager * @throws InitialisationException */ public MuleClient(String configResources, ConfigurationBuilder builder, String user, String password) throws ConfigurationException, InitialisationException { this(configResources, builder); this.user = new MuleCredentials(user, password.toCharArray()); } /** * Initialises a default {@link MuleContext} for use by the client. * * @param startManager start the Mule context if it has not yet been initialised * @throws MuleException */ private void init(boolean startManager) throws MuleException { // if we are creating a server for this client then set client mode // this will disable Admin connections by default; // If there is no local muleContext present create a default muleContext if (muleContext == null) { muleContext = MuleServer.getMuleContext(); } if (muleContext == null) { logger.info("No existing ManagementContext found, creating a new Mule instance"); MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder(); DefaultMuleConfiguration config = new DefaultMuleConfiguration(); config.setClientMode(true); contextBuilder.setMuleConfiguration(config); muleContext = muleContextFactory.createMuleContext(contextBuilder); } else { logger.info("Using existing MuleContext: " + muleContext); } if (!muleContext.isStarted() && startManager == true) { logger.info("Starting Mule..."); muleContext.start(); } } /** * Dispatches an event asynchronously to a endpointUri via a Mule server. The URL * determines where to dispatch the event to. * * @param url the Mule URL used to determine the destination and transport of the * message * @param payload the object that is the payload of the event * @param messageProperties any properties to be associated with the payload. In * the case of JMS you could set the JMSReplyTo property in these * properties. * @throws org.mule.api.MuleException */ public void dispatch(String url, Object payload, Map messageProperties) throws MuleException { dispatch(url, new DefaultMuleMessage(payload, messageProperties)); } /** * Dispatches an event asynchronously to a endpointUri via a Mule server. The URL * determines where to dispatch the event to. * * @param url the Mule URL used to determine the destination and transport of the * message * @param message the message to send * @throws org.mule.api.MuleException */ public void dispatch(String url, MuleMessage message) throws MuleException { MuleEvent event = getEvent(message, url, false); try { event.getSession().dispatchEvent(event); } catch (MuleException e) { throw e; } catch (Exception e) { throw new DispatchException(ClientMessages.failedToDispatchClientEvent(), event.getMessage(), event.getEndpoint(), e); } } /** * Sends an event synchronously to a component * * @param component the name of the Mule component to send to * @param transformers a comma separated list of transformers to apply to the * result message * @param payload the object that is the payload of the event * @param messageProperties any properties to be associated with the payload. as * null * @return the result message if any of the invocation * @throws org.mule.api.MuleException if the dispatch fails or the components or * transfromers cannot be found */ public MuleMessage sendDirect(String component, String transformers, Object payload, Map messageProperties) throws MuleException { MuleMessage message = new DefaultMuleMessage(payload, messageProperties); return sendDirect(component, transformers, message); } /** * Sends an event synchronously to a component * * @param componentName the name of the Mule component to send to * @param transformers a comma separated list of transformers to apply to the * result message * @param message the message to send * @return the result message if any of the invocation * @throws org.mule.api.MuleException if the dispatch fails or the components or * transfromers cannot be found */ public MuleMessage sendDirect(String componentName, String transformers, MuleMessage message) throws MuleException { Service service = muleContext.getRegistry().lookupService(componentName); if (service == null) { throw new MessagingException(CoreMessages.objectNotRegistered("Service", componentName), message); } List trans = null; if (transformers != null) { trans = TransformerUtils.getTransformers(transformers); } if (!muleContext.getConfiguration().isDefaultSynchronousEndpoints()) { logger.warn("The mule muleContext is not running synchronously, a null message payload will be returned"); } MuleSession session = new DefaultMuleSession(service, muleContext); ImmutableEndpoint endpoint = getDefaultClientEndpoint(service, message.getPayload()); MuleEvent event = new DefaultMuleEvent(message, endpoint, session, true); if (logger.isDebugEnabled()) { logger.debug("MuleClient sending event direct to: " + componentName + ". MuleEvent is: " + event); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -