You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

ServiceMix Bean

The ServiceMix Bean component provides integration with beans (POJOs) with the JBI bus to make it easy to use POJOs to process JBI message exchanges.

Deployment

Currently (v 3.1), servicemix-bean supports two different deployment models. The first one uses an xbean.xml configuration file where one can configure the different endpoints / beans that will be used. The other one only works with a static configuration file (servicemix.xml) and can not be used with standard JBI packaging but allows automatic detection of the beans to expose.

xbean.xml

<beans xmlns:bean="http://servicemix.apache.org/bean/1.0">

  <bean:endpoint service="test:service" endpoint="endpoint" bean="#listenerBean"/>

  <bean id="listenerBean" class="org.apache.servicemix.bean.beans.ListenerBean"/>

</beans>

Static configuration

When used in a static configuration, beans can be automatically discovered amongst spring configured beans:

<beans xmlns:sm="http://servicemix.apache.org/config/1.0"
       xmlns:bean="http://servicemix.apache.org/bean/1.0"
       xmlns:test="urn:test">

  <sm:container id="jbi" embedded="true" createMBeanServer="false">
    <sm:activationSpecs>
      <sm:activationSpec>
        <sm:component>
          <bean:component/>
        </sm:component>
      </sm:activationSpec>
    </sm:activationSpecs>
  </sm:container>

  <bean id="consumerBean" class="org.apache.servicemix.bean.beans.ConsumerBean"/>
  <bean id="listenerBean" class="org.apache.servicemix.bean.beans.ListenerBean"/>
  <bean id="annotatedBean" class="org.apache.servicemix.bean.beans.AnnotatedBean"/>
  <bean id="plainBean" class="org.apache.servicemix.bean.beans.PlainBean"/>

</beans>

Such beans can be accessed by resolving a URI:

DocumentFragment epr = URIResolver.createWSAEPR("bean:annotatedBean");
ServiceEndpoint se = client.getContext().resolveEndpointReference(epr);
exchange.setEndpoint(se);

Beans can also be discovered by searching within defined packages:

<beans xmlns:sm="http://servicemix.apache.org/config/1.0"
       xmlns:bean="http://servicemix.apache.org/bean/1.0"
       xmlns:test="urn:test">

  <sm:container id="jbi" embedded="true" createMBeanServer="false">
    <sm:activationSpecs>
      <sm:activationSpec>
        <sm:component>
          <bean:component searchPackages="org.apache.servicemix.bean.beans"/>
        </sm:component>
      </sm:activationSpec>
    </sm:activationSpecs>
  </sm:container>

</beans>

In such a case, beans must have the @Endpoint annotation.

Of course, you can use the endpoint xml element to configure your POJOs:

<beans xmlns:sm="http://servicemix.apache.org/config/1.0"
       xmlns:bean="http://servicemix.apache.org/bean/1.0"
       xmlns:test="urn:test">

  <sm:container id="jbi" embedded="true" createMBeanServer="false">
    <sm:activationSpecs>
      <sm:activationSpec>
        <sm:component>
          <bean:component>
            <bean:endpoints>
              <bean:endpoint service="test:service" endpoint="endpoint" bean="#listenerBean">
            </bean:endpoints>
          <bean:component>
        </sm:component>
      </sm:activationSpec>
    </sm:activationSpecs>
  </sm:container>

  <bean id="listenerBean" class="org.apache.servicemix.bean.beans.ListenerBean"/>

</beans>

Beans

The servicemix-bean component can accept different kind of POJOs. These POJOs may be annotated to customize their behavior. All the following annotations belong to the org.apache.servicemix.bean package.

Annotation

Target

Description

Callback

Method

 

Content

Parameter

 

Correlation

Type

 

Endpoint

Type

This annotation is mandatory if the bean is automatically searched from a list of packages.

ExchangeTarget

Field

 

Operation

Method

 

Property

Parameter

 

XPath

Parameter

 

In addition, standard annotations can be used:

Annotation

Target

Description

Resource

Field

The Resource annotation marks a resource that is needed by the application. Currently, this annotation is only supported on fields of type ComponentContext and DeliveryChannel.

PostConstruct

Method

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

PreDestroy

Method

The PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container.

Examples

URI

You can use a handy URI to refer to beans using the Spring names of beans.

bean:someName

The above would refer to the bean named someName in the Spring ApplicationContext which is used to boot up the bean component.

  • No labels