Delphi Integration with the Gl…
  • RSS Feed

Last modified on 3/22/2011 2:15 PM by User.

Delphi Integration with the GlassFish v3 Application Server Part 1 - Sending Messages

Introduction

This Habari OpenMQ Client tutorial will guide you through the creation of a simple web application for GlassFish v3 which uses a Servlet to send messages to a message queue on the embedded OpenMQ broker. The Delphi ConsumerTool application then can receive messages from the message queue.

Message Flow

  1. a web browser sends a HTTP request to a Java Servlet running the GlassFish v3 server
  2. for every new request, the Servlet sends JMS messages to a Queue in the embedded OpenMQ broker
  3. the Delphi Habari OpenMQ ConsumerTool waits for new JMS messages to the JMS Queue on the Stomp adapter port
  4. incoming messages from the OpenMQ broker are displayed as ConsumerTool console output

Requirements

Create a Web Application in Java

  • Start NetBeans IDE.
  • In the IDE, choose File > New Project (Ctrl-Shift-N).
  • In the New Project wizard, expand the Java Web category and select Web Application. Then click Next.
  • In the Name and Location page of the wizard, do the following:
    • In the Project Name field, type HabariGlassFish3App.
    • Leave the Use Dedicated Folder for Storing Libraries checkbox unselected.
    • Leave the Set as Main Project checkbox selected.
  • Click Next.
  • In the Server and Settings page of the wizard, do the following:
    • In the Server list, choose GlassFish v3
    • Leave the Java EE version unchanged
    • Leave the Enable Contexts and Dependency Injection checkbox unselected.
  • Click Finish

The project is created and opened in the IDE. You should see the following components:

  • The Projects window, which contains a tree view of the components of the project, including source files, libraries that your code depends on, and so on.
  • The Source Editor window with a file called index.jsp open.

Add a Servlet to the project.

  • In the IDE, choose File > New File ... (Ctrl-N).
  • In the New File wizard, choose the Web category and select Servlet. Then click Next.
  • In the Name and Location page of the wizard, do the following:
    • In the Class Name field, type OpenMQServlet.
    • Leave the Location unchanged.
    • In the Package field, type openmqapp.
  • Click Finish

Add JMS resources to the Servlet

In the source editor window for OpenMQServlet.java, do the following:

  • after the line

public class OpenMQServlet extends HttpServlet {

  • insert the following code:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
    @Resource(mappedName="jms/HabariConnectionFactory")
    private ConnectionFactory connectionFactory;

    @Resource(mappedName="jms/TOOL.DEFAULT")
    private Queue queue;

    private void sendMessage() throws JMSException {
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer messageProducer = session.createProducer(queue);

        Calendar c = new GregorianCalendar();
        Date now = c.getTime();

        Message message = session.createTextMessage(now.toString());

        messageProducer.send(message);
        messageProducer.close();
        connection.close();
        System.out.println("Message sent: " + now.toString());
    }

  • use Fix Imports (Ctrl+Shift+I) to add the missing import statements.
  • Note: you need to choose javax.jms.Queue and javax.jms.Connection.

Send a JMS Message in the processRequest method

  • Insert the following code at the beginning of the processRequest method :

1:
2:
3:
4:
5:
        try {
            sendMessage();
        } catch (JMSException ex) {
            Logger.getLogger(OpenMQServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

  • use Fix Imports (Ctrl+Shift+I) to add the missing import statement.

Build the web application

  • In the IDE, choose Run > Build Main Project (F11).
  • Check the output folder dist, the web application file HabariGlassFish3App.war should be there.

Configure JMS Resources

Start GlassFish

  1. Expand the Servers node in the Services window of the IDE.
  2. Right-click the application server node GlassFish Server 3 and select Start
  3. Wait until a small green arrow next to the application server node indicates the server is running.

Open the Admin Console

  1. Confirm that the application server is running by expanding the Servers node in the Services window of the IDE. A small green arrow next to the application server node indicates the server is running.
  2. Right-click the application server node and choose View Admin Console to open the login window in your browser.
  3. Log in to the server. The default user name and password are admin and adminadmin.

Create the JMS Connection Factory

To verify if the connection factory with the JNDI name jms/HabariConnectionFactory exists, or to create it:

  1. In the Admin Console in your browser, expand the Resources node and JMS Resources node in the left frame.
  2. Click on the Connection Factories link in the left frame to check if the resource is registered with the server and if necessary modify the resource.

If the connection factory does not exist, you can create it in the Admin Console.

  1. Click New ...
  2. In the Pool Name field, enter jms/HabariConnectionFactory
  3. From the Resource Type field, select javax.jms.ConnectionFactory
  4. Click OK

Create the JMS Queue

To verify or create a JMS queue with the JNDI name jms/TOOL.DEFAULT and the physical name TOOL.DEFAULT,

  1. In the Admin Console in your browser, expand the Resources node and JMS Resources node in the left frame.
  2. Click on the Destination Resources link in the left frame to check if the resource is registered with the server and if necessary modify the resource.

If the queue does not exist, you can create it in the Admin Console.

  1. Click New ...
  2. In the JNDI Name field, enterjms/TOOL.DEFAULT
  3. In the Physical Destination Name field, enter TOOL.DEFAULT
  4. From the Resource Type list, select javax.jms.Topic
  5. Click OK

Test the Installation

Configure the Stomp Adapter

With a text editor, open the file

 

<glassfish31-install>/glassfish/domain1/imq/instances/imqbroker/props/config.properties

  • Add the following lines

1:
2:
3:
4:
imq.bridge.admin.user=admin
imq.bridge.admin.password=admin
imq.bridge.activelist=stomp
imq.bridge.enabled=true

  • Restart GlassFish

Run Habari ConsumerTool

Every browser request for the OpenMQServlet URL above sends a message to the TOOL.DEFAULT queue.

To receive the messages with the Habari OpenMQ Client ConsumerTool application,

  1. Start ConsumerTool
  2. Open a browser
  3. Navigate to the OpenMQServlet URL http://localhost:8080/HabariGlassFish3App/OpenMQServlet
  4. Check the GlassFish log window in the NetBeans IDE. It should show that a message has been sent:

INFO: Message sent: Tue Dec 29 16:12:11 CET 2009