A small example of mule HTTP endpoint + Transformer

Source: Internet
Author: User

In this example, the original message is stored in the payload of the message when messages are transmitted between mule components.

1. Open mule studio 3.3

2. Create a new project named test. The main process is main. flow.

3. Switch to the flow creation process

4. Drag an HTTP endpoint. The default value is inbound endpoint.

5. Drag the second HTTP endpoint to connect to the previous HTTP endpoint. This time, the HTTP outbound endpoint is used by default.

6. Drag a Java transformer between two HTTP endpoints

At this time, the process is as follows (HTTP inbound endpoint on the left, a Java-type transformer in the middle, and an HTTP outbound endpoint on the right ):

7. Create a package under/src/main/Java, create a Java class, and integrate it with abstractmessagetransformer;

The implementation code is as follows:

   1: package com.leipei.test;

   2:  

   3: import org.apache.log4j.Logger;

   4: import org.mule.api.MuleMessage;

   5: import org.mule.api.transformer.TransformerException;

   6: import org.mule.transformer.AbstractMessageTransformer;

   7:  

   8: public class MyTransformer extends AbstractMessageTransformer {

   9: private static Logger logger=Logger.getLogger(MyTransformer.class);

  10:     @Override

  11:     public Object transformMessage(MuleMessage message, String outputEncoding)

  12:             throws TransformerException {

  13:         String content=null;

  14:         try {

  15:             content = message.getPayloadAsString();

  16:             logger.info("orignal content:"+content);

  17:         } catch (Exception e) {

  18:             logger.error(e.getMessage());

  19:         }

  20:         return content;

  21:     }

  22:  

  23: }

This Code indicates that we have obtained the message from the inbound HTTP endpoint without any processing and directly handed it to the HTTP outbound endpoint.

8. Modify the HTTP outbound endpoint so that it can send an http get request to Baidu:

9. Open the HTTP inbound endpoint and confirm that the listening port of the HTTP inbound endpoint that can receive the mule message locally is as follows:

This is the default configuration.

10. Start the mule project and you can see the following output in the console:

**********************************************************************
INFO 2012-08-17 23:37:37,666 [[test].config.change.1.thread.1] org.mule.module.launcher.DeploymentService:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started app 'test' +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

11. Enter http: // localhost: 8081 in the local browser. The result is as follows:

This indicates that mule has forwarded the local request, and our request content is indeed put in the payload of the message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.