Mule http inbound endpoint example

0

In this article we will use http transport to receive data. You can either receive or post data, in our example, we will look into http inbound endpoint. It internally uses the GET method to return data from a request-response inbound endpoint.

We will add <http:inbound-endpoint> as the message source in the flow to configure the http inbound endpoint. An inbound HTTP endpoint exposes a service over HTTP, essentially making it an HTTP server.

HTTP/S endpoints are synchronous by default, so you do not have to set exchange-pattern=”request-response” but we still have specified exchange-pattern for clarity sake. We will send a message to the endpoint and expect a response from the flow. We can either use host, port and path attributes to specify the individual components, host server to which the HTTP must connect, the http server port and the location in the host domain. Alternatively we can also use the address attribute which is what we have done here.

In this example, we will simply start an HTTP server accepting requests on http://localhost:8888/someRequest.

<http:inbound-endpoint address="http://localhost:${port1}/someRequest" exchange-pattern="request-response"/>

When entered http://localhost:8888/someRequest in browser, you will see:

The appender message processor appends the string ” received”.

<append-string-transformer message=" Received"/>

muleContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:test="http://www.mulesoft.org/schema/mule/test"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
       http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
       http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.xsd
       http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

    <flow name="httpFlow" processingStrategy="synchronous">
        <http:inbound-endpoint address="http://localhost:${port1}/someRequest" exchange-pattern="request-response"/>
        <append-string-transformer message=" Received"/>
    </flow>

</mule>

In the below example, we now make a client invocation of an HTTP endpoint.

MuleMessage message = new DefaultMuleMessage("<html><body>Hello</body></html>", ctx);
MuleMessage response = muleClient.send("http://localhost:8888/someRequest", message);

MuleHttpInboundExample:

package com.javarticles.mule;

import org.mule.DefaultMuleMessage;
import org.mule.api.MuleContext;
import org.mule.api.MuleMessage;
import org.mule.api.context.MuleContextBuilder;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;
import org.mule.module.client.MuleClient;

public class MuleHttpInboundExample {
    public static void main(String[] args) throws Exception {
        System.setProperty("port1", "8888");
        DefaultMuleConfiguration dmc = new DefaultMuleConfiguration();
        dmc.setId("muleexample");
        dmc.setWorkingDirectory("/esb/mule");
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
                "muleContext.xml");
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        contextBuilder.setMuleConfiguration(dmc);
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
                contextBuilder);
        ctx.start();
        try {
            MuleClient muleClient = new MuleClient(ctx);
            MuleMessage message = new DefaultMuleMessage("<html><body>Hello</body></html>", ctx);
            MuleMessage response = muleClient.send("http://localhost:8888/someRequest", message);            
            System.out.println("Response: " + response.getPayloadAsString());
        } finally {
            ctx.dispose();
        }
    }
}

Output:

Response: <html><body>Hello</body></html> Received

Download the source code

This was an example about http inbound endpoint.

You can download the source code here: muleHttpInboundExample.zip
Share.

Comments are closed.