Mule ESB java组件两种写法

1是编写java的transformer, 2 是编写java的component

先说1.
说到底就是一个消息的转换功能, 就是获取到http请求过来的消息(payload), 然后将其转换为自己后续需要的格式的数据形态。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.activation.DataHandler;

import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.lifecycle.Callable;



public class Transformer implements Callable  {

	@Override
	public Object onCall(MuleEventContext eventContext) throws Exception {
		// TODO Auto-generated method stub
		
		MuleMessage message  = eventContext.getMessage();
		
		//message.getPayload()得到的是通过Httpclient-MultipartEntity传过来的二进制流
		System.out.println(message.getPayload());
		InputStream in = (InputStream) message.getPayload();
		ByteArrayOutputStream bstream = new ByteArrayOutputStream();  
        byte[] buff = new byte[100];  
        int rc = 0;
        try {
			while ((rc = in.read(buff, 0, 100)) > 0) {  
				bstream.write(buff, 0, rc);  
			}
		} catch (IOException e) {
			e.printStackTrace();
		}  
        byte[] bytes = bstream.toByteArray();  
        
        System.out.println("-------------------------111--" + bytes);
        //httpClient addPart传过来的StringBody
        System.out.println(message.getInboundAttachmentNames());
        DataHandler h1 = message.getInboundAttachment("serviceId");
        DataHandler h2 =  message.getInboundAttachment("fileName");
		System.out.println("------------------------------------");
		
		Map map = new HashMap();
		map.put("serviceId", h1.getContent().toString());
		map.put("fileName", h2.getContent().toString());
		map.put("payload", bytes);
		
		return map;
	}
}


将http post过来的两个字符串参数 和 一个文件byte流 转换为 map形式, 目的是因为http传过来的是 multipart/form-data , 而后面的框架代码要同时兼容application/x-www-form-urlencoded , 所以统一进行转换

实现Callable 接口, 取得MuleEventContext上下文,进而获取MuleMessage。
其中getPayload()得到了 inputstream , 获取文件字节数组, 通过getInboundAttachment("serviceId") 获取到StringBody、
直接返回消息实例

2. component
目的是改变消息set到MuleEvent ,返回event


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.activation.DataHandler;

import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.processor.MessageProcessor;

public class MutipartTransformer implements MessageProcessor  {

	@Override
	public MuleEvent process(MuleEvent event) throws MuleException {
		
		MuleMessage mulemessage = event.getMessage();
		
		InputStream in = (InputStream) mulemessage.getPayload();
		ByteArrayOutputStream bstream = new ByteArrayOutputStream();  
        byte[] buff = new byte[100];  
        int rc = 0;
        try {
			while ((rc = in.read(buff, 0, 100)) > 0) {  
				bstream.write(buff, 0, rc);  
			}
		} catch (IOException e) {
			e.printStackTrace();
		}  
        byte[] bytes = bstream.toByteArray();  
		
        
        System.out.println(mulemessage.getInboundAttachmentNames());
        DataHandler h1 = mulemessage.getInboundAttachment("serviceId");
        DataHandler h2 =  mulemessage.getInboundAttachment("fileName");
        try {
			System.out.println(h1.getContent().toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
        try {
			System.out.println(h2.getContent().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbb    ");
		
		Map map = new HashMap();
		map.put("serviceId", h1);
		map.put("fileName", h2);
		map.put("payload", bytes);
		event.setMessage(mulemessage);
		return event;
	}
}

你可能感兴趣的:(java)