1 10 11 package org.mule.routing.outbound; 12 13 import org.mule.config.i18n.Message; 14 import org.mule.config.i18n.Messages; 15 import org.mule.impl.MuleMessage; 16 import org.mule.umo.UMOMessage; 17 import org.mule.umo.UMOSession; 18 import org.mule.umo.routing.RoutingException; 19 20 30 public class MessageChunkingRouter extends FilteringOutboundRouter 31 { 32 private int chunkSize = 0; 33 private int numberOfMessages = 1; 34 35 public int getChunkSize() 36 { 37 return chunkSize; 38 } 39 40 public void setChunkSize(int chunkSize) 41 { 42 this.chunkSize = chunkSize; 43 } 44 45 public int getNumberOfMessages() 46 { 47 return numberOfMessages; 48 } 49 50 public void setNumberOfMessages(int numberOfMessages) 51 { 52 this.numberOfMessages = numberOfMessages; 53 } 54 55 public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) 56 throws RoutingException 57 { 58 if (chunkSize == 0 && numberOfMessages < 2) 59 { 60 return super.route(message, session, synchronous); 61 } 62 else if (chunkSize > 0) 63 { 64 byte[] data = new byte[0]; 65 try 66 { 67 data = message.getPayloadAsBytes(); 68 } 69 catch (Exception e) 70 { 71 throw new RoutingException(new Message(Messages.FAILED_TO_READ_PAYLOAD), message, 72 getEndpoint(0, message), e); 73 } 74 75 int parts = data.length / chunkSize; 76 if ((parts * chunkSize) < data.length) 77 { 78 parts++; 79 } 80 int len = chunkSize; 81 UMOMessage part = null; 82 int count = 0; 83 int pos = 0; 84 byte[] buffer = null; 85 try 86 { 87 for (; count < parts; count++) 88 { 89 if ((pos + len) > data.length) 90 { 91 len = data.length - pos; 92 } 93 buffer = new byte[len]; 94 System.arraycopy(data, pos, buffer, 0, buffer.length); 95 pos += len; 96 part = new MuleMessage(buffer, message); 97 part.setCorrelationId(message.getUniqueId()); 98 part.setCorrelationGroupSize(parts); 99 part.setCorrelationSequence(count); 100 super.route(part, session, synchronous); 101 } 102 103 } 104 catch (RoutingException e) 105 { 106 e = new RoutingException(e.getI18nMessage(), e.getUmoMessage(), e.getEndpoint(), e.getCause()); 108 throw e; 113 } 114 } 115 return message; 116 } 117 } 118 | Popular Tags |