Mule ESB java组件两种写法

来源:互联网 发布:115网盘岛国资源 淘宝 编辑:程序博客网 时间:2024/06/01 20:41

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


先说1. 
说到底就是一个消息的转换功能, 就是获取到http请求过来的消息(payload), 然后将其转换为自己后续需要的格式的数据形态。 
Java代码  收藏代码
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import javax.activation.DataHandler;  
  8.   
  9. import org.mule.api.MuleEventContext;  
  10. import org.mule.api.MuleMessage;  
  11. import org.mule.api.lifecycle.Callable;  
  12.   
  13.   
  14.   
  15. public class Transformer implements Callable  {  
  16.   
  17.     @Override  
  18.     public Object onCall(MuleEventContext eventContext) throws Exception {  
  19.         // TODO Auto-generated method stub  
  20.           
  21.         MuleMessage message  = eventContext.getMessage();  
  22.           
  23.         //message.getPayload()得到的是通过Httpclient-MultipartEntity传过来的二进制流  
  24.         System.out.println(message.getPayload());  
  25.         InputStream in = (InputStream) message.getPayload();  
  26.         ByteArrayOutputStream bstream = new ByteArrayOutputStream();    
  27.         byte[] buff = new byte[100];    
  28.         int rc = 0;  
  29.         try {  
  30.             while ((rc = in.read(buff, 0100)) > 0) {    
  31.                 bstream.write(buff, 0, rc);    
  32.             }  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         }    
  36.         byte[] bytes = bstream.toByteArray();    
  37.           
  38.         System.out.println("-------------------------111--" + bytes);  
  39.         //httpClient addPart传过来的StringBody  
  40.         System.out.println(message.getInboundAttachmentNames());  
  41.         DataHandler h1 = message.getInboundAttachment("serviceId");  
  42.         DataHandler h2 =  message.getInboundAttachment("fileName");  
  43.         System.out.println("------------------------------------");  
  44.           
  45.         Map map = new HashMap();  
  46.         map.put("serviceId", h1.getContent().toString());  
  47.         map.put("fileName", h2.getContent().toString());  
  48.         map.put("payload", bytes);  
  49.           
  50.         return map;  
  51.     }  
  52. }  


将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 

Java代码  收藏代码
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import javax.activation.DataHandler;  
  8.   
  9. import org.mule.api.MuleEvent;  
  10. import org.mule.api.MuleException;  
  11. import org.mule.api.MuleMessage;  
  12. import org.mule.api.processor.MessageProcessor;  
  13.   
  14. public class MutipartTransformer implements MessageProcessor  {  
  15.   
  16.     @Override  
  17.     public MuleEvent process(MuleEvent event) throws MuleException {  
  18.           
  19.         MuleMessage mulemessage = event.getMessage();  
  20.           
  21.         InputStream in = (InputStream) mulemessage.getPayload();  
  22.         ByteArrayOutputStream bstream = new ByteArrayOutputStream();    
  23.         byte[] buff = new byte[100];    
  24.         int rc = 0;  
  25.         try {  
  26.             while ((rc = in.read(buff, 0100)) > 0) {    
  27.                 bstream.write(buff, 0, rc);    
  28.             }  
  29.         } catch (IOException e) {  
  30.             e.printStackTrace();  
  31.         }    
  32.         byte[] bytes = bstream.toByteArray();    
  33.           
  34.           
  35.         System.out.println(mulemessage.getInboundAttachmentNames());  
  36.         DataHandler h1 = mulemessage.getInboundAttachment("serviceId");  
  37.         DataHandler h2 =  mulemessage.getInboundAttachment("fileName");  
  38.         try {  
  39.             System.out.println(h1.getContent().toString());  
  40.         } catch (IOException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         try {  
  44.             System.out.println(h2.getContent().toString());  
  45.         } catch (IOException e) {  
  46.             // TODO Auto-generated catch block  
  47.             e.printStackTrace();  
  48.         }  
  49.         System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbb    ");  
  50.           
  51.         Map map = new HashMap();  
  52.         map.put("serviceId", h1);  
  53.         map.put("fileName", h2);  
  54.         map.put("payload", bytes);  
  55.         event.setMessage(mulemessage);  
  56.         return event;  
  57.     }  
  58. }  
1 0
原创粉丝点击