配置组件(Configuring Components)

服务组件将包含业务逻辑来处理来自Mule ESB的信息,一个服务组件通常可以是以下类型中的任何一种,它包括了Spring bean对象,

POJO,SCRIPT,Webservice,或者REST CALL。因为这些都是对于你的实现已经是非常具体的了,你通常可以创建客户化的组件,

或者简单的使用已经存在的POJO,Mule为您提供了一些标准组件或者你可以继承这些组件来扩展你的需求,这个章节将介绍如何

配置各种类型的组件。

看更加详细的配置信息你可以参考这个Component Configuration Reference.

Mule提供的一些简单的组件(Simple Components)

Configuration Element Description
<log-component/> Logs component invocations, outputting the received message as a string. This component does not return a response.
<echo-component/>

Extends the log component to log and echo the incoming message. The message is transformed before being returned,

so transformations on inbound endpoints will be applied.

<null-component/> Throws an exception when invoked. This is useful for testing use cases that use a forwarding consumer inbound router.
<passthrough-component>

Similar to the echo component but does not log. This component is useful when defining services that consist of inbound

and outbound endpoints/routers but don't have a component implementation. Note that explicitly configuring this component

has exactly the same result as configuring a service with no component.

<bridge-component/> Identical to the pass-through component but preserves the Mule 1.4 terminology.
<test:component/>

Configures the Mule FunctionalTestComponent, which allows more complex testing scenarios to be created. For more information,

see Functional Testing.

java组件(Java Components)

java组件指定一个java类来作为服务组件或者是配置一个容器中已存在的引用如spring组件,它们同时还配置了Mule管理服务组件的生命周期,

调用,和管理实例池。java组件能够通过服务组件实现类名快速简单的配置在<component> 或者 <pooled-component>元素中。

这个<pooled-component>元素能够为你的服务建立一个概要池。在以上两种情况中,PrototypeObjectFactory将默认被使用并且将为每一次的

请求或者池中的新建对象做实例化。

<component class="org.my.ServiceComponentImpl"/>

...

<pooled-component class="org.my.ServiceComponentImpl"/>

另外你可以明确指定对象工厂,正如SingletonObjectFactory创建单例对象。

<component>

  <singleton-object class="org.my.ServiceComponentImpl"/>

</component>

如果你添加拦截器到组件中的话这个明确的语法将被替换为<component class>语法。更多更详细的配置信息可以参考Configuring Java Components.

其他组件(Other Components)

还有许多其他可用组件允许你使用不同技术来作为服务组件,这些组件通常被包括在transports 或者 modules.

Configuration Description
<http:rest-service-component/> Proxies a remote call to a REST-style web service.
<cxf:wrapper-component/> Proxies a remote call to a web service using CXF.
<script:component/> Configures a JSR-223 script for the service component.
使用拦截器自定义行为(Customizing Behavior with Interceptors)
Mule拦截器对于连接多个服务器组件是非常有用的,拦截器模式常常被称为实用的AOP面向方面编程),因为它允许开发人员拦截的对象处理和可能改变处理工作
及结果。更全面的配置信息请参考
Using Interceptors
生命周期(Lifecycle)
组件和其他任何一个注册在Mule中的对象一样有生命周期,它被注册是通过添加一个或者多个生命周期拦截器到你的组件中。从Mule 3.0之后可以通过JSR-250注解
方式来配置初始化和销毁方法。下面的列表描述了组件的生命周期的各个阶段:
Lifecycle Description Interface Annotation
initialise

The first lifecycle method called once any injectors on the component have been called.

This means any properties on the component will be set before the initialise lifecycle is called.

org.mule.api.lifecycle.Initialisable #javax.annotation.PostConstruct
start This is called when the MuleContext is started. org.mule.api.lifecycle.Startable  
stop This is called when the MuleContext is stopped, or the service that owns this component is stopped. org.mule.api.lifecycle.Stoppable  
dispose

Called as the object is being disposed off. Typically this happens because either the MuleContext

is shutting down or the service that wraps this component was unregistered.

org.mule.api.lifecycle.Disposible

#javax.annotation.PreDestroy

生命周期的例子
完整生命周期:
org.my.ServiceComponentImpl implements org.my.ServiceComponent, org.mule.api.lifecycle.Lifecycle
{
public void initialise() throws InitialisableException { }

public void start() throws MuleException { }

public void stop() throws MuleException { }

public void dispose() { }
}
仅有初始化的生命周期:
org.my.ServiceComponentImpl implements org.my.ServiceComponent, org.mule.api.lifecycle.Initialisable
{
public void initialise() throws InitialisableException { }
}
通过JSR-250注解方式来初始化或者销毁的生命周期:
org.my.ServiceComponentImpl implements org.my.ServiceComponent
{
@PostConstruct
public void init() { }

@PreDestroy
public void destroy() { }
}
posted @ 2011-03-31 14:21  wuwenyu  阅读(178)  评论(0)    收藏  举报