MTOM is a standard which allows your services to transfer binary data efficiently and conveniently. Many frameworks have support for MTOM - Axis2, JAX-WS RI, JBoss WS, XFire, Microsoft's WCF, and more.
If the binary is part of the XML document, it needs to be base64 encoded - taking CPU time and increasing the payload size. When MTOM is enabled on a service, it takes binary data which might normally be part of the XML document, and creates an attachment for it.
Enabling MTOM is a rather simple process. First, you must annotate your schema type or POJO to let JAXB know that a particular field could be a candidate for MTOM optimization. Second, you just tell CXF that you wish to enable MTOM.
This page tells you how to activate MTOM for JAXB. MTOM is also supported in Aegis.
1) Annotating the Message
1a) Modifying your schema for MTOM
Lets say we have a Picture schema type like this:
In this case the ImageData element is something we would like to have transferred as an attachment. To do this we just need to add an xmime:expectedContentTypes annotation:
This tells JAXB (which WSDL2Java uses to generate POJOs for your service) that this field could be of any content type. Instead of creating a byte array for the base64Binary element, it will create a DataHandler instead which can be used to stream the data.
1b) Annotation your JAXB beans to enable MTOM
If you're doing code first, you need to add an annotation to your POJO to tell JAXB that the field is a candidate for MTOM optimization. Lets say we have a Picture class with has Title and ImageData fields, then it might look like this:
Note the use of 'application/octet-stream'. According to the standard, you should be able to use any MIME type you like, in order to specify the actual content of the attachment. However, due to a defect in the JAX-B reference implementation, this won't work.
2) Enable MTOM on your service
If you've used JAX-WS to publish your endpoint you can enable MTOM like so:
Or, if you used XML to publish your endpoint:
If you're using the simple frontend you can set the mtom-enabled property on your ServerFactoryBean or ClientProxyFactoryBean:
Similarly, you can use the XML configuration:
Once you've got the above done, its time to start writing your logic. DataHandlers are easy to use and create. To consume a DataHandler:
There are many ways to create DataHandlers. You can use a FileDataSource, ByteArrayDataSource, or write your own DataSource: