Mule ESB java元件兩種寫法
阿新 • • 發佈:2019-02-13
先說1.
說到底就是一個訊息的轉換功能, 就是獲取到http請求過來的訊息(payload), 然後將其轉換為自己後續需要的格式的資料形態。
Java程式碼
- 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
- 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
Java程式碼
- 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;
- }
- }