The following example describes a doProcessMessage() filter method that outputs multiple messages for each one one input message:

@Override
public Message[] doProcessMessage(final Message[] messages) throws MessageException, FilterProcessingException,
                                                InterruptedException {
                               
	List<Message> outputMessageList = new ArrayList<Message>();
                               
    for (int i = 0; i< messages.length; i++) {
                                               
    	Message message = messages[i];
        try {
        	byte[] inputBuffer = Messages.asBytes(message);                                        
            byte[][] jpegArray = dicomInvoker.getJPG(inputBuffer);
            if (null==jpegArray || 0==jpegArray.length) {
            	throw new FilterProcessingException("No images could be processed");
            } else {
            	for (int jpegs=0; jpegs < jpegArray.length; ++jpegs) {
                                                                                               
 					Message outmessage = message.spawn();
                    Messages.setBody(outmessage, jpegArray[jpegs]);
                    outputMessageList.add(outmessage);
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new FilterProcessingException("Error with message encoding.", e);
        } catch (IOException e) {
            throw new MessageException("Error reading message.", e);
        }                                             
	}
    return  (Message[])outputMessageList.toArray(new Message[outputMessageList.size()]);
}
  • Create a List to hold the output messages:

     List<Message> outputMessageList= new ArrayList<Message>(1);
  • For each Message you want to send, invoke the spawn() method against the input message:

    Message outmessage = message.spawn();
  • Add this message instance to a List multiple times for each output message:

    outputMessageList.add(outmessage);
  • Return the List as an array of Messages:

     return (Message[])outputMessageList.toArray(new Message[outputMessageList.size()]);