Mule ESB java组件两种写法
来源:互联网 发布:115网盘岛国资源 淘宝 编辑:程序博客网 时间:2024/06/01 20:41
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;
- }
- }
1 0
- Mule ESB java组件两种写法
- Mule ESB
- mule esb
- Mule ESB
- Mule ESB 2.0 苦斗两周之后的初印象
- Mule ESB的component(三)——Java Component
- ESB构架的MULE
- Mule esb简介
- Mule ESB 学习笔记
- Mule ESB 学习笔记
- mule esb 简介(摘要)
- Mule ESB 学习笔记
- Mule ESB安装
- mule esb 3.X
- Mule ESB 学习笔记
- mule ESB总结
- Mule ESB 学习笔记
- Mule ESB 学习笔记
- http协议详解
- response.sendRedirect("/"); disp.include(request, response);
- 关于C语言时间获取函数调用的概念
- AudioManager 类的使用
- 一只兔子如何吃掉狼的!
- Mule ESB java组件两种写法
- oracle怎么实现分页比较好
- VS 2012 C#ActiveX控件开发总结
- 2015阿里移动推荐算法比赛第一赛季总结
- jsf - javax.validation.ConstraintViolationException
- AP聚类
- Android library引用失败
- [C语言][LeetCode][61]Rotate List
- 如何查看 SQL Server 占用内存