Sun Java logo     Previous      Contents      Index      Next     

Sun logo
Sun Java System Application Server Platform Edition 8 Developer's Guide 

Chapter 14  
Using the Java Message Service

This chapter describes how to use the Java™ Message Service (JMS) API. The Sun Java™ System Application Server has a fully integrated JMS provider: the Sun Java™ System Message Queue software.

For general information about the JMS API, see the J2EE tutorial:

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JMS.html#wp84181

For detailed information about JMS concepts and JMS support in Sun Java System Application Server, see the Sun Java System Application Server Administration Guide.

This chapter contains the following sections:


The JMS Provider

Sun Java System Application Server support for JMS messaging, in general, and for message-driven beans, in particular, requires messaging middleware that implements the JMS specification: a JMS provider. Sun Java System Application Server uses the Sun Java System Message Queue software as its native JMS provider. The Sun Java System Message Queue software is tightly integrated into Sun Java System Application Server, providing transparent JMS messaging support. This support (known within Sun Java System Application Server as the JMS Service) requires only minimal administration.

For more information about the Sun Java System Message Queue, refer to the following documentation:

http://docs.sun.com/db/prod/s1.s1msgqu#hic

For general information about the JMS API, see the JMS web page at:

http://java.sun.com/products/jms/index.html


Administration of the JMS Service

To configure the JMS Service and prepare JMS resources for use in applications deployed to the Sun Java System Application Server, you must perform these tasks:

For information about other JMS administration tasks, see the Sun Java System Application Server Administration Guide and the Sun Java System Message Queue documentation at:

http://docs.sun.com/db/prod/s1.s1msgqu#hic

Configuring the JMS Service

You can edit or check the JMS Service configuration in the following ways:

The “Using The Administration Console” section describes each JMS Service setting. The “Using The Command Line Interface” section merely lists syntax and default values.

Using the Administration Console

To edit the JMS Service configuration using the Administration Console, perform the following tasks:

  1. Login to the Administration Console by going to the following URL in your web browser:
  2. http://host:port/asadmin

    For example:

    http://localhost:4848/asadmin

  3. Click on the Java Message Service component.
  4. You can edit the following information. Defaults are displayed.
    • Start Timeout (secs) - Specifies the amount of time the server waits at startup for the corresponding JMS instance to respond. If there is no response, startup is aborted. If set to 0, the server waits indefinitely. The default is 30 seconds.
    • Type - A value of LOCAL means the JMS provider is started along with the application server. A value of REMOTE means the JMS host is remote and is not started by the application server. A value of NONE means no JMS service is used.
    • Start Arguments - Specifies the string of arguments supplied for startup of the corresponding JMS instance. By default, there are no arguments.
  5. To add a property, click the Add Property button and enter the property name and value. To delete properties, check the properties you want to delete, then click the Delete Properties button. The following table lists the standard JMS Service properties.
  6. Table 14-1  JMS Service Properties 

    Property

    Default

    Description

    instance-name

    imqbroker

    Specifies the full Sun Java System Message Queue broker instance name.

    instance-name-suffix

    none

    Specifies a suffix to add to the full Sun Java System Message Queue broker instance name. The suffix is separated from the instance name by an underscore character (_). For example, if the instance name is imqbroker, appending the suffix xyz changes the instance name to imqbroker_xyz.

    append-version

    false

    If true, appends the major and minor version numbers, preceded by underscore characters (_), to the full Sun Java System Message Queue broker instance name. For example, if the instance name is imqbroker, appending the version numbers changes the instance name to imqbroker_8_0.

  7. Open on the Java Message Service component.
  8. Open the JMS Hosts component and click on default_JMS_host.
  9. You can edit the following information. Defaults are displayed.
    • Host - Specifies the host name of the JMS provider. The default is localhost.
    • Port - Specifies the port number used by the JMS provider. The default is 7676.
    • Admin Username - Specifies the administrator user name for the JMS provider. The default is admin.
    • Admin Password - Specifies the administrator password for the JMS provider. The default is admin.
  10. Click the Save button.

Using the Command Line Interface

To configure the JMS service using the command line interface, use the asadmin set command. The syntax is as follows, with defaults shown for optional parameters that have them:

asadmin set --user user attribute_name=value [attribute_name=value] *

For more information about the optional general asadmin parameters (--password, --passwordfile, --host, --port, --secure, --terse, --echo, and --interactive), see the Sun Java System Application Server Administration Guide.

The attribute_name is a hierarchical name that looks like this:

server.jms-service.jms_attribute_name

or like this:

server.jms-service.jms-host.default_JMS_host.jms_attribute_name

