Mule School: Invoking Java Component over HTTP

March 21 2012

7 comments 0
motif

Since Mule is built on and Spring, it has native integration capabilities to invoke and Spring components. In this tutorial, we shall learn how to pass request received from endpoint on to Java component and receive response.

Please complete Hello World lesson from last week before proceeding further.

First step, create a new project as described in the Hello World tutorial. Name the flow “java”.

Drag and drop the “HTTP” endpoint and “Echo” component as described in Hello World lesson. Double click on HTTP (Inbound Endpoint) and specify “java” for path field

.

Create a new Java class by clicking on File -> New -> Class.

package com..java;
 
public class JavaMule {
  public String sayHello(String name) {
    return "Hello " + name;
  }
}

Drag and drop a “Java” component between HTTP (Inbound Endpoint) and Echo component. Double click on “Java” component to bring up properties dialog. Click the + symbol next to “Object” field and select “core:singleton-object” and com.mulesoft.java.JavaMule for object type and class respectively. Click OK to save and close.

Open “Configuration XML” tab to see generated XML content.

<mule ...>
  <flow name="javaFlow1" doc:name="javaFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="java" doc:name="HTTP"/>
    <component doc:name="Java">
      <singleton-object/>
    </component>
    <echo-component doc:name="Echo"/>
  </flow>
</mule>

Hit Save All and run “java.mflow” as Mule Application (right click on the file). Access http://localhost:8081/java from browser to see “Hello /java” output. If you carefully observe, we didn’t explicitly specify which method to invoke from JavaMule class. Through reflection, Mule automatically tries to identify relevant Java method. Sometimes, we may need to explicitly map request to specific methods. We are going to explore how to do this in subsequent lessons.


We'd love to hear your opinion on this post

7 Responses to “Mule School: Invoking Java Component over HTTP”

  1. Cannot get this tutorial to work. Got the following error:
    ERROR 2012-03-22 10:26:56,270 [[learning_mule].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy:
    ********************************************************************************
    Message : Failed to find entry point for component, the following resolvers tried but failed: [
    CallableEntryPointResolver: Object “[email protected]” does not implement required interface “interface org.mule.api.lifecycle.Callable”
    ReflectionEntryPointResolver: Could not find entry point on: “com.mulesoft.java.JavaMule” with arguments: “{class java.lang.String}”
    MethodHeaderPropertyEntryPointResolver: The required property “method” is not set on the event
    AnnotatedEntryPointResolver: Component: [email protected] doesn’t have any annotated methods, skipping.
    ]

    Agree(0)Disagree(0)Comment
    • Hi Daniel, this error suggests that Mule couldn’t find a method in “JavaMule” class with “String” parameter. Looks like this blog post was missing the definition for JavaMule class. I’ve corrected it now. Please update JavaMule class as described in the blog and run the flow again.

      Agree(0)Disagree(0)Comment
  2. Updated JavaMule as in the blog but still get the same error:
    Component that caused exception is: DefaultJavaComponent{javaFlow1.commponent.23999306}. Message payload is of type: String

    Agree(0)Disagree(0)Comment
  3. Forget my previous comment. It works now after I restart Mute Studio.

    Thanks,
    Daniel

    Agree(0)Disagree(0)Comment
  4. […] the previous lesson Invoking Java Component Over HTTP, we learned how to invoke a simple method of a Java component in Mule Flow. Let’s now go a […]

    Agree(0)Disagree(0)Comment
  5. When I made this tutorial, I just added a second flow into the learningmule project from tut 1. Running the app with more than 1 flow doesnt seem to work. No listeners get registered, and I see an error/warning “This JVM wasnt styarted by wrapper” Is there a 1:1 mapping of flow to app?

    Agree(0)Disagree(0)Comment
    • Stuart, each Mule project can have multiple flows. Only flow names need to be unique. Doest the embeded Mule Server start when you try to run Mule Application? If yes, can you post error message you see on console.

      Agree(0)Disagree(0)Comment