Configuring GlassFish

We have installed the GlassFish server. Let's now take a look at how to initialize our JMS provider, before we actually write some code to send and receive messages.

First, we define some resources in the GlassFish server, which are called administered objects. These have names, which we can use to access the resources in our application code. The first admin object is connectionFactory. By default, the server has a connectionFactory called __defaultConnectionFactory. This is used for bootstrapping in the messaging model. We can use a Java Naming and Directory Interface (JNDI) lookup by passing the name of the factory. This gives an object in return, which represents the ConnectionFactory

We then use the connectionFactory to create the JMSContext. This can be used to create queues and topics, and send messages to them.

In JMS, in order to implement the point-to-point model, we use queues. To implement the publish-subscribe model, we use topics. We look up these resources using JNDI in our code. The GlassFish server defines a default connection factory, __defaultConnectionFactoryThis factory, the queues, and topics are registered as part of the JNDI setup inside the GlassFish server.

Finally, let's create something called destination resources. These destination resources serve as the message stores. We can create queues and topics as message stores. To create a resource, click on Destination Resource, select New, and define the resource. We provide a JNDI name, which will be used for looking up the resource, and a physical name. We also need to select the type of resource, either queue or topic. This is shown in the following screenshot:

Here, we created a queue called jms/PointToPointQueue. Similarly, we can create a topic as follows:

We have now created two destination resources—a messaging queue called PointToPointQueue and a messaging topic called Topic. This is shown in the following screenshot: 

We are now all set with the administration objects required to demonstrate the point-to-point and publish-subscribe models. Let's write some code to demonstrate these models.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset