Sun GlassFish Message Queue 4.4 Developer's Guide for Java Clients

Working with Attachments

If a message contains any data that is not XML, you must add it to the message as an attachment. A message can have any number of attachment parts. Each attachment part can contain anything from plain text to image files.

To create an attachment, you must create a URL object that specifies the location of the file that you want to attach to the SOAP message. You must also create a data handler that will be used to interpret the data in the attachment. Finally, you need to add the attachment to the SOAP message.

To create and add an attachment part to the message, you need to use the JavaBeans Activation Framework (JAF) API. This API allows you to determine the type of an arbitrary piece of data, encapsulate access to it, discover the operations available on it, and activate a bean that can perform these operations. You must include the activation.jar library in your application code in order to work with the JavaBeans Activation Framework.

ProcedureTo Create and Add an Attachment

  1. Create a URL object and initialize it to contain the location of the file that you want to attach to the SOAP message.

    URL url = new URL("");
  2. Create a data handler and initialize it with a default handler, passing the URL as the location of the data source for the handler.

    DataHandler dh = new DataHandler(url);
  3. Create an attachment part that is initialized with the data handler containing the URL for the image.

    AttachmentPart ap1 = message.createAttachmentPart(dh);
  4. Add the attachment part to the SOAP message.


    After creating the attachment and adding it to the message, you can send the message in the usual way.

    If you are using JMS to send the message, you can use the SOAPMessageIntoJMSMessage conversion utility to convert a SOAP message that has an attachment into a JMS message that you can send to a JMS queue or topic using Message Queue.