The jms_attribute_name is the JMS service attribute that needs to be configured. For example:

server.jms-service.jms-host.default_JMS_host.port

To view the list of JMS service attribute names that can be configured using the asadmin set command, use the asadmin get command with a wildcard. The asadmin get command has the same syntax as the asadmin set command. For example:

asadmin get --user joeuser "server.jms-service.*"

A list of attribute names for configuring the JMS service of the server1 application server instance is displayed as follows:

server.jms-service.init-timeout-in-seconds = 60
server.jms-service.start-args = <null>
server.jms-service.type = LOCAL

Here are two examples of running the asadmin set command:

asadmin set --user joeuser server.jms-service.type=REMOTE

asadmin set --user joeuser server.jms-service.jms-host.default_JMS_host.port=7677

The attribute_name for a JMS property is a hierarchical name that looks like this:

server.jms-service.property.jms_property_name

The jms_property_name is the JMS service property that needs to be configured. Here is an example of running the asadmin set command to set a JMS property:

asadmin set --user joeuser server.jms-service.property.instance-name-suffix=xyz

Checking Whether the JMS Provider Is Running

You can use the asadmin jms-ping command to check whether a Sun Java System Message Queue instance is running. The syntax is as follows, with defaults shown for optional parameters that have them:

asadmin jms-ping --user user

For more information about the optional general asadmin parameters (--password, --passwordfile, --host, --port, --secure, --terse, --echo, and --interactive), see the Sun Java System Application Server Administration Guide.

For example:

asadmin jms-ping --user joeuser

Creating Physical Destinations

Produced messages are delivered for routing and subsequent delivery to consumers using physical destinations in the JMS provider. A physical destination is identified and encapsulated by an administered object (a Topic or Queue destination resource) that an application component uses to specify the destination of messages it is producing and the source of messages it is consuming.

This section describes how to create a physical destination. To create a destination resource, see Creating JMS Resources: Destinations and Connection Factories.

You can create a JMS physical destination in the following ways:

The “Using The Administration Console” section describes each physical destination setting. The “Using The Command Line Interface” section merely lists syntax and default values.

Using the Administration Console

To create a JMS physical destination using the Administration Console, perform the following tasks:

  1. Login to the Administration Console by going to the following URL in your web browser:
  2. http://host:port/asadmin

    For example:

    http://localhost:4848/asadmin

  3. Open the Java Message Service component.
  4. Click Service, then click Physical Destinations.
  5. Click the New button.
  6. Enter the following information:
    • Destination Name (required) - Specify the name of the physical destination.
    • Type (required) - Select queue or topic from the list.
  7. Click the OK button.

Using the Command Line Interface

To create a JMS physical destination using the command line, use the asadmin create-jmsdest command. The syntax is as follows, with defaults shown for optional parameters that have them:

asadmin create-jmsdest --user user --desttype dest_type [--property (name=value)[:name=value]*] dest_name

For more information about the parameters specific to asadmin create-jmsdest, see Using the Administration Console.

For more information about the optional general asadmin parameters (--password, --passwordfile, --host, --port, --secure, --terse, --echo, and --interactive), see the Sun Java System Application Server Administration Guide.

For example:

asadmin create-jmsdest --user joeuser --desttype topic MyDest

To delete a JMS physical destination, use the following command:

asadmin delete-jmsdest --user user --desttype dest_type dest_name

For example:

asadmin delete-jmsdest --user joeuser --desttype topic MyDest

To list JMS physical destinations, use the following command:

asadmin list-jmsdest --user user [-desttype dest_type]

For example:

asadmin list-jmsdest --user joeuser --desttype topic

Creating JMS Resources: Destinations and Connection Factories

You can create two kinds of JMS resources in Sun Java System Application Server:

In either case, the steps for creating a JMS resource are the same. You can create a JMS resource in the following ways:

The “Using The Administration Console” section describes each JMS resource setting. The “Using The Command Line Interface” section merely lists syntax and default values.

Using the Administration Console

To create a JMS resource using the Administration Console, perform the following tasks:

  1. Login to the Administration Console by going to the following URL in your web browser:
  2. http://host:port/asadmin

    For example:

    http://localhost:4848/asadmin

  3. Open the Java Message Service component.
  4. Click Connection Factories to create a connection factory, or click Destination Resources to create a queue or topic.
  5. Click the New button.
  6. Enter the following information:
    • JNDI Name (required) - Enter the JNDI name that application components must use to access the JMS resource.
    • Type (required) - Select the type of the JMS resource.
      • If you are on the Connection Factories page, the types are:
      •       javax.jms.TopicConnectionFactory
              javax.jms.QueueConnectionFactory

      • If you are on the Destination Resources page, the types are:
      •       javax.jms.Topic
              javax.jms.Queue

    • Description (optional) - You can enter a text description of the JMS resource.
  7. Check the Resource Enabled box to enable the JMS resource.
  8. If a JMS resource is disabled, no application component can connect to it, but its configuration remains in the domain.

  9. Click the OK button.
  10. To add a property, click the Add Property button and enter the property name and value. To delete properties, check the properties you want to delete, then click the Delete Properties button.
  11. The following table lists the most commonly used JMS resource properties. For a complete list of the available properties (called administered object attributes in Sun Java System Message Queue), see the Sun Java System Message Queue Administration Guide.

    Table 14-2  JMS Resource Properties 

    Property

    Default

    Description

    Name

    none

    Specifies the JMS physical destination name associated with this JMS resource. You must specify this property for JMS resources of the Type javax.jms.Topic or javax.jms.Queue.

    Description

    none

    Specifies a text description of the JMS resource.

    MessageServiceAddressList

    none

    Specifies a list of host/port combinations of the Sun Java System Message Queue. For JMS resources of the Typejavax.jms.TopicConnectionFactory or javax.jms.QueueConnectionFactory.

    ClientID

    none

    Specifies the JMS Client Identifier to be associated with a Connection created using the createQueueConnection and createTopicConnection methods of the QueueConnectionFactory and TopicConnectionFactory classes, respectively.

    For JMS resources of the Typejavax.jms.TopicConnectionFactory or javax.jms.QueueConnectionFactory.

    Durable subscription names are unique and only valid within the scope of a client identifier. To create or reactivate a durable subscriber, the connection must have a valid client identifier. The JMS specification ensures that client identifiers are unique and that a given client identifier is allowed to be used by only one active connection at a time.

    UserName

    guest

    Specifies the username for connecting to the Sun Java System Message Queue. For JMS resources of the Typejavax.jms.TopicConnectionFactory or javax.jms.QueueConnectionFactory.

    Password

    guest

    Specifies the password for connecting to the Sun Java System Message Queue. For JMS resources of the Typejavax.jms.TopicConnectionFactory or javax.jms.QueueConnectionFactory.


    Note

    All JMS resource properties that used to work with version 7 of the Application Server are supported for backward compatibility.


  12. Click the OK button.

Using the Command Line Interface

To create a JMS resource using the command line, use the asadmin create-jms-resource command. The syntax is as follows, with defaults shown for optional parameters that have them:

asadmin create-jms-resource --user user --restype resource_type [--enabled=true] [--description text] [--property (name=value)[:name=value]*] jndi_name

For more information about the parameters specific to asadmin create-jms-resource, see Using the Administration Console.

For more information about the optional general asadmin parameters (--password, --passwordfile, --host, --port, --secure, --terse, --echo, and --interactive), see the Sun Java System Application Server Administration Guide.

For example:

asadmin create-jms-resource --user joeuser --restype javax.jms.Topic --property imqDestinationName=testTopic MyTopic

To delete a JMS resource, use the following command:

asadmin delete-jms-resource --user user jndi_name

For example:

asadmin delete-jms-resource --user joeuser MyTopic

To list JMS resources, use the following command:

asadmin list-jms-resources --user user [--restype resource_type]

For example:

asadmin list-jms-resources --user joeuser --restype Topic


Message Queue Resource Adapter

The Sun Java System Message Queue is integrated into the Sun Java System Application Server using a resource adapter that is compliant with the Connector 1.5 specification. The module name of this system resource adapter is jmsra. Every JMS resource is converted to a corresponding connector resource of this resource adapter as follows:

You can use connector configuration tools to manage JMS resources. For more information, see:

http://developers.sun.com/prodtech/appserver/reference/techart/as8_connectors


ConnectionFactory Authentication

If your web, EJB, or client module has res-auth set to Container, but you use the ConnectionFactory.createConnection("user","password") method to get a connection, the Sun Java System Application Server searches the container for authentication information before using the supplied user and password. Version 7 of the Application Server threw an exception in this situation.


Message Queue varhome Directory

Sun Java System Message Queue uses a default directory for storing data such as persistent messages and its log file. This directory is called varhome. Sun Java System Application server uses domain_dir/imq as the varhome directory. Thus for the default Application Server domain, Message Queue data is stored in the following location:

install_dir/domains/domain1/imq/var/instances/imqbroker

Version 7 of the Application Server stored this data in the following location:

install_dir/imq/var/instances/domain1_server

When you execute Sun Java System Message Queue scripts such as install_dir/imq/bin/imqusermgr, you should use the -varhome option. For example:

imqusermgr -varhome $AS_INSTALL/domains/domain1/imq add -u testuser -p testpassword


Delivering SOAP Messages Using the JMS API

Web service clients use the Simple Object Access Protocol (SOAP) to communicate with web services. SOAP uses a combination of XML-based data structuring and Hyper Text Transfer Protocol (HTTP) to define a standardized way of invoking methods in objects distributed in diverse operating environments across the Internet.

For more information about SOAP, see the Apache SOAP web site:

http://xml.apache.org/soap/index.html

You can take advantage of the JMS provider’s reliable messaging when delivering SOAP messages. You can convert a SOAP message into a JMS message, send the JMS message, then convert the JMS message back into a SOAP message. The following sections explain how to do these conversions:

Sending SOAP Messages Using the JMS API

You use the MessageTransformer utility to convert a SOAP message into a JMS message. You then send the JMS message containing the SOAP payload as you would a normal JMS message.

  1. Import the library com.sun.messaging.xml.MessageTransformer. This is the utility whose methods you use to convert SOAP messages to JMS messages and the reverse.
  2. import com.sun.messaging.xml.MessageTransformer;

  3. Initialize the TopicConnectionFactory, TopicConnection, TopicSession, and publisher.
  4. tcf = new TopicConnectionFactory();
    tc = tcf.createTopicConnection();
    session = tc.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
    topic = session.createTopic(topicName);
    publisher = session.createPublisher(topic);

  5. Construct a SOAP message using the SOAP with Attachments API for Java (SAAJ). For more information on constructing a SOAP message, see the Sun Java System Message Queue Developer’s Guide.
  6. *construct a default soap MessageFactory */
    MessageFactory mf = MessageFactory.newInstance();

    * Create a SOAP message object.*/
    SOAPMessage soapMessage = mf.createMessage();

    /** Get SOAP part.*/
    SOAPPart soapPart = soapMessage.getSOAPPart();

    /* Get SOAP envelope. */
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

    /* Get SOAP body.*/
    SOAPBody soapBody = soapEnvelope.getBody();

    /* Create a name object. with name space */
    /* http://www.sun.com/imq. */
    Name name = soapEnvelope.createName("HelloWorld", "hw",
      "http://www.sun.com/imq");

    * Add child element with the above name. */
    SOAPElement element = soapBody.addChildElement(name)

    /* Add another child element.*/
    element.addTextNode( "Welcome to Sun Java System Web Services." );

    /* Create an atachment with activation API.*/
    URL url = new URL ("http://java.sun.com/webservices/");
    DataHandler dh = new DataHandler (url);
    AttachmentPart ap = soapMessage.createAttachmentPart(dh);

    /*set content type/ID. */
    ap.setContentType("text/html");
    ap.setContentId("cid-001");

    /** add the attachment to the SOAP message.*/
    soapMessage.addAttachmentPart(ap);
    soapMessage.saveChanges();

  7. Convert the SOAP message to a JMS message by calling the MessageTransformer.SOAPMessageintoJMSMessage() method.
  8. Message m = MessageTransformer.SOAPMessageIntoJMSMessage (soapMessage, session );

  9. Publish the JMS message.
  10. publisher.publish(m);

  11. Close the JMS connection.
  12. tc.close();

Receiving SOAP Messages Using the JMS API

You receive the JMS message containing the SOAP payload as you would a normal JMS message. You then use the MessageTransformer utility to convert the JMS message back into a SOAP message.

  1. Import the library com.sun.messaging.xml.MessageTransformer. This is the utility whose methods you use to convert SOAP messages to JMS messages and the reverse.
  2. import com.sun.messaging.xml.MessageTransformer;

  3. Initialize the TopicConnectionFactory, TopicConnection, TopicSession, TopicSubscriber, and Topic.
  4. messageFactory = MessageFactory.newInstance();
    tcf = new com.sun.messaging.TopicConnectionFactory();
    tc = tcf.createTopicConnection();

    session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

    topic = session.createTopic(topicName);
    subscriber = session.createSubscriber(topic);
    subscriber.setMessageListener(this);
    tc.start();

  5. Use the OnMessage method to receive the message. Use the SOAPMessageFromJMSMessage method to convert the JMS message to a SOAP message.
  6. public void onMessage (Message message) {
      SOAPMessage soapMessage =
      MessageTransformer.SOAPMessageFromJMSMessage( message,
      messageFactory ); }

  7. Retrieve the content of the SOAP message.


Previous      Contents      Index      Next     


Copyright 2004 Sun Microsystems, Inc. All rights reserved.