LDSTechForumProjects

Streams AQ

Prerequisites

Slides

File:StreamsAQ.pdf

Lab

Setup

  • Download the File:Streams-aq.zip.
  • Import the project into the LDS Tech IDE.
  • Start the LDS Tech Oracle XE VM
  • (Optional) Download and install Oracle SQL Developer.

Configure Streams AQ

  • Create a database migration script named 002-streamsaq.fwd.sql in the streams-aq-db module under the path schema-versions/DEFAULT
  • Open the newly created migration script and add the following:
begin
    -- Create the queue table
    dbms_aqadm.create_queue_table(
        queue_table=>'QUEUE_TABLE',
        queue_payload_type=>'SYS.AQ$_JMS_MESSAGE',
        multiple_consumers=>false,
        comment=>'This is the queue table used for the Streams AQ Training Lab'
    );

    -- Create the queue
    dbms_aqadm.create_queue(
        queue_name => 'queue',
        queue_table => 'queue_table',
        comment => 'This is the queue used for the Streams AQ Training Lab.'
    );

    -- Start the queue
    dbms_aqadm.start_queue( queue_name => 'queue' );
end;
/

Be sure to include the "/" after the "end;" statement!

  • Run the database migration script using the 'stack-db:migrate' Maven target.
    • This can be done by right clicking on the db module and selecting Run As | Migrate Database
    • Alternatively, you can run Maven from the command line using mvn stack-db:migrate

If the database migration runs successfully, you should be ready to start using Streams AQ.

Verify Streams AQ Has Been Configure (Optional)

  • Start Oracle SQL developer
  • Connect to your database
  • Under the "Queues Tables" section, make sure you see your queue table
  • Under the "Queues" section, make sure you see your queue

Configure a Spring JmsTemplate

You will need a a Spring JmsTemplate instance to enqueue messages.

  • Open the "applicationContext.xml" file in the src/main/resources directory in the web module of your application.
  • Add the following to the applicationContext.xml file you just opened:
<stack-jms:jms>
    <stack-jms:destination destination="queue" />
</stack-jms:jms>

Use the Spring JmsTemplate in Code

  • Open the class org.lds.stack.training.jms.view.JmsController
  • Add the following field to the controller
@Inject
private JmsTemplate jmsTemplate;
  • In the send method, add the following code:
jmsTemplate.send(new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
        return session.createTextMessage(text);
    }
});

Run Your Application and Queue Messages

  • Start your application by right clicking on the web module and TODO Put exact text here for starting Tomcat
  • Navigate your browser to [1]
  • Type some text in the message box and press the Queue button.
  • (Optional) Using SQL Developer, select QUEUE_TABLES under the Queues Tables section under your database in the Connections view.
  • (Optional) After the QUEUE_TABLES tab opens, select the data sub-tab to see your message in the queue.
  • (Optional) Queue more messages using the web interface and make sure they appear your queue table.

Implement and Configure a MessageListener

  • Create the class MessageListenerImpl in the org.lds.stack.training.jms.service package.
  • Have the MessageListenerImpl class you just created implement the interface javax.jms.MessageListener. This will require you to add an onMessage method to you class.
  • Inside the onMessage method, add the following:
try {
	TextMessage textMessage = (TextMessage) message;
	System.out.println(textMessage.getText());
} catch (Exception e) {
	throw new RuntimeException(e);
}

Note: The message variable in the second line should be the same name as the parameter passed to the onMessage method.

  • To configure the message listener, go back to the applicationContext.xml file and add the following bean definition:
<bean id="messageListener" class="org.lds.stack.training.jms.service.MessageListenerImpl" />
  • Once the message listener bean has been defined, we need to configure a JMS message listener. Place the following inside the <stack-jms:jms> element.
<stack-jms:listener-container>
    <stack-jms:listener destination="queue" ref="messageListener" />
</stack-jms:listener-container>

Note: We already have an ExecutorService created for you so you don't need to add this to your project.

Test the MessageListener

  • Restart Tomcat
  • Watch your console output, you should see all the queued up messages getting dequeued.
  • (Optional) Go to SQL Developer and make sure your queue table is empty.
This page was last modified on 15 February 2012, at 12:00.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.