## REST Example

### Overview

This section describes how to run the REST example, which consists of a servlet engine integral to the message broker binary, and some demonstration servlets that run as a Web application. To connect to the Web applications, you can use your favorite Web browser.

### Example prerequisites

You must ensure that the message broker is configured to instantiate an embedded servlet engine. In your broker configuration file, `conf/activemq.xml`, check that there is a `jetty` element configured as shown in Example 4.1 .

Example 4.1. Configuration of an Embedded Servlet Engine

    <!-- Embedded servlet engine for serving up the Admin console
    -->
    <jetty xmlns="http://mortbay.com/schemas/jetty/1.0">
      <connectors>
        <nioConnector port="8161" />
      </connectors>
      <handlers>
        <webAppContext contextPath="/admin"
          resourceBase="${activemq.base}/webapps/admin"
          logUrlOnStart="true" />
        <webAppContext contextPath="/demo"
          resourceBase="${activemq.base}/webapps/demo"
          logUrlOnStart="true" />
      </handlers>
    </jetty>

With the configuration shown in Example 4.1 , the servlet engine opens up a HTTP port on IP port, 8161. The following Web applications are loaded:

- Demonstration application (from `webapps/demo`),

- REST protocol servlets (from `webapps/demo`).

- Web console servlet (from `webapps/admin`),

### Example steps

To run the REST Web example, perform the following steps:

1. Run the servlet engine .

2. Open a Web browser .

3. Send a message .

4. Browse the message queue .

5. Receive a message from the queue .

### Run the servlet engine

To run the embedded servlet engine, open a new command window and enter the following command to start the default message broker:

    activemq

This step assumes that your broker is configured as described in Example prerequisites .

### Open a Web browser

Open your favorite Web browser (for example, Firefox or Internet Explorer) and navigate to the following URL:

    http://localhost:8161/demo

Your browser should now show the welcome page for the Web examples, as shown in Figure 4.1 .

Figure 4.1. Welcome Page for Web Examples

### Send a message

To view the form for publishing messages, click the link, Send a message. The Send a JMS Message form now appears in your browser, as shown in Figure 4.2 .

Figure 4.2. The Send a JMS Message Form

In the Destination name text field, enter `FOO.BAR` to send a message to the `FOO.BAR` queue. Leave the Destination Type as `Queue`. Then enter an arbitrary text message in the large message text box. Click the Send button at the bottom of the form to send the message.

### Browse the message queue

Using the history feature of your browser, navigate back to the example welcome page (see Figure 4.1 ). The `queueBrowse` servlet supports a variety of ways to browse the contents of a queue and these are listed at the bottom of the welcome page. The following browsing options are listed:

- Browse a queue.

- Browse a queue as XML.

- Browse a queue as Atom.

- Browse a queue as RSS 1.0.

- Browse a queue as RSS 2.0.

If you click on Browse a queue, you should see a page like Figure 4.3 .

Figure 4.3. Default Option to Browse a Queue

If you click on Browse a queue as XML, you should see a page like Figure 4.4 .

Figure 4.4. Option to Browse a Queue as XML

If you click on Browse a queue as Atom, you should see a page like Figure 4.5 .

Figure 4.5. Option to Browse a Queue as Atom

If you click on Browse a queue as RSS 1.0 or Browse a queue as RSS 2.0, you should see a page like Figure 4.6 .

Figure 4.6. Option to Browse a Queue as RSS 1.0

### Receive a message from the queue

To receive a message from the `FOO.BAR` queue, open the example welcome page in your browser, http://localhost:8161/demo, and click the link, Receive a message.

You should now see the text of the message that you sent earlier. You will probably also receive an error from your browser, if the message is not formatted as HTML or XML (which the browser expects).