---
title: "Mule ESB的component（二）——Component的使用"
author: "winnie825"
date: "2012-11-27T16:20:56Z"
tags: []
categories: ["企业架构"]
slug: "mule-esb-component-component"
draft: false
lastmod: "2012-11-27T16:20:56Z"
id: 1736072
url: "https://winnie825.iteye.com/blog/1736072"
---

**一 Mule自带的component**

打开mule的核心jar包mule-core-3.0.0.jar,可以看到如下结构，期中simple中的component即mule自带的component：

![](http://dl.iteye.com/upload/attachment/0077/1309/cb855a0f-0703-31d8-8ec7-214e9db9e13b.jpg)

 

    **1. bridge component**

    mule中的每一个service中都会包含一个bridge component，它被隐式的引用了：

![](http://dl.iteye.com/upload/attachment/0077/1311/f6257840-9482-3925-acd3-5d3496da5137.jpg)

     定义：This is the bridge component. As its name suggests, it bridges its inbound router to its outbound router, but doesn’t perform any particular operation on the message.

    从定义中看，bridge component的作用是将message从inbound传递到outbound中，期间它对message没有任何操作。

 

    在《Mule-ESB-3-User-Guide》中找到一段关于PassThroughComponent的描述，bridge component的默认实现类应该就是PassThroughComponent：

```
Service Bridge
Service component configuration is optional in Mule 2.x. The default and implicit component used is PassThroughComponent . This component
automatically bridges inbound messages to the outbound phase and simply passes messages to the outbound routers. This approach is useful for
bridging endpoints if you want to pass a message from one transport to another.
```

 

    **2. echo component和log component**

    两者都是为了记录信息，从源码中分析，两者的关联是EchoComponent extends LogComponent，前者调用父类的方法进行日志输出。两者的差别是：echo将message的信息全部输出，而log会默认截取前100个长度的信息。

 

    使用方法如下图，log component使用&lt;log-component /&gt;替换&lt;echo-component /&gt;：

![](http://dl.iteye.com/upload/attachment/0077/1313/1c92dfe6-32ae-3f48-a677-e9657eca210f.jpg)

 

    **3. null component**

    从源码中可以看到，当NullComponent接收到信息时，会抛出一个异常：

```
package org.mule.component.simple;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

/**
 * <code>NullComponent</code> is a service that is used as a placeholder. This
 * implementation will throw an exception if a message is received for it.
 */
public class NullComponent implements Callable
{

    public Object onCall(MuleEventContext context) throws Exception
    {
        throw new UnsupportedOperationException("This service cannot receive messages. Service is: "
                                                + context.getFlowConstruct().getName());
    }

}
```

 

使用方法

![](http://dl.iteye.com/upload/attachment/0077/1315/3412d94f-1b1b-38a8-87e7-1b14e84d6a71.jpg)

    **4. StaticComponent**

    这个没有找到先关的资料，从api中可以看到该类的描述:

    A service that will return a static data object as a result. This is useful for testing with expected results. The data returned can be read from a file or set as a property on this service.

 

    这个component应该是mule用于测试时设置期望数据的组件。

 

二 远程调用

    mule支持远程调用，它支持RPC和REST两类调用，由于对这两块技术是空白，所以大家自己研究吧= =！