2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction...

99
2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3 Point-To-Point Messaging 16.3.1 Voter Application: Overview 16.3.2 Voter Application: Sender Side 16.3.3 Voter Application: Receiver Side| 16.3.4 Voter Application: Configuring and Running 16.4 Publish/Subscribe Messaging 16.4.1 Weather Application: Overview 16.4.2 Weather Application: Publisher Side 16.4.3 Weather Application: Subscriber Side 16.4.4 Weather Application: Configuring and Running 16.5 Message-Driven Enterprise JavaBeans 16.5.1 Voter Application: Overview 16.5.2 Voter Application: Receiver Side\ 16.5.3 Voter Application: Configuring and Running

Transcript of 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction...

Page 1: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

Chapter 16: Messaging with JMSOutline16.1 Introduction16.2 Installation and Configuration of J2EE 1.316.3 Point-To-Point Messaging

16.3.1 Voter Application: Overview16.3.2 Voter Application: Sender Side16.3.3 Voter Application: Receiver Side|16.3.4 Voter Application: Configuring and Running

16.4 Publish/Subscribe Messaging16.4.1 Weather Application: Overview16.4.2 Weather Application: Publisher Side16.4.3 Weather Application: Subscriber Side16.4.4 Weather Application: Configuring and

Running16.5 Message-Driven Enterprise JavaBeans

16.5.1 Voter Application: Overview16.5.2 Voter Application: Receiver Side\16.5.3 Voter Application: Configuring and Running

Page 2: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.1 Introduction

• Message-oriented middleware– enables components to post messages for other components

– two types• point-to-point messaging model

– components send messages to message queue

• messages sent to one consumer

• publish/subscribe messaging model

– components publish message to topic on server

• multiple subscribers receive message for given topic

Page 3: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.1 Introduction (cont.)

• Message– composed of

• header

– message destination

– sending time

• properties (optional)

– server

• determines type of message being sent

– clients

• helps determine what messages to receive

• body

– content of message

Page 4: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.1 Introduction (cont.)

– composed of 5 types1. BytesMessages

2. MapMessages

3. ObjectMessages

4. StreamMessages

5. TextMessages

• Message-driven beans– Enterprise JavaBeans that support messaging

– EJB container uses any message-driven bean for given topic• message-driven beans cannot maintain clients state

– enable components to receive messages asynchronously

Page 5: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.2 Installation and Configuration of J2EE 1.3

• Steps for installing J2EE 1.3– download and unpack appropriate software bundle

• http://java.sun.com/j2ee/j2sdkee-beta/index.html

– set environment variables according to Fig. 16.1

Page 6: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.2 Installation and Configuration of J2EE 1.3 (cont.)

Environment variable

Value

J2EE_HOME Directory in which J2EE 1.3 is installed ( e.g., C:\j2sdkee1.3).

JAVA_HOME Directory in which J2SE 1.3.1 is installed ( e.g., C:\jdk1.3.1). PATH The existing PATH plus the J2EE 1.3 bin directory (e.g.,

C:\j2sdkee1.3\bin). Fig. 16.1 Setting environment variables for J 2EE 1.3 installation.

Page 7: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3 Point-To-Point Messaging

• Allows clients send messages to message queue.– receiver connects to queue to consume non-consumed

messages

• Messages intended for one receiver.• Messages stored in queue until client consumes

messages.

Page 8: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3 Point-To-Point Messaging (cont.)

Receiver

Sender

Sender

QueueMessage

Messages

Message

Fig. 16.2 Point-to-point messaging model.

Page 9: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.1 Voter Application: Overview

• Tallies votes to favorite computer languages.• Class Voter

– sends votes as messages to Votes queue• messages are simple TextMessage objects

– body contains candidate name

• Class VoteCollector– consumes messages and tallies votes

– updates display

Page 10: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.1 Voter Application: Overview (cont.)

VoteCollector

Voter Voter

TextMessage

Votes queue

TextMessages

TextMessage

Fig. 16.3 Voter application overview.

Page 11: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.2 Voter Application: Sender Side

• Consists of single class, Voter.– allows user to select programming language

– sends vote to Votes queue

Page 12: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.4 Voter class submits votes as messages to queue.

Line 13

1 // Voter.java2 // Voter is the GUI that allows the client to vote3 // for a programming language. Voter sends the vote4 // to the "Votes" queue as a TextMessage.5 package com.deitel.advjhtp1.jms.voter;6 7 // Java core packages8 import java.awt.*;9 import java.awt.event.*;10 11 // Java extension packages12 import javax.swing.*;13 import javax.jms.*;14 import javax.naming.*;15 16 public class Voter extends JFrame {17 18 private String selectedLanguage;19 20 // JMS variables21 private QueueConnection queueConnection;22 private QueueSession queueSession;23 private QueueSender queueSender;24 25 // Voter constructor26 public Voter()27 {28 // lay out user interface29 super( "Voter" );30 31 Container container = getContentPane();32 container.setLayout( new BorderLayout() );33

contains JMS API classes and interfaces

Page 13: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.4 Voter class submits votes as messages to queue.

34 JTextArea voteArea =35 new JTextArea( "Please vote for your\n" +36 "favorite programming language" );37 voteArea.setEditable( false );38 container.add( voteArea, BorderLayout.NORTH );39 40 JPanel languagesPanel = new JPanel();41 languagesPanel.setLayout( new GridLayout( 0, 1 ) );42 43 // add each language as its own JCheckBox44 // ButtonGroup ensures exactly one language selected45 ButtonGroup languagesGroup = new ButtonGroup();46 CheckBoxHandler checkBoxHandler = new CheckBoxHandler();47 String languages[] = 48 { "C", "C++", "Java", "Lisp", "Python" };49 selectedLanguage = "";50 51 // create JCheckBox for each language52 // and add to ButtonGroup and JPanel53 for ( int i = 0; i < languages.length; i++ ) {54 JCheckBox checkBox = new JCheckBox( languages[ i ] );55 checkBox.addItemListener( checkBoxHandler );56 languagesPanel.add( checkBox );57 languagesGroup.add( checkBox );58 }59 60 container.add( languagesPanel, BorderLayout.CENTER );61 62 // create button to submit vote63 JButton submitButton = new JButton( "Submit vote!" );64 container.add( submitButton, BorderLayout.SOUTH );65

Page 14: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.4 Voter class submits votes as messages to queue.

Line 92

Lines 96-100

66 // invoke method submitVote when submitButton clicked67 submitButton.addActionListener (68 69 new ActionListener() {70 71 public void actionPerformed ( ActionEvent event ) {72 submitVote();73 }74 }75 );76 77 // invoke method quit when window closed78 addWindowListener(79 80 new WindowAdapter() {81 82 public void windowClosing( WindowEvent event ) {83 quit();84 }85 }86 );87 88 // connect to message queue89 try {90 91 // create JNDI context92 Context jndiContext = new InitialContext();93 94 // retrieve queue connection factory and95 // queue from JNDI context96 QueueConnectionFactory queueConnectionFactory = 97 ( QueueConnectionFactory )98 jndiContext.lookup( "VOTE_FACTORY" );99 Queue queue = ( Queue ) jndiContext.lookup( "Votes" );100

create JNDI context

server administrator responsible for creating queue

connection factory and queue

Page 15: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.4 Voter class submits votes as messages to queue.

Lines 102-103

Lines 104-106

Lines 107

Lines 131-132

Line 133

101 // create connection, session and sender102 queueConnection = 103 queueConnectionFactory.createQueueConnection();104 queueSession =105 queueConnection.createQueueSession( false,106 Session.AUTO_ACKNOWLEDGE );107 queueSender = queueSession.createSender( queue );108 }109 110 // process Naming exception from JNDI context111 catch ( NamingException namingException ) {112 namingException.printStackTrace();113 System.exit( 1 );114 }115 116 // process JMS exception from queue connection or session117 catch ( JMSException jmsException ) {118 jmsException.printStackTrace();119 System.exit( 1 );120 }121 122 } // end Voter constructor123 124 // submit selected vote to "Votes" queue as TextMessage125 public void submitVote()126 {127 if ( selectedLanguage != "" ) {128 129 // create text message containing selected language130 try {131 TextMessage voteMessage = 132 queueSession.createTextMessage();133 voteMessage.setText( selectedLanguage );134

create QueueConnectioncreate QueueSession

post messages through QueueSender instance

message instance

set body of message

Page 16: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.4 Voter class submits votes as messages to queue.

Line 136

Line 154

135 // send the message to the queue136 queueSender.send( voteMessage );137 }138 139 // process JMS exception140 catch ( JMSException jmsException ) {141 jmsException.printStackTrace();142 }143 }144 145 } // end method submitVote146 147 // close client application148 public void quit()149 {150 if ( queueConnection != null ) {151 152 // close queue connection if it exists153 try {154 queueConnection.close();155 }156 157 // process JMS exception158 catch ( JMSException jmsException ) {159 jmsException.printStackTrace();160 }161 }162 163 System.exit( 0 );164 165 } // end method quit166

send message

close connection to queue

Page 17: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.4 Voter class submits votes as messages to queue.

167 // launch Voter application168 public static void main( String args[] )169 {170 Voter voter = new Voter();171 voter.pack();172 voter.setVisible( true );173 }174 175 // CheckBoxHandler handles event when checkbox checked176 private class CheckBoxHandler implements ItemListener {177 178 // checkbox event179 public void itemStateChanged( ItemEvent event )180 {181 // update selectedLanguage182 JCheckBox source = ( JCheckBox ) event.getSource();183 selectedLanguage = source.getText();184 }185 }186 }

Page 18: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.2 Voter Application: Sender Side (cont.)

Fig. 16.5 Voter application votes for favorite programming language

Page 19: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.3 Voter Application: Receiver Side

• Class VoteCollector intended receiver– tallies and displays votes

• Votes queue can be populated before VoteCollector connects.

Page 20: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.6 VoteCollector class retrieves and tallies votes.

1 // VoteCollector.java2 // VoteCollector tallies and displays the votes3 // posted as TextMessages to the "Votes" queue.4 package com.deitel.advjhtp1.jms.voter;5 6 // Java core packages7 import java.awt.*;8 import java.awt.event.*;9 import java.util.*;10 11 // Java extension packages12 import javax.swing.*;13 import javax.jms.*;14 import javax.naming.*;15 16 public class VoteCollector extends JFrame {17 18 private JPanel displayPanel;19 private Map tallies = new HashMap();20 21 // JMS variables22 private QueueConnection queueConnection;23 24 // VoteCollector constructor25 public VoteCollector()26 {27 super( "Vote Tallies" );28 29 Container container = getContentPane();30 31 // displayPanel will display tally results32 displayPanel = new JPanel();33 displayPanel.setLayout( new GridLayout( 0, 1 ) );34 container.add( new JScrollPane( displayPanel ) );35

Page 21: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.6 VoteCollector class retrieves and tallies votes.

Line 51

Lines 55-57

Lines 61-65

Lines 66-67

36 // invoke method quit when window closed37 addWindowListener(38 39 new WindowAdapter() {40 41 public void windowClosing( WindowEvent event ) {42 quit();43 }44 }45 );46 47 // connect to "Votes" queue48 try {49 50 // create JNDI context51 Context jndiContext = new InitialContext();52 53 // retrieve queue connection factory54 // and queue from JNDI context55 QueueConnectionFactory queueConnectionFactory =56 ( QueueConnectionFactory )57 jndiContext.lookup( "VOTE_FACTORY" );58 Queue queue = ( Queue ) jndiContext.lookup( "Votes" );59 60 // create connection, session and receiver61 queueConnection = 62 queueConnectionFactory.createQueueConnection();63 QueueSession queueSession =64 queueConnection.createQueueSession( false,65 Session.AUTO_ACKNOWLEDGE );66 QueueReceiver queueReceiver =67 queueSession.createReceiver( queue );68

create JNDI context

get queue connection factory

create QueueSession

create votes receiver

Page 22: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.6 VoteCollector class retrieves and tallies votes.

Lines 70-71

Lines 74

Lines 92-109

69 // initialize and set message listener70 queueReceiver.setMessageListener( 71 new VoteListener( this ) );72 73 // start connection74 queueConnection.start();75 }76 77 // process Naming exception from JNDI context78 catch ( NamingException namingException ) {79 namingException.printStackTrace();80 System.exit( 1 );81 }82 83 // process JMS exception from queue connection or session84 catch ( JMSException jmsException ) {85 jmsException.printStackTrace();86 System.exit( 1 );87 }88 89 } // end VoteCollector constructor90 91 // add vote to corresponding tally92 public void addVote( String vote )93 {94 if ( tallies.containsKey( vote ) ) {95 96 // if vote already has corresponding tally97 TallyPanel tallyPanel =98 ( TallyPanel ) tallies.get( vote );99 tallyPanel.updateTally();100 }101

register listener

activate connection

updates tallies and display. Callback method for

VoteListener instance

Page 23: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.6 VoteCollector class retrieves and tallies votes.

Line 118

102 // add to GUI and tallies103 else {104 TallyPanel tallyPanel = new TallyPanel( vote, 1 );105 displayPanel.add( tallyPanel );106 tallies.put( vote, tallyPanel );107 validate();108 }109 }110 111 // quit application112 public void quit()113 {114 if ( queueConnection != null ) {115 116 // close the queue connection if it exists117 try {118 queueConnection.close();119 }120 121 // process JMS exception 122 catch ( JMSException jmsException ) {123 jmsException.printStackTrace();124 System.exit( 1 );125 }126 127 }128 129 System.exit( 0 );130 131 } // end method quit132

close queue connection

Page 24: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.6 VoteCollector class retrieves and tallies votes.

133 // launch VoteCollector134 public static void main( String args[] )135 {136 VoteCollector voteCollector = new VoteCollector();137 voteCollector.setSize( 200, 200 );138 voteCollector.setVisible( true );139 }140 }

Page 25: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.3 Voter Application: Receiver Side (cont.)

Fig. 16.7 VoteCollector tallies and displays votes.

Page 26: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.8 VoteListener class receives messages from the queue.

Line 11

Line 29

Line 32

1 // VoteListener.java2 // VoteListener is the message listener for the3 // receiver of the "Votes" queue. It implements 4 // the specified onMessage method to update the 5 // GUI with the received vote.6 package com.deitel.advjhtp1.jms.voter;7 8 // Java extension packages9 import javax.jms.*;10 11 public class VoteListener implements MessageListener {12 13 private VoteCollector voteCollector;14 15 // VoteListener constructor16 public VoteListener( VoteCollector collector )17 {18 voteCollector = collector;19 }20 21 // receive new message22 public void onMessage( Message message )23 {24 TextMessage voteMessage;25 26 // retrieve and process message27 try {28 29 if ( message instanceof TextMessage ) {30 voteMessage = ( TextMessage ) message;31 String vote = voteMessage.getText();32 voteCollector.addVote( vote );33 34 System.out.println( "Received vote: " + vote );35 }

implements MessageListener

interface

ensure message of type TextMessage

call back

Page 27: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.8 VoteListener class receives messages from the queue.

36 37 else {38 System.out.println( "Expecting " +39 "TextMessage object, received " +40 message.getClass().getName() );41 }42 }43 44 // process JMS exception from message45 catch ( JMSException jmsException ) {46 jmsException.printStackTrace();47 }48 49 } // end method onMessage50 }

Page 28: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.9 TallyPanel class displays candidate name and tally.

1 // TallyPanel.java2 // TallPanel is the GUI component which displays3 // the name and tally for a vote candidate.4 package com.deitel.advjhtp1.jms.voter;5 6 // Java core packages7 import java.awt.*;8 9 // Java extension packages10 import javax.swing.*;11 12 public class TallyPanel extends JPanel {13 14 private JLabel nameLabel;15 private JTextField tallyField;16 private String name;17 private int tally;18 19 // TallyPanel constructor20 public TallyPanel( String voteName, int voteTally )21 {22 name = voteName;23 tally = voteTally;24 25 nameLabel = new JLabel( name );26 tallyField = 27 new JTextField( Integer.toString( tally ), 10 );28 tallyField.setEditable( false );29 tallyField.setBackground( Color.white );30 31 add( nameLabel );32 add( tallyField );33 34 } // end TallyPanel constructor35

Page 29: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.9 TallyPanel class displays candidate name and tally.

36 // update tally by one vote37 public void updateTally()38 {39 tally++;40 tallyField.setText( Integer.toString( tally ) );41 }42 }

increments tally by one

Page 30: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.4 Voter Application: Configuring and Running

1. Start J2EE serverj2ee –verbose

2. Create Votes queue (in new window)j2eeadmin –addJmsDestination Votes queue

3. Verify queue was createdj2eeadmin –listJmsDestination

4. Create connection factoryj2eeadmin –addJmsFactory VOTE_FACTORY queue

5. Start VoteCollectorjava –classpath %J2EE_HOME%\lib\j2ee.jar;.

-Djms.properties=%J2EE_HOME%\config\jms_client.properties

com.deitel.advjhtp1.jms.voter.VoteCollector

6. Start Voter (in new window)java –classpath %J2EE_HOME%\lib\j2ee.jar;.

-Djms.properties=%J2EE_HOME%\config\jms_client.properties

com.deitel.advjhtp1.jms.voter.Voter

Page 31: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.3.4 Voter Application: Configuring and Running (cont.)

• Once application finished– remove connection factory

j2eeadmin –removeJmsFactory VOTE_FACTORY

– remote topicj2eeadmin –removeJmsDestination Votes

– stop J2EE serverj2ee -stop

Page 32: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4 Publish/Subscribe Messaging

• Allows multiple clients to– connect to topic on server

– send messages

– receive messages

• When client publishes message, message sent to all clients subscribed to topic.

• Two subscription types:1. nondurable

• messages received while subscriptions active

2. durable

• server maintains messages while subscription inactive

– server sends messages when client reactivates

Page 33: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4 Publish/Subscribe Messaging (cont.)

Publisher

Publisher

Topic

Message

MessagesMessage

Subsc riberMessages

Subsc riber

Fig. 16.10 Publish/subscribe messaging model.

Page 34: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.1 Weather Application: Overview

• Class WeatherPublisher– retrieves weather updates from URL

– publishes information as messages to topic

• Class WeatherSubscriber– provides GUI

• enables user to select desired cities

– subscribes to Weather topic• receives corresponding messages

• uses message selector

Page 35: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.1 Weather Application: Overview (cont.)

Nationa l Weather Servic e

WeatherPublisher

Weather topic

ObjectMessage

HTML

WeatherSubscriber WeatherSubscriber

ObjectMessage ObjectMessage

Fig. 16.11 Weather application overview.

Page 36: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.2 Weather Application: Publisher Side

• Class WeatherPublisher– retrieves weather updates from National Weather Service

– publishes weather updates to Weather topic

– messages of type ObjectMessage• String property City specifies corresponding city

Page 37: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.12 WeatherPublisher class publishes messages to Weather topic.

1 // WeatherPublisher.java2 // WeatherPublisher retrieves weather conditions from the National3 // Weather Service and publishes them to the Weather topic 4 // as ObjectMessages containing WeatherBeans. The city name is 5 // used in a String property "City" in the message header.6 package com.deitel.advjhtp1.jms.weather;7 \8 // Java core packages9 import java.io.*;10 import java.net.*;11 import java.util.*;12 13 // Java extension packages14 import javax.jms.*;15 import javax.naming.*;16 17 // Deitel packages18 import com.deitel.advjhtp1.rmi.weather.WeatherBean;19 20 public class WeatherPublisher extends TimerTask {21 22 private BufferedReader in;23 private TopicConnection topicConnection;24 25 // WeatherPublisher constructor26 public WeatherPublisher()27 {28 // update weather conditions every minute29 Timer timer = new Timer();30 timer.scheduleAtFixedRate( this, 0, 60000 );31 32 // allow user to quit33 InputStreamReader inputStreamReader =34 new InputStreamReader( System.in );35 char answer = '\0';

Page 38: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.12 WeatherPublisher class publishes messages to Weather topic.

36 37 // loop until user enters q or Q38 while ( !( ( answer == 'q' ) || ( answer == 'Q' ) ) ) {39 40 // read in character41 try {42 answer = ( char ) inputStreamReader.read();43 }44 45 // process IO exception46 catch ( IOException ioException ) {47 ioException.printStackTrace();48 System.exit( 1 );49 }50 51 } // end while52 53 // close connections54 try {55 56 // close topicConnection if it exists57 if ( topicConnection != null ) {58 topicConnection.close();59 }60 61 in.close(); // close connection to NWS Web server 62 timer.cancel(); // stop timer63 }64 65 // process JMS exception from closing topic connection66 catch ( JMSException jmsException ) {67 jmsException.printStackTrace();68 System.exit( 1 );69 }70

Page 39: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.12 WeatherPublisher class publishes messages to Weather topic.

Line 90

Line 95-97

Line 103-104

71 // process IO exception from closing connection72 // to NWS Web server73 catch ( IOException ioException ) {74 ioException.printStackTrace();75 System.exit( 1 );76 }77 78 System.exit( 0 );79 80 } // end WeatherPublisher constructor81 82 // get weather information from NWS83 public void run()84 {85 // connect to topic "Weather"86 try { 87 System.out.println( "Update weather information..." );88 89 // create JNDI context90 Context jndiContext = new InitialContext();91 String topicName = "Weather";92 93 // retrieve topic connection factory and topic94 // from JNDI context95 TopicConnectionFactory topicConnectionFactory =96 ( TopicConnectionFactory )97 jndiContext.lookup( "WEATHER_FACTORY" );98 99 Topic topic =100 ( Topic ) jndiContext.lookup( topicName );101 102 // create connection, session, publisher and message103 topicConnection = 104 topicConnectionFactory.createTopicConnection();105

create JNDI context

look up TopicConnectionFactory

and Topic

create TopicConnection

Page 40: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.12 WeatherPublisher class publishes messages to Weather topic.

Lines 106-108

Lines 110-111

Lines 113-114

106 TopicSession topicSession =107 topicConnection.createTopicSession( false,108 Session.AUTO_ACKNOWLEDGE );109 110 TopicPublisher topicPublisher = 111 topicSession.createPublisher( topic );112 113 ObjectMessage message =114 topicSession.createObjectMessage();115 116 // connect to National Weather Service117 // and publish conditions to topic118 119 // National Weather Service Travelers Forecast page120 URL url = new URL(121 "http://iwin.nws.noaa.gov/iwin/us/traveler.html" );122 123 // set up text input stream to read Web page contents124 in = new BufferedReader(125 new InputStreamReader( url.openStream() ) );126 127 // helps determine starting point of data on Web page128 String separator = "TAV12"; 129 130 // locate separator string in Web page131 while ( !in.readLine().startsWith( separator ) )132 ; // do nothing133 134 // strings representing headers on Travelers Forecast135 // Web page for daytime and nighttime weather136 String dayHeader =137 "CITY WEA HI/LO WEA HI/LO";138 139 String nightHeader =140 "CITY WEA LO/HI WEA LO/HI";

create TopicSessionobtain TopicPublisher

will contain WeatherBean objects

Page 41: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.12 WeatherPublisher class publishes messages to Weather topic.

Lines 165-168

Line 173

Lines 174-175

141 142 String inputLine = "";143 144 // locate header that begins weather information145 do {146 inputLine = in.readLine();147 }148 149 while ( !inputLine.equals( dayHeader ) &&150 !inputLine.equals( nightHeader ) );151 152 // create WeatherBean objects for each city's data153 // publish to Weather topic using city as message's type154 inputLine = in.readLine(); // get first city's info155 156 // the portion of inputLine containing relevant data is157 // 28 characters long. If the line length is not at 158 // least 28 characters long, done processing data.159 while ( inputLine.length() > 28 ) {160 161 // create WeatherBean object for city162 // first 16 characters are city name163 // next six characters are weather description164 // next six characters are HI/LO temperature165 WeatherBean weather = new WeatherBean(166 inputLine.substring( 0, 16 ).trim(), 167 inputLine.substring( 16, 22 ).trim(), 168 inputLine.substring( 23, 29 ).trim() );169 170 // publish WeatherBean object with city name171 // as a message property, 172 // used for selection by clients173 message.setObject( weather );174 message.setStringProperty( "City",175 weather.getCityName() );

create WeatherBean

object

store WeatherBean in MessageObjectset City property

Page 42: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.12 WeatherPublisher class publishes messages to Weather topic.

Line 176

176 topicPublisher.publish( message );177 178 System.out.println( "published message for city: "179 + weather.getCityName() );180 181 inputLine = in.readLine(); // get next city's info182 }183 184 System.out.println( "Weather information updated." );185 186 } // end try187 188 // process Naming exception from JNDI context189 catch ( NamingException namingException ) {190 namingException.printStackTrace();191 System.exit( 1 );192 }193 194 // process JMS exception from connection,195 // session, publisher or message196 catch ( JMSException jmsException ) {197 jmsException.printStackTrace();198 System.exit( 1 );199 }200 201 // process failure to connect to National Weather Service202 catch ( java.net.ConnectException connectException ) {203 connectException.printStackTrace();204 System.exit( 1 );205 }206 207 // process other exceptions208 catch ( Exception exception ) {209 exception.printStackTrace();210 System.exit( 1 );

publish message to topic

Page 43: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.12 WeatherPublisher class publishes messages to Weather topic.

211 }212 213 } // end method run214 215 // launch WeatherPublisher216 public static void main( String args[] )217 { 218 System.err.println( "Initializing server...\n" +219 "Enter 'q' or 'Q' to quit" );220 221 WeatherPublisher publisher = new WeatherPublisher();222 }223 }

Page 44: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.2 Weather Application: Publisher Side (cont.)

Fig. 16.13 WeatherPublisher publishing weather update messages.

Page 45: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.3 Weather Application: Subscriber Side

• Subscribes to Weather topic.– receives weather updates for selected cities

• Presents GUI for user.

Page 46: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.14 WeatherSubscriber class allows user to receive weather updates.

1 // WeatherSubscriber.java2 // WeatherSubscriber presents a GUI for the client to request3 // weather conditions for various cities. The WeatherSubscriber4 // retrieves the weather conditions from the Weather topic;5 // each message body contains a WeatherBean object. The message6 // header contains a String property "City," which allows7 // the client to select the desired cities.8 package com.deitel.advjhtp1.jms.weather;9 10 // Java core packages11 import java.awt.*;12 import java.awt.event.*;13 14 // Java extension packages15 import javax.swing.*;16 import javax.naming.*;17 import javax.jms.*;18 19 public class WeatherSubscriber extends JFrame {20 21 // GUI variables22 private WeatherDisplay weatherDisplay;23 private JList citiesList;24

Page 47: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.14 WeatherSubscriber class allows user to receive weather updates.

Line 55

25 // cities contains cities for which weather26 // updates are available on "Weather" topic27 private String cities[] = { "ALBANY NY", "ANCHORAGE", 28 "ATLANTA", "ATLANTIC CITY", "BOSTON", "BUFFALO", 29 "BURLINGTON VT", "CHARLESTON WV", "CHARLOTTE", "CHICAGO", 30 "CLEVELAND", "DALLAS FT WORTH", "DENVER", "DETROIT", 31 "GREAT FALLS", "HARTFORD SPGFLD", "HONOLULU", 32 "HOUSTON INTCNTL", "KANSAS CITY", "LAS VEGAS", 33 "LOS ANGELES", "MIAMI BEACH", "MPLS ST PAUL", "NEW ORLEANS", 34 "NEW YORK CITY", "NORFOLK VA", "OKLAHOMA CITY", "ORLANDO", 35 "PHILADELPHIA", "PHOENIX", "PITTSBURGH", "PORTLAND ME", 36 "PORTLAND OR", "RENO" };37 38 // JMS variables39 private TopicConnection topicConnection;40 private TopicSession topicSession;41 private Topic topic;42 private TopicSubscriber topicSubscriber;43 private WeatherListener topicListener;44 45 // WeatherSubscriber constructor46 public WeatherSubscriber()47 {48 super( "JMS WeatherSubscriber..." );49 weatherDisplay = new WeatherDisplay();50 51 // set up JNDI context and JMS connections52 try {53 54 // create JNDI context55 Context jndiContext = new InitialContext();56

get JNDI context

Page 48: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.14 WeatherSubscriber class allows user to receive weather updates.

Lines 59-61

Line 65

Lines 68-69

Lines 72-73

Line 76

57 // retrieve topic connection factory 58 // from JNDI context59 TopicConnectionFactory topicConnectionFactory =60 ( TopicConnectionFactory ) jndiContext.lookup( 61 "WEATHER_FACTORY" );62 63 // retrieve topic from JNDI context64 String topicName = "Weather";65 topic = ( Topic ) jndiContext.lookup( topicName );66 67 // create topic connection68 topicConnection = 69 topicConnectionFactory.createTopicConnection();70 71 // create topic session72 topicSession = topicConnection.createTopicSession( false,73 Session.AUTO_ACKNOWLEDGE );74 75 // initialize listener 76 topicListener = new WeatherListener( weatherDisplay );77 }78 79 // process Naming exception from JNDI context80 catch ( NamingException namingException ) {81 namingException.printStackTrace();82 }83 84 // process JMS exceptions from topic connection or session85 catch ( JMSException jmsException ) {86 jmsException.printStackTrace();87 }88 89 // lay out user interface90 Container container = getContentPane();91 container.setLayout( new BorderLayout() );

obtain TopicConnectionFactory

create Topic

create TopicConnection

create TopicSession

initialize WeatherListener

Page 49: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.14 WeatherSubscriber class allows user to receive weather updates.

92 93 JPanel selectionPanel = new JPanel();94 selectionPanel.setLayout( new BorderLayout() );95 96 JLabel selectionLabel = new JLabel( "Select Cities" );97 selectionPanel.add( selectionLabel, BorderLayout.NORTH );98 99 // create list of cities for which users100 // can request weather updates101 citiesList = new JList( cities );102 selectionPanel.add( new JScrollPane( citiesList ),103 BorderLayout.CENTER );104 105 JButton getWeatherButton = new JButton( "Get Weather..." );106 selectionPanel.add( getWeatherButton, BorderLayout.SOUTH );107 108 // invoke method getWeather when getWeatherButton clicked109 getWeatherButton.addActionListener (110 111 new ActionListener() {112 113 public void actionPerformed ( ActionEvent event ) 114 {115 getWeather();116 }117 }118 119 ); // end call to addActionListener120 121 container.add( selectionPanel, BorderLayout.WEST );122 container.add( weatherDisplay, BorderLayout.CENTER );123 124 // invoke method quit when window closed125 addWindowListener(126

Page 50: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.14 WeatherSubscriber class allows user to receive weather updates.

Line 153

127 new WindowAdapter() {128 129 public void windowClosing( WindowEvent event ) 130 {131 quit();132 }133 }134 135 ); // end call to addWindowListener136 137 } // end WeatherSubscriber constructor138 139 // get weather information for selected cities140 public void getWeather()141 {142 // retrieve selected indices143 int selectedIndices[] = citiesList.getSelectedIndices();144 145 if ( selectedIndices.length > 0 ) {146 147 // if topic subscriber exists, method has148 // been called before149 if ( topicSubscriber != null ) {150 151 // close previous topic subscriber152 try {153 topicSubscriber.close(); 154 }155 156 // process JMS exception157 catch ( JMSException jmsException ) {158 jmsException.printStackTrace();159 }160

remove previous subscriber so that new subscriber can filter newly selected cities

Page 51: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.14 WeatherSubscriber class allows user to receive weather updates.

Lines 166-173

Lines 177-178

Line 180

161 // clear previous cities from display162 weatherDisplay.clearCities();163 }164 165 // create message selector to retrieve specified cities166 StringBuffer messageSelector = new StringBuffer();167 messageSelector.append( 168 "City = '" + cities[ selectedIndices[ 0 ] ] + "'" );169 170 for ( int i = 1; i < selectedIndices.length; i++ ) {171 messageSelector.append( " OR City = '" +172 cities[ selectedIndices[ i ] ] + "'" );173 }174 175 // create topic subscriber and subscription176 try {177 topicSubscriber = topicSession.createSubscriber( 178 topic, messageSelector.toString(), false );179 topicSubscriber.setMessageListener( topicListener );180 topicConnection.start();181 182 JOptionPane.showMessageDialog( this,183 "A weather update should be arriving soon..." );184 }185 186 // process JMS exception187 catch ( JMSException jmsException ) {188 jmsException.printStackTrace();189 }190 191 } // end if192 193 } // end method getWeather194

create MessageSelector

create TopicSubscriber

begin receiving messages

Page 52: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.14 WeatherSubscriber class allows user to receive weather updates.

Line 203

Line 207

195 // quit WeatherSubscriber application196 public void quit()197 {198 // close connection and subscription to topic199 try {200 201 // close topic subscriber202 if ( topicSubscriber != null ) {203 topicSubscriber.close(); 204 }205 206 // close topic connection 207 topicConnection.close();208 }209 210 // process JMS exception211 catch ( JMSException jmsException ) {212 jmsException.printStackTrace();213 System.exit( 1 );214 }215 216 System.exit( 0 );217 218 } // end method quit219 220 // launch WeatherSubscriber application221 public static void main( String args [] )222 {223 WeatherSubscriber subscriber = new WeatherSubscriber();224 subscriber.pack();225 subscriber.setVisible( true );226 }227 }

close subscriber

close connection

Page 53: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.3 Weather Application: Subscriber Side (cont.)

Fig. 16.15 WeatherSubscriber selecting cities for weather updates.

Page 54: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.3 Weather Application: Subscriber Side (cont.)

Fig. 16.16 WeatherSubscriber having received updated weather conditions.

Page 55: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.17 WeatherListener class subscribes to Weather topic to receive weather forecasts.

Line 15

Line 32

1 // WeatherListener.java2 // WeatherListener is the MessageListener for a subscription3 // to the Weather topic. It implements the specified onMessage4 // method to update the GUI with the corresponding city's5 // weather.6 package com.deitel.advjhtp1.jms.weather;7 8 // Java extension packages9 import javax.jms.*;10 import javax.swing.*;11 12 // Deitel packages13 import com.deitel.advjhtp1.rmi.weather.WeatherBean;14 15 public class WeatherListener implements MessageListener {16 17 private WeatherDisplay weatherDisplay;18 19 // WeatherListener constructor20 public WeatherListener( WeatherDisplay display )21 {22 weatherDisplay = display;23 }24 25 // receive new message26 public void onMessage( Message message )27 {28 // retrieve and process message29 try {30 31 // ensure Message is an ObjectMessage32 if ( message instanceof ObjectMessage ) {33

implements MessageListener

ensures message of type ObjectMessage

Page 56: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.17 WeatherListener class subscribes to Weather topic to receive weather forecasts.

Lines 35-38

Line 41

34 // get WeatherBean from ObjectMessage35 ObjectMessage objectMessage = 36 ( ObjectMessage ) message;37 WeatherBean weatherBean =38 ( WeatherBean ) objectMessage.getObject();39 40 // add WeatherBean to display41 weatherDisplay.addItem( weatherBean );42 43 } // end if44 45 else {46 System.out.println( "Expected ObjectMessage," +47 " but received " + message.getClass().getName() );48 }49 50 } // end try51 52 // process JMS exception from message53 catch ( JMSException jmsException ) {54 jmsException.printStackTrace();55 }56 57 } // end method onMessage58 }

obtain WeatherBean

display contents

Page 57: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.18 WeatherDisplay displays WeatherBeans in a Jlist using a WeatherCellRenderer.

1 // WeatherDisplay.java2 // WeatherDisplay extends JPanel to display results3 // of client's request for weather conditions.4 package com.deitel.advjhtp1.jms.weather;5 6 // Java core packages7 import java.awt.*;8 import java.awt.event.*;9 import java.util.*;10 11 // Java extension packages12 import javax.swing.*;13 14 // Deitel packages15 import com.deitel.advjhtp1.rmi.weather.*;16 17 public class WeatherDisplay extends JPanel {18 19 // WeatherListModel and Map for storing WeatherBeans20 private WeatherListModel weatherListModel;21 private Map weatherItems;22 23 // WeatherDisplay constructor24 public WeatherDisplay()25 {26 setLayout( new BorderLayout() );27 28 ImageIcon headerImage = new ImageIcon( 29 WeatherDisplay.class.getResource( 30 "images/header.jpg" ) );31 add( new JLabel( headerImage ), BorderLayout.NORTH );32

Page 58: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.18 WeatherDisplay displays WeatherBeans in a Jlist using a WeatherCellRenderer.

Lines 56-58

Lines 62-63

33 // use JList to display updated weather conditions34 // for requested cities35 weatherListModel = new WeatherListModel();36 JList weatherJList = new JList( weatherListModel );37 weatherJList.setCellRenderer( new WeatherCellRenderer() );38 39 add( new JScrollPane( weatherJList ), BorderLayout.CENTER );40 41 // maintain WeatherBean items in HashMap 42 weatherItems = new HashMap();43 44 } // end WeatherDisplay constructor45 46 // add WeatherBean item to display47 public void addItem( WeatherBean weather )48 {49 String city = weather.getCityName();50 51 // check whether city is already in display52 if ( weatherItems.containsKey( city ) ) {53 54 // if city is in Map, and therefore in display55 // remove previous WeatherBean object56 WeatherBean previousWeather = 57 ( WeatherBean ) weatherItems.remove( city );58 weatherListModel.remove( previousWeather ); 59 }60 61 // add WeatherBean to Map and WeatherListModel62 weatherListModel.add( weather );63 weatherItems.put( city, weather );64 65 } // end method addItem

remove if bean previously existing

add new bean to list

Page 59: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.18 WeatherDisplay displays WeatherBeans in a Jlist using a WeatherCellRenderer.

66 67 // clear all cities from display68 public void clearCities()69 {70 weatherItems.clear();71 weatherListModel.clear();72 }73 }

Page 60: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.4 Weather Application: Configuring and Running

1. Start J2EE serverj2ee –verbose

2. Create Weather topic (new window)j2eeadmin –addJmsDestination Weather topic

3. Verify topic selectedj2eeadmin –listJmsDestination

4. Create connection factoryj2eeadmin –addJmsFactory WEATHER_FACTORY topic

5. Start WeatherPublisherjava –classpath %J2EE_HOME%\lib\j2ee.jar;.

-Djms.properties=%J2EE_HOME%\config\jms_client.propertiescom.deitel.advjhtp1.jms.weather.WeatherPublisher

6. Start WeatherSubscriber java –classpath %J2EE_HOME%\lib\j2ee.jar;.

-Djms.properties=%J2EE_HOME%\config\jms_client.propertiescom.deitel.advjhtp1.jmx.weather.WeatherSubscriber

Page 61: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.4.4 Weather Application: Configuring and Running (cont.)

• To close application1. remove factory

j2eeadmin –removeJmsFactory WEATHER_FACTORY

2. remove topicj2eeadmin –removeJmsDestination Weather

3. stop J2EE serverj2ee -stop

Page 62: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5 Message-Driven Enterprise JavaBeans

• New type of EJB available in EJB version 2.0.– part of Java 2 Enterprise Edition version 1.3

• When message is received– container uses any available message-driven bean

• message-driven beans handle messages from multiple clients

• Must not maintain state.• Developers provide only bean implementation.

Page 63: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.1 Voter Application: Overview

• Class Voter– posts vote messages to queue

– not concerned with receiver’s implementation• benefit of loosely coupled systems

• Class VoteCollectorEJB– message-bean implementation

– delegates message to entity-bean Candidate

• Class Candidate– tallies votes

– stores results in database Voting

• Class TallyDisplay– presents GUI with tallies from database

Page 64: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.1 Voter Application: Overview (cont.)

VoteCollectorEJB

Voter Voter

TextMessage

Votes queue

TextMessages

TextMessage

Fig. 16.19 Voter application overview.

Page 65: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.2 Voter Application: Receiver Side

• Candidate entity EJB represents– vote candidate for which users vote

– number of votes for candidate

• Uses container managed persistence.

Page 66: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.20 CandidateHome interface for Candidate EJB.

Lines 15-16

Lines 19-20

Lines 23-24

1 // CandidateHome.java2 // CandidateHome is the home interface for the Candidate EJB.3 package com.deitel.advjhtp1.jms.mdb;4 5 // Java core libraries6 import java.rmi.*;7 import java.util.*;8 9 // Java standard extensions10 import javax.ejb.*;11 12 public interface CandidateHome extends EJBHome {13 14 // find Candidate with given name15 public Candidate findByPrimaryKey( String candidateName )16 throws RemoteException, FinderException;17 18 // find all Candidates19 public Collection findAllCandidates()20 throws RemoteException, FinderException;21 22 // create new Candidate EJB23 public Candidate create( String candidateName ) 24 throws RemoteException, CreateException;25 }

locate particular Candidate

locate Collection of Candidates

create new Candidate with given name and 0 votes

Page 67: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.21 Candidate remote interface for Candidate EJB.

Line 15

Line 18

Line 21

1 // Candidate.java2 // Candidate is the remote interface for the Candidate3 // EJB, which maintains a tally of votes.4 package com.deitel.advjhtp1.jms.mdb;5 6 // Java core libraries7 import java.rmi.RemoteException;8 9 // Java standard extensions10 import javax.ejb.EJBObject;11 12 public interface Candidate extends EJBObject {13 14 // place vote for this Candidate15 public void incrementVoteCount() throws RemoteException;16 17 // get total vote count for this Candidate18 public Integer getVoteCount() throws RemoteException;19 20 // get Candidate's name21 public String getCandidateName() throws RemoteException;22 }

add vote to Candidate

current vote tally

candidate’s name

Page 68: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.22 CandidateEJB class to maintain candidate tallies.

Lines 17-18

Lines 21-25

1 // CandidateEJB.java2 // CandidateEJB is an entity EJB that uses container-managed3 // persistence to persist its Candidate and its vote tally.4 package com.deitel.advjhtp1.jms.mdb;5 6 // Java core libraries7 import java.rmi.RemoteException;8 9 // Java standard extensions10 import javax.ejb.*;11 12 public class CandidateEJB implements EntityBean {13 14 private EntityContext entityContext;15 16 // container-managed fields17 public Integer voteCount;18 public String name; 19 20 // place vote for this Candidate21 public void incrementVoteCount()22 {23 int newVoteCount = voteCount.intValue() + 1;24 voteCount = new Integer( newVoteCount );25 }26 27 // get total vote count for this Candidate28 public Integer getVoteCount()29 {30 return voteCount;31 }32

container-managed fields voteCount and name

increment vote tally

Page 69: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.22 CandidateEJB class to maintain candidate tallies.

Lines 34-37

Lines 40-47

33 // get Candidate's name34 public String getCandidateName()35 {36 return name;37 } 38 39 // create new Candidate40 public String ejbCreate( String candidateName ) 41 throws CreateException42 { 43 name = candidateName;44 voteCount = new Integer( 0 );45 46 return null;47 }48 49 // do post-creation tasks when creating new Candidate50 public void ejbPostCreate( String candidateName ) {}51 52 // set EntityContext53 public void setEntityContext( EntityContext context )54 {55 entityContext = context;56 }57 58 // unset EntityContext59 public void unsetEntityContext() 60 {61 entityContext = null;62 }63 64 // activate Candidate instance65 public void ejbActivate() 66 {67 name = ( String ) entityContext.getPrimaryKey();

return candidate name

initializes new candidate with specified name and initializes vote count to 0

Page 70: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.22 CandidateEJB class to maintain candidate tallies.

68 }69 70 // passivate Candidate instance71 public void ejbPassivate() 72 {73 name = null;74 }75 76 // load Candidate from database77 public void ejbLoad() {}78 79 // store Candidate in database80 public void ejbStore() {}81 82 // remove Candidate from database83 public void ejbRemove() {} 84 }

Page 71: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.23 VoteCollectorEJB class tallies votes from Votes queue.

Lines 21-48

1 // VoteCollectorEJB.java2 // VoteCollectorEJB is a MessageDriven EJB that tallies votes.3 package com.deitel.advjhtp1.jms.mdb;4 5 // Java core packages6 import java.util.*;7 import java.rmi.*;8 9 // Java extension packages10 import javax.ejb.*;11 import javax.rmi.*;12 import javax.jms.*;13 import javax.naming.*;14 15 public class VoteCollectorEJB16 implements MessageDrivenBean, MessageListener {17 18 private MessageDrivenContext messageDrivenContext;19 20 // receive new message21 public void onMessage( Message message )22 {23 TextMessage voteMessage;24 25 // retrieve and process message26 try {27 28 if ( message instanceof TextMessage ) {29 voteMessage = ( TextMessage ) message;30 String vote = voteMessage.getText();31 countVote( vote );32 33 System.out.println( "Received vote: " + vote ); 34 } // end if35

called by container whenever vote message

is placed in queue

Page 72: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.23 VoteCollectorEJB class tallies votes from Votes queue.

Lines 60-67

36 else {37 System.out.println( "Expecting " +38 "TextMessage object, received " +39 message.getClass().getName() );40 }41 42 } // end try43 44 // process JMS exception from message45 catch ( JMSException jmsException ) {46 jmsException.printStackTrace();47 }48 }49 50 // add vote to corresponding tally51 private void countVote( String vote )52 { 53 // CandidateHome reference for finding/creating Candidates54 CandidateHome candidateHome = null;55 56 // find Candidate and increment vote count57 try {58 59 // look up Candidate EJB60 Context initialContext = new InitialContext();61 62 Object object = initialContext.lookup( 63 "java:comp/env/ejb/Candidate" );64 65 candidateHome = 66 ( CandidateHome ) PortableRemoteObject.narrow( 67 object, CandidateHome.class );68

obtain reference to Candidate EJB

Page 73: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.23 VoteCollectorEJB class tallies votes from Votes queue.

Lines 70-71

Line 74

Lines 83-84

69 // find Candidate for whom the user voted70 Candidate candidate = 71 candidateHome.findByPrimaryKey( vote ); 72 73 // increment Candidate's vote count74 candidate.incrementVoteCount();75 76 } // end try77 78 // if Candidate not found, create new Candidate79 catch ( FinderException finderException ) { 80 81 // create new Candidate and increment its vote count82 try {83 Candidate newCandidate = candidateHome.create( vote );84 newCandidate.incrementVoteCount();85 }86 87 // handle exceptions creating new Candidate88 catch ( Exception exception ) {89 throw new EJBException( exception );90 }91 92 } // end FinderException catch93 94 // handle exception when looking up OrderProducts EJB95 catch ( NamingException namingException ) { 96 throw new EJBException( namingException ); 97 } 98 99 // handle exception when invoking OrderProducts methods100 catch ( RemoteException remoteException ) { 101 throw new EJBException( remoteException ); 102 }103

find particular Candidateincrement vote count

create new Candidate if specified candidate

name not found

Page 74: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.23 VoteCollectorEJB class tallies votes from Votes queue.

104 } // end method countVote105 106 // set message driven context107 public void setMessageDrivenContext( 108 MessageDrivenContext context )109 {110 messageDrivenContext = context;111 } 112 113 // create bean instance114 public void ejbCreate() {}115 116 // remove bean instance117 public void ejbRemove() {}118 }

Page 75: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.24 TallyDisplay displays candidate tallies from database.

1 // TallyDisplay.java2 // TallyDisplay displays the votes from database.3 package com.deitel.advjhtp1.jms.mdb;4 5 // Java core packages6 import java.awt.*;7 import java.awt.event.*;8 import java.rmi.*;9 import java.util.*;10 import java.util.List;11 12 // Java extension packages13 import javax.swing.*;14 import javax.ejb.*;15 import javax.rmi.*;16 import javax.naming.*;17 18 public class TallyDisplay extends JFrame {19 20 // TallyDisplay constructor21 public TallyDisplay()22 {23 super( "Vote Tallies" );24 25 Container container = getContentPane();26 27 // displayPanel displays tally results28 JPanel displayPanel = new JPanel();29 displayPanel.setLayout( new GridLayout( 0, 1 ) );30 container.add( new JScrollPane( displayPanel ) );31

Page 76: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.24 TallyDisplay displays candidate tallies from database.

Lines 36-46

Lines 50-59

32 // find Candidates and display tallies33 try {34 35 // look up Candidate EJB36 Context initialContext = new InitialContext();37 38 Object object = initialContext.lookup( 39 "Candidate" );40 CandidateHome candidateHome = 41 ( CandidateHome ) PortableRemoteObject.narrow( 42 object, CandidateHome.class );43 44 // find all Candidates45 Collection candidates = 46 candidateHome.findAllCandidates();47 48 // add TallyPanel with candidate name and49 // vote count for each candidate50 Iterator iterator = candidates.iterator();51 while ( iterator.hasNext() ) {52 Candidate candidate = ( Candidate ) iterator.next();53 54 // create TallyPanel for Candidate55 TallyPanel tallyPanel =56 new TallyPanel( candidate.getCandidateName(),57 candidate.getVoteCount().intValue() );58 displayPanel.add( tallyPanel );59 }60 61 } // end try62 63 // handle exception finding Candidates64 catch ( FinderException finderException ) { 65 finderException.printStackTrace();

lookup all Candidates

display snapshot of information

Page 77: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.24 TallyDisplay displays candidate tallies from database.

66 } 67 // handle exception looking up Candidate EJB68 catch ( NamingException namingException ) { 69 namingException.printStackTrace(); 70 } 71 72 // handle exception communicating with Candidate73 catch ( RemoteException remoteException ) { 74 remoteException.printStackTrace(); 75 }76 77 } // end TallyDisplay constructor78 79 // launch TallyDisplay application80 public static void main( String args[] )81 {82 TallyDisplay tallyDisplay = new TallyDisplay();83 tallyDisplay.setDefaultCloseOperation( EXIT_ON_CLOSE );84 tallyDisplay.pack();85 tallyDisplay.setVisible( true );86 }87 }

Page 78: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.2 Voter Application: Receiver Side (cont.)

Fig. 16.25 TallyDisplay displays candidate tallies from database.

Page 79: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.26 TallyPanel class displays the name and tally for a candidate.

1 // TallyPanel.java2 // TallPanel is the GUI component which displays3 // the name and tally for a vote candidate.4 package com.deitel.advjhtp1.jms.mdb;5 6 // Java core packages7 import java.awt.*;8 9 // Java extension packages10 import javax.swing.*;11 12 public class TallyPanel extends JPanel {13 14 private JLabel nameLabel;15 private JTextField tallyField;16 private String name;17 private int tally;18 19 // TallyPanel constructor20 public TallyPanel( String voteName, int voteTally )21 {22 name = voteName;23 tally = voteTally;24 25 nameLabel = new JLabel( name );26 tallyField = 27 new JTextField( Integer.toString( tally ), 10 );28 tallyField.setEditable( false );29 tallyField.setBackground( Color.white );30 31 add( nameLabel );32 add( tallyField );

Page 80: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall.All rights reserved.

Outline

Fig. 16.26 TallyPanel class displays the name and tally for a candidate.

33 34 } // end TallyPanel constructor35 36 }

Page 81: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running

• Apply to file resource.propertiesjdbcDataSource.number.name=jdbc/Voting

jdbcDataSource.number.url=jdbc:cloudscape:rmi:VotingDB;create=true

where number is next number in jdbcDataSource entries.

• Start J2EE serverj2ee –verbose

• Create queuej2eeadmin –addJmsDestination Votes queue

• Create connection factoryj2eeadmin –addJmsFactory VOTE_FACTORY queue

• Run deploy tooldeploytool

Page 82: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

• Within deploy tool, create new applicationFile -> New ApplicationBrowse

• navigate to parent directory of com package structure

File• enter VoteCollectorApp.ear as file name

New ApplicationOK

• Add Candidate EJBFile -> New Enterprise Bean

Page 83: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.27 EJB JAR settings for VoteCollectorApp application.

Page 84: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.28 Add class files for Candidate EJB.

Page 85: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.29 General settings for Candidate EJB.

Page 86: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.30 Entity settings for Candidate EJB.

Page 87: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.31 Entity tab for Candidate EJB.

Page 88: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.32 Database settings for Candidate EJB.

Page 89: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.33 SQL generation for Candidate EJB.

Page 90: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.34 SQL warning for Candidate EJB.

Page 91: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

• Create VoteCollectorFile -> New Enterprise Bean

Page 92: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.35 EJB JAR settings for VoteCollector EJB.

Page 93: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.36 Add class file for VoteCollector EJB.

Page 94: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.37 General settings for VoteCollector EJB.

Page 95: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.38 Transaction management settings for the VoteCollector EJB

Page 96: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.39 Message-Driven Bean settings for VoteCollector EJB.

Page 97: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.40 Enterprise Bean References for VoteCollector EJB.

Page 98: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.41 Setting JNDI names for VoteCollectorApp.

Page 99: 2002 Prentice Hall. All rights reserved. Chapter 16: Messaging with JMS Outline 16.1 Introduction 16.2 Installation and Configuration of J2EE 1.3 16.3.

2002 Prentice Hall. All rights reserved.

16.5.3 Voter Application: Configuring and Running (cont.)

Fig. 16.42 Deploying the VoteCollector application.