Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Java Component Reference

The Java component enables the developer to package custom Java code that executes when the component receives a message. The Java component can be used to enhance the functionality and capability of your web-based applications written in Java.

If all you really want to do with your java class is to modify the Payload of the message in some way, you’re probably better off using a Java Transformer rather than a Java Component.

Configuration

To configure the Java component, selecting a class is the only required entry. The class is simply a collection of properties, attributes and methods the component applies to all messages. In addition, this component allows you to optionally configure Singleton (object factory that creates an object instance) and Spring (object factory used to obtain Spring bean instances) object factories, which are invoked when a message is received. Singleton is simply the mode used to deploy the bean. The singleton object factory does not create a new instance for each request; instead, the instance is shared. On the other hand, the Spring object factory does not create any instances; idt simply looks them up from Spring.

Lastly, the Java component allows you to configure interceptors. An interceptor enables the developer to provide additional synchronous that are executed on messages received by the Java component. Use interceptors to act on received messages, to log errors or exceptions, apply some business logic, or invoke additional Java services.

If the application referencing Java expects a response, use a request-response connector to enable the Java component to communicate in a two-way fashion (see below).

java+component

If Java is used to simply pass data to the next building block in the flow, the transaction is referred to as one-way. No response is required from the client.

General Tab

The General tab lets you change the display name, select a class and enter the spring bean information this component will reference. To configure the Java component, from the Message Flow canvas double-click the component to open the Properties Editor. Use the browse button to select a class for the component. Selecting a class is a required setting.

java_general

Field Description

Display Name

Defaults to the generic component name. Change the Display Name, which must be alpha-numeric, to reflect the transformer’s specific role, i.e. Custom Java Component.

Class Name

A class is a collection of properties, attributes and methods referenced by the component. Use the ellipses button to select a class. Type the first few characters of the class name to narrow the list of choices.

Object

This parameter allows the developer to define singleton and spring bean objects. When using a singleton bean, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching this bean definition will result in this specific bean instance being returned. To configure a singleton bean, select an appropriate class that contains the required properties, attributes and methods. Configure the spring bean object to define the class that will be used to obtain the spring bean instances. The class attribute tells Spring which class to instantiate.

java_object

Advanced Tab

The Advanced tab lets you manage interceptors and select the object factory to be used by the component. An interceptor contains the business logic that is applied to the message payload before being sent to the next building block. You can add the following types of interceptors to the Java component:

  • Custom interceptor

  • Logging interceptor

  • Timer interceptor

If necessary, you can add an Abstract Lifecycle Adapter Factory, which enables the object to reference an alternative class when required. When configuring this factory it is required to select a class. In addition, you can use this panel to define the Spring bean property elements.

java_advanced

Panel Description

Interceptors Management

Interceptors enable the developer to provide additional services to the component such as the ability to log transactions and the ability to log the time for each transaction. Use the Add Custom Interceptor to create a custom interceptor that can be referenced by Spring bean objects. The Interceptor Stack enables you to bundle multiple interceptors. Use the Interceptor Stack to apply multiple interceptors on this Java component. The interceptors will be applied in the order defined in the stack.

Object Factory

A collection of properties, attributes and classes referenced by the component. The object factory must contain a class. To configure the object factory, select the plus (plus) button then use the browse button to select a class. Configure the generic panel to enter spring bean element information.

advanced_custom

Field Description

Name

Enter the property name of the spring bean instance.

Value

Enter the value of the spring bean instance. e.g., it could be a string or Boolean. This spring instance can have a value or the spring instance can reference another spring bean.

Reference

Enter the name of the spring bean you want to reference. The value field is not used if you are referencing another spring bean.

Basic Hello World Java Component Class

To create a brand new Java Class, follow these steps: 

  1. In the package explorer right click on your project and select New > Class

  2. Name your class, and make the package org.mule.transformers

  3. You can use the basic class as a skeleton to construct a simple Java Component:

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    package org.mule.transformers;
    import org.mule.api.MuleEventContext;
    import org.mule.api.MuleMessage;
    import org.mule.api.lifecycle.Callable;
    import org.mule.api.transformer.TransformerException;
    import org.mule.transformer.AbstractMessageTransformer;
    
    public class helloWorldComponent implements Callable{
    @Override
        public Object onCall(MuleEventContext eventContext) throws Exception {
    
            eventContext.getMessage().setInvocationProperty("myProperty", "Hello World!");
            return eventContext.getMessage().getPayload();
        }
    }

    Your class doesn’t necessarily need to extend the Callable class.

    Note that what you set as the return argument will become the output payload of your transformer. In this case, the outbound payload is set to the inbound payload to avoid transforming it.

  4. Drag a new Java Component into your flow, and set the Class Name field to reference your newly created class.

    java+component

    Or in the XML view, add a component element, and reference the Java class in the class attribute:

    
                
             
    1
    
    <component doc:name="Java" class="org.mule.transformers.helloWorldComponent"/>