⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 muleclient.java

📁 提供ESB 应用mule源代码 提供ESB 应用mule源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     *            the result message     * @param timeout how long to block waiting to receive the event, if set to 0 the     *            receive will not wait at all and if set to -1 the receive will wait     *            forever     * @return the message received or <code>null</code> if no message was received     * @throws org.mule.api.MuleException     */    public MuleMessage request(String url, String transformers, long timeout) throws MuleException    {        return request(url, TransformerUtils.getTransformers(transformers), timeout);    }    /**     * Will receive an event from an endpointUri determined by the URL     *      * @param url the Mule URL used to determine the destination and transport of the     *            message     * @param transformers Transformers used to modify the result message     * @param timeout how long to block waiting to receive the event, if set to 0 the     *            receive will not wait at all and if set to -1 the receive will wait     *            forever     * @return the message received or <code>null</code> if no message was received     * @throws org.mule.api.MuleException     */    public MuleMessage request(String url, List transformers, long timeout) throws MuleException    {        MuleMessage message = request(url, timeout);        if (message != null && transformers != null)        {            message.applyTransformers(transformers);        }        return message;    }    /**     * Packages a mule event for the current request     *      * @param message the event payload     * @param uri the destination endpointUri     * @param synchronous whether the event will be synchronously processed     * @return the MuleEvent     * @throws MuleException     */    protected MuleEvent getEvent(MuleMessage message, String uri, boolean synchronous)        throws MuleException    {        ImmutableEndpoint endpoint = getOutboundEndpoint(uri);        if (!endpoint.getConnector().isStarted() && muleContext.isStarted())        {            endpoint.getConnector().start();        }        try        {            DefaultMuleSession session = new DefaultMuleSession(message,                ((AbstractConnector) endpoint.getConnector()).getSessionHandler(), muleContext);            if (user != null)            {                message.setProperty(MuleProperties.MULE_USER_PROPERTY, MuleCredentials.createHeader(                    user.getUsername(), user.getPassword()));            }            DefaultMuleEvent event = new DefaultMuleEvent(message, endpoint, session, synchronous);            return event;        }        catch (Exception e)        {            throw new DispatchException(CoreMessages.failedToCreate("Client event"), message, endpoint, e);        }    }    protected InboundEndpoint getInboundEndpoint(String uri) throws MuleException    {        // There was a potential leak here between get() and putIfAbsent(). This        // would cause the endpoint that was created to be used rather an endpoint        // with the same key that has been created and put in the cache by another        // thread. To avoid this we test for the result of putIfAbsent result and if        // it is non-null then an endpoint was created and added concurrently and we        // return this instance instead.        InboundEndpoint endpoint = (InboundEndpoint) inboundEndpointCache.get(uri);        if (endpoint == null)        {            endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(uri);            InboundEndpoint concurrentlyAddedEndpoint = (InboundEndpoint) inboundEndpointCache.putIfAbsent(uri, endpoint);            if (concurrentlyAddedEndpoint != null)            {                return concurrentlyAddedEndpoint;            }        }        return endpoint;    }    protected OutboundEndpoint getOutboundEndpoint(String uri) throws MuleException    {        // There was a potential leak here between get() and putIfAbsent(). This        // would cause the endpoint that was created to be used rather an endpoint        // with the same key that has been created and put in the cache by another        // thread. To avoid this we test for the result of putIfAbsent result and if        // it is non-null then an endpoint was created and added concurrently and we        // return this instance instead.        OutboundEndpoint endpoint = (OutboundEndpoint) outboundEndpointCache.get(uri);        if (endpoint == null)        {            endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(uri);            OutboundEndpoint concurrentlyAddedEndpoint = (OutboundEndpoint) outboundEndpointCache.putIfAbsent(uri, endpoint);            if (concurrentlyAddedEndpoint != null)            {                return concurrentlyAddedEndpoint;            }        }        return endpoint;    }    protected ImmutableEndpoint getDefaultClientEndpoint(Service service, Object payload)        throws MuleException    {        // as we are bypassing the message transport layer we need to check that        ImmutableEndpoint endpoint = (ImmutableEndpoint) service.getInboundRouter().getEndpoints().get(0);        if (endpoint != null)        {            if (endpoint.getTransformers() != null)            {                // the original code here really did just check the first exception                // as far as i can tell                if (TransformerUtils.isSourceTypeSupportedByFirst(endpoint.getTransformers(),                    payload.getClass()))                {                    return endpoint;                }                else                {                    EndpointBuilder builder = new EndpointURIEndpointBuilder(endpoint, muleContext);                    builder.setTransformers(new LinkedList());                    return muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(builder);                }            }            else            {                return endpoint;            }        }        else        {            EndpointBuilder builder = new EndpointURIEndpointBuilder("vm://mule.client", muleContext);            builder.setName("muleClientProvider");            endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(builder);        }        return endpoint;    }    /**     * Sends an event synchronously to a endpointUri via a Mule server without     * waiting for the result.     *      * @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 sendNoReceive(String url, Object payload, Map messageProperties) throws MuleException    {        if (messageProperties == null)        {            messageProperties = new HashMap();        }        messageProperties.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, "false");        MuleMessage message = new DefaultMuleMessage(payload, messageProperties);        MuleEvent event = getEvent(message, url, true);        try        {            event.getSession().sendEvent(event);        }        catch (MuleException e)        {            throw e;        }        catch (Exception e)        {            throw new DispatchException(ClientMessages.failedToDispatchClientEvent(), event.getMessage(),                event.getEndpoint(), e);        }    }    /**     * The overriding method may want to return a custom {@link MuleContext} here     *      * @return the UMOManager to use     */    public MuleContext getMuleContext()    {        return muleContext;    }    /**     * Registers a Java object as a component that listens for events on the     * given URL. By default the ThreadingProfile for the components will be set so     * that there will only be one thread of execution.     *      * @param component any java object, Mule will it's endpointUri discovery to     *            determine which event to invoke based on the evnet payload type     * @param name The identifying name of the components. This can be used to later     *            unregister it     * @param listenerEndpoint The url endpointUri to listen to     * @throws MuleException     * @deprecated Use the RegistryContext to get the registry and register the     *             service there     */    public void registerComponent(Object component, String name, EndpointURI listenerEndpoint)        throws MuleException    {        throw new UnsupportedOperationException("registerComponent");        // builder.registerComponentInstance(service, name, listenerEndpoint,        // null);    }    /**     * Registers a Java object as a component that listens for and sends events     * on the given urls. By default the ThreadingProfile for the components will be     * set so that there will only be one thread of execution.     *      * @param component any java object, Mule will it's endpointUri discovery to     *            determine which event to invoke based on the evnet payload type     * @param name The identifying name of the components. This can be used to later     *            unregister it     * @param listenerEndpoint The url endpointUri to listen to     * @param sendEndpoint The url endpointUri to dispatch to     * @throws MuleException     * @deprecated Use the RegistryContext to get the registry and register the     *             service there     */    public void registerComponent(Object component,                                  String name,                                  MuleEndpointURI listenerEndpoint,                                  MuleEndpointURI sendEndpoint) throws MuleException    {        throw new UnsupportedOperationException("registerComponent");        // builder.registerComponentInstance(service, name, listenerEndpoint,        // sendEndpoint);    }    /**     * Registers a user configured MuleDescriptor of a components to the server. If     * users want to register object instances with the server rather than class     * names that get created at runtime or reference to objects in the container,     * the user must call the descriptors setImplementationInstance() method - <code>     * MyBean implementation = new MyBean();     * descriptor.setImplementationInstance(implementation);     * </code>     * Calling this method is equivilent to calling Model.registerComponent(..)     *      * @param descriptor the componet descriptor to register     * @throws MuleException the descriptor is invalid or cannot be initialised or     *             started     * @see org.mule.api.model.Model     * @deprecated Use the RegistryContext to get the registry and register the     *             service there     */    // public void registerComponent(UMODescriptor descriptor) throws MuleException    // {    // throw new UnsupportedOperationException("registerComponent");    // //builder.registerComponent(descriptor);    // }    /**     * Unregisters a previously register components. This will also unregister any     * listeners for the components Calling this method is equivilent to calling     * Model.unregisterComponent(..)     *      * @param name the name of the componet to unregister     * @throws MuleException if unregistering the components fails, i.e. The     *             underlying transport fails to unregister a listener. If the     *             components does not exist, this method should not throw an     *             exception.     * @see org.mule.api.model.Model     * @deprecated Use the RegistryContext to get the registry and unregister the     *             service there     */    public void unregisterComponent(String name) throws MuleException    {        throw new UnsupportedOperationException("registerComponent");        // builder.unregisterComponent(name);    }    public RemoteDispatcher getRemoteDispatcher(String serverEndpoint) throws MuleException    {        RemoteDispatcher rd = new RemoteDispatcher(serverEndpoint);        rd.setExecutor(muleContext.getWorkManager());        dispatchers.add(rd);        return rd;    }    public RemoteDispatcher getRemoteDispatcher(String serverEndpoint, String user, String password)        throws MuleException    {        RemoteDispatcher rd = new RemoteDispatcher(serverEndpoint, new MuleCredentials(user,            password.toCharArray()));        rd.setExecutor(muleContext.getWorkManager());        dispatchers.add(rd);        return rd;    }    /**     * Will dispose the MuleManager instance <b>if</b> a new instance was created for this     * client. Otherwise this method only cleans up resources no longer needed     */    public void dispose()    {        synchronized (dispatchers)        {            for (Iterator iterator = dispatchers.iterator(); iterator.hasNext();)            {                RemoteDispatcher remoteDispatcher = (RemoteDispatcher) iterator.next();                remoteDispatcher.dispose();                remoteDispatcher = null;            }            dispatchers.clear();        }        // Dispose the muleContext only if the muleContext was created for this        // client        if (muleContext.getConfiguration().isClientMode())        {            logger.info("Stopping Mule...");            muleContext.dispose();        }    }    public void setProperty(String key, Object value)    {        try        {            muleContext.getRegistry().registerObject(key, value);        }        catch (RegistrationException e)        {            logger.error(e);        }    }    public Object getProperty(String key)    {        return muleContext.getRegistry().lookupObject(key);    }    public MuleConfiguration getConfiguration()    {        return muleContext.getConfiguration();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -