3 min read

(For more resources on Java, see here.)

Setting up GlassFish for JMS

Before we start writing code to take advantage of the JMS API, we need to configure some GlassFish resources. Specifically, we need to set up a JMS connection factory, a message queue, and a message topic.

Setting up a JMS connection factory

The easiest way to set up a JMS connection factory is via GlassFish’s web console. The web console can be accessed by starting our domain, by entering the following command in the command line:

asadmin start-domain domain1

Then point the browser to http://localhost:4848 and log in:

Setting up GlassFish for JMS and Working with Message Queues

A connection factory can be added by expanding the Resources node in the tree at the left-hand side of the web console, expanding the JMS Resources node and clicking on the Connection Factories node, then clicking on the New… button in the main area of the web console.

Setting up GlassFish for JMS and Working with Message Queues

For our purposes, we can take most of the defaults. The only thing we need to do is enter a Pool Name and pick a Resource Type for our connection factory.

It is always a good idea to use a Pool Name starting with “jms/” when picking a name for JMS resources. This way JMS resources can be easily identified when browsing a JNDI tree.

In the text field labeled Pool Name, enter jms/GlassFishBookConnectionFactory. Our code examples later in this article will use this JNDI name to obtain a reference to this connection factory.

The Resource Type drop-down menu has three options:

  • javax.jms.TopicConnectionFactory – used to create a connection factory that creates JMS topics for JMS clients using the pub/sub messaging domain
  • javax.jms.QueueConnectionFactory – used to create a connection factory that creates JMS queues for JMS clients using the PTP messaging domain
  • javax.jms.ConnectionFactory – used to create a connection factory that creates either JMS topics or JMS queues

For our example, we will select javax.jms.ConnectionFactory. This way we can use the same connection factory for all our examples, those using the PTP messaging domain and those using the pub/sub messaging domain.

After entering the Pool Name for our connection factory, selecting a connection factory type, and optionally entering a description for our connection factory, we must click on the OK button for the changes to take effect.

Setting up GlassFish for JMS and Working with Message Queues

We should then see our newly created connection factory listed in the main area of the GlassFish web console.

Setting up a JMS message queue

A JMS message queue can be added by expanding the Resources node in the tree at the left-hand side of the web console, expanding the JMS Resources node and clicking on the Destination Resources node, then clicking on the New… button in the main area of the web console.

Setting up GlassFish for JMS and Working with Message Queues

In our example, the JNDI name of the message queue is jms/GlassFishBookQueue. The resource type for message queues must be javax.jms.Queue. Additionally, a Physical Destination Name must be entered. In this example, we use GlassFishBookQueue as the value for this field.

After clicking on the New… button, entering the appropriate information for our message queue, and clicking on the OK button, we should see the newly created queue:

Setting up GlassFish for JMS and Working with Message Queues

Setting up a JMS message topic

Setting up a JMS message topic in GlassFish is very similar to setting up a message queue.

In the GlassFish web console, expand the Resources node in the tree at the left hand side, then expand the JMS Resouces node and click on the Destination Resources node, then click on the New… button in the main area of the web console.

Setting up GlassFish for JMS and Working with Message Queues

Our examples will use a JNDI Name of jms/GlassFishBookTopic. As this is a message topic, Resource Type must be javax.jms.Topic. The Description field is optional. The Physical Destination Name property is required. For our example, we will use GlassFishBookTopic as the value for this property.

After clicking on the OK button, we can see our newly created message topic:

Setting up GlassFish for JMS and Working with Message Queues

Now that we have set up a connection factory, a message queue, and a message topic, we are ready to start writing code using the JMS API.

LEAVE A REPLY

Please enter your comment!
Please enter your name here