Chapter 16 Messaging with JMS Outline 16 1
- Slides: 99
Chapter 16: Messaging with JMS Outline 16. 1 Introduction 16. 2 Installation and Configuration of J 2 EE 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 Java. Beans 16. 5. 1 Voter Application: Overview 16. 5. 2 Voter Application: Receiver Side 16. 5. 3 Voter Application: Configuring and Running 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 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 2002 Prentice Hall. All rights reserved.
16. 1 Introduction (cont. ) – composed of 5 types 1. 2. 3. 4. 5. • Bytes. Messages Map. Messages Object. Messages Stream. Messages Text. Messages Message-driven beans – Enterprise Java. Beans 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 2002 Prentice Hall. All rights reserved.
16. 2 Installation and Configuration of J 2 EE 1. 3 • Steps for installing J 2 EE 1. 3 – download and unpack appropriate software bundle • http: //java. sun. com/j 2 ee/j 2 sdkee-beta/index. html – set environment variables according to Fig. 16. 1 2002 Prentice Hall. All rights reserved.
16. 2 Installation and Configuration of J 2 EE 1. 3 (cont. ) 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. 2002 Prentice Hall. All rights reserved.
16. 3 Point-To-Point Messaging (cont. ) Fig. 16. 2 Point-to-point messaging model. 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 Text. Message objects – body contains candidate name • Class Vote. Collector – consumes messages and tallies votes – updates display 2002 Prentice Hall. All rights reserved.
16. 3. 1 Voter Application: Overview (cont. ) Fig. 16. 3 Voter application overview. 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 2002 Prentice Hall. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // Voter. java // Voter is the GUI that allows the client to vote // for a programming language. Voter sends the vote // to the "Votes" queue as a Text. Message. package com. deitel. advjhtp 1. jms. voter; Outline // Java core packages import java. awt. *; import java. awt. event. *; // Java extension packages import javax. swing. *; import javax. jms. *; import javax. naming. *; contains JMS API classes and interfaces public class Voter extends JFrame { private String selected. Language; // JMS variables private Queue. Connection queue. Connection ; private Queue. Session queue. Session ; private Queue. Sender queue. Sender ; Fig. 16. 4 Voter class submits votes as messages to queue. Line 13 // Voter constructor public Voter() { // lay out user interface super( "Voter" ); Container container = get. Content. Pane(); container. set. Layout( new Border. Layout() ); 2002 Prentice Hall. All rights reserved.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 JText. Area vote. Area = new JText. Area( "Please vote for yourn" + "favorite programming language" ); vote. Area. set. Editable( false ); container. add( vote. Area, Border. Layout. NORTH ); Outline JPanel languages. Panel = new JPanel(); languages. Panel. set. Layout( new Grid. Layout( 0, 1 ) ); // add each language as its own JCheck. Box // Button. Group ensures exactly one language selected Button. Group languages. Group = new Button. Group(); Check. Box. Handler check. Box. Handler = new Check. Box. Handler(); String languages[] = { "C", "C++", "Java", "Lisp", "Python" }; selected. Language = ""; // create JCheck. Box for each language // and add to Button. Group and JPanel for ( int i = 0; i < languages. length; i++ ) { JCheck. Box check. Box = new JCheck. Box( languages[ i ] ); check. Box. add. Item. Listener( check. Box. Handler ); languages. Panel. add( check. Box ); languages. Group. add( check. Box ); } Fig. 16. 4 Voter class submits votes as messages to queue. container. add( languages. Panel, Border. Layout. CENTER ); // create button to submit vote JButton submit. Button = new JButton( "Submit vote!" ); container. add( submit. Button, Border. Layout. SOUTH ); 2002 Prentice Hall. All rights reserved.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 // invoke method submit. Vote when submit. Button clicked submit. Button. add. Action. Listener ( Outline new Action. Listener() { public void action. Performed ( Action. Event event ) { submit. Vote (); } } ); // invoke method quit when window closed add. Window. Listener( new Window. Adapter() { public void window. Closing( Window. Event event ) { quit(); } } Fig. 16. 4 Voter class submits votes as messages to queue. ); Line 92 // connect to message queue try { Lines 96 -100 // create JNDI context Context jndi. Context = new Initial. Context(); // retrieve queue connection factory and // queue from JNDI context Queue. Connection. Factory queue. Connection. Factory = ( Queue. Connection. Factory ) jndi. Context. lookup( "VOTE_FACTORY" ); Queue queue = ( Queue ) jndi. Context. lookup( "Votes" ); create JNDI context server administrator responsible for creating queue connection factory and 2002 queue Prentice Hall. All rights reserved.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 // create connection, session and sender queue. Connection = queue. Connection. Factory. create. Queue. Connection(); queue. Session = queue. Connection. create. Queue. Session( false, Session. AUTO_ACKNOWLEDGE ); queue. Sender = queue. Session. create. Sender( queue ); } // process Naming exception from JNDI context catch ( Naming. Exception naming. Exception ) { naming. Exception. print. Stack. Trace(); System. exit( 1 ); } // process JMS exception from queue connection or session catch ( JMSException jms. Exception ) { jms. Exception. print. Stack. Trace(); System. exit( 1 ); } } // end Voter constructor // submit selected vote to "Votes" queue as Text. Message public void submit. Vote() { if ( selected. Language != "" ) { // create text message containing selected language try { Text. Message vote. Message = queue. Session. create. Text. Message(); vote. Message. set. Text( selected. Language ); Outline create Queue. Connection create. Voter Fig. 16. 4 Queue. Session class submits votes as messages to post messages through queue. Queue. Sender instance Lines 102 -103 Lines 104 -106 Lines 107 Lines 131 -132 Line 133 message instance set body of message 2002 Prentice Hall. All rights reserved.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 // send the message to the queue. Sender. send( vote. Message ); Outline } // process JMS exception catch ( JMSException jms. Exception ) { jms. Exception. print. Stack. Trace(); } send message } } // end method submit. Vote // close client application public void quit() { if ( queue. Connection != null ) { // close queue connection if it exists try { queue. Connection. close(); } // process JMS exception catch ( JMSException jms. Exception ) { jms. Exception. print. Stack. Trace(); } Fig. 16. 4 Voter class submits votes as messages to queue. Line 136 to queue close connection Line 154 } System. exit( 0 ); } // end method quit 2002 Prentice Hall. All rights reserved.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 // launch Voter application public static void main( String args[] ) { Voter voter = new Voter(); voter. pack(); voter. set. Visible( true ); } Outline // Check. Box. Handler handles event when checkbox checked private class Check. Box. Handler implements Item. Listener { // checkbox event public void item. State. Changed( Item. Event event ) { // update selected. Language JCheck. Box source = ( JCheck. Box ) event. get. Source(); selected. Language = source. get. Text(); } } } Fig. 16. 4 Voter class submits votes as messages to queue. 2002 Prentice Hall. All rights reserved.
16. 3. 2 Voter Application: Sender Side (cont. ) Fig. 16. 5 Voter application votes for favorite programming language 2002 Prentice Hall. All rights reserved.
16. 3. 3 Voter Application: Receiver Side • Class Vote. Collector intended receiver – tallies and displays votes • Votes queue can be populated before Vote. Collector connects. 2002 Prentice Hall. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Vote. Collector. java // Vote. Collector tallies and displays the votes // posted as Text. Messages to the "Votes" queue. package com. deitel. advjhtp 1. jms. voter; Outline // Java core packages import java. awt. *; import java. awt. event. *; import java. util. *; // Java extension packages import javax. swing. *; import javax. jms. *; import javax. naming. *; public class Vote. Collector extends JFrame { private JPanel display. Panel; private Map tallies = new Hash. Map(); // JMS variables private Queue. Connection queue. Connection ; Fig. 16. 6 Vote. Collector class retrieves and tallies votes. // Vote. Collector constructor public Vote. Collector() { super( "Vote Tallies" ); Container container = get. Content. Pane(); // display. Panel will display tally results display. Panel = new JPanel(); display. Panel. set. Layout( new Grid. Layout( 0, 1 ) ); container. add( new JScroll. Pane( display. Panel ) ); 2002 Prentice Hall. All rights reserved.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 // invoke method quit when window closed add. Window. Listener( Outline new Window. Adapter() { public void window. Closing( Window. Event event ) { quit(); } } ); // connect to "Votes" queue try { // create JNDI context Context jndi. Context = new Initial. Context(); // retrieve queue connection factory // and queue from JNDI context Queue. Connection. Factory queue. Connection. Factory = ( Queue. Connection. Factory ) jndi. Context. lookup( "VOTE_FACTORY" ); Queue queue = ( Queue ) jndi. Context. lookup( "Votes" ); // create connection, session and receiver queue. Connection = queue. Connection. Factory. create. Queue. Connection(); Queue. Session queue. Session = queue. Connection. create. Queue. Session( false, Session. AUTO_ACKNOWLEDGE ); Queue. Receiver queue. Receiver = queue. Session. create. Receiver( queue ); Fig. 16. 6 Vote. Collector class retrieves and tallies votes. create JNDI context Line 51 Lines 55 -57 get queue connection factory Lines 61 -65 Lines 66 -67 create Queue. Session create votes receiver 2002 Prentice Hall. All rights reserved.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 Outline // initialize and set message listener queue. Receiver. set. Message. Listener( new Vote. Listener( this ) ); // start connection queue. Connection. start(); } activate connection register listener // process Naming exception from JNDI context catch ( Naming. Exception naming. Exception ) { naming. Exception. print. Stack. Trace(); System. exit( 1 ); } // process JMS exception from queue connection or session catch ( JMSException jms. Exception ) { jms. Exception. print. Stack. Trace(); System. exit( 1 ); } } // end Vote. Collector constructor // add vote to corresponding tally public void add. Vote( String vote ) { if ( tallies. contains. Key( vote ) ) { Fig. 16. 6 Vote. Collector class retrieves and tallies votes. Lines 70 -71 Lines 74 updates tallies and. Lines display. 92 -109 Callback method for Vote. Listener instance // if vote already has corresponding tally Tally. Panel tally. Panel = ( Tally. Panel ) tallies. get( vote ); tally. Panel. update. Tally(); } 2002 Prentice Hall. All rights reserved.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 // add to GUI and tallies else { Tally. Panel tally. Panel = new Tally. Panel( vote, 1 ); display. Panel. add( tally. Panel ); tallies. put( vote, tally. Panel ); validate(); } Outline } // quit application public void quit() { if ( queue. Connection != null ) { // close the queue connection if it exists try { queue. Connection. close(); } // process JMS exception catch ( JMSException jms. Exception ) { jms. Exception. print. Stack. Trace(); System. exit( 1 ); } Fig. 16. 6 Vote. Collector close queue classconnection retrieves and tallies votes. Line 118 } System. exit( 0 ); } // end method quit 2002 Prentice Hall. All rights reserved.
133 134 135 136 137 138 139 140 // launch Vote. Collector public static void main( String args[] ) { Vote. Collector vote. Collector = new Vote. Collector(); vote. Collector. set. Size( 200, 200 ); vote. Collector. set. Visible( true ); } Outline } Fig. 16. 6 Vote. Collector class retrieves and tallies votes. 2002 Prentice Hall. All rights reserved.
16. 3. 3 Voter Application: Receiver Side (cont. ) Fig. 16. 7 Vote. Collector tallies and displays votes. 2002 Prentice Hall. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Outline // Vote. Listener. java // Vote. Listener is the message listener for the // receiver of the "Votes" queue. It implements // the specified on. Message method to update the // GUI with the received vote. package com. deitel. advjhtp 1. jms. voter; // Java extension packages import javax. jms. *; public class Vote. Listener implements Message. Listener { private Vote. Collector vote. Collector ; // Vote. Listener constructor public Vote. Listener( Vote. Collector collector ) { vote. Collector = collector; } Fig. 16. 8 Vote. Listener implements class receives Message. Listener messages from the interface queue. Line 11 // receive new message public void on. Message( Message message ) { Text. Message vote. Message ; Line 29 Line 32 // retrieve and process message try { if ( message instanceof Text. Message ) { vote. Message = ( Text. Message ) message; String vote = vote. Message. get. Text(); vote. Collector. add. Vote( vote ); System. out. println( "Received vote: " + vote ); } ensure message of type Text. Message call back 2002 Prentice Hall. All rights reserved.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 else { System. out. println( "Expecting " + "Text. Message object, received " + message. get. Class(). get. Name() ); } Outline } // process JMS exception from message catch ( JMSException jms. Exception ) { jms. Exception. print. Stack. Trace(); } } // end method on. Message } Fig. 16. 8 Vote. Listener class receives messages from the queue. 2002 Prentice Hall. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Tally. Panel. java // Tall. Panel is the GUI component which displays // the name and tally for a vote candidate. package com. deitel. advjhtp 1. jms. voter; Outline // Java core packages import java. awt. *; // Java extension packages import javax. swing. *; public class Tally. Panel extends JPanel { private JLabel name. Label; JText. Field tally. Field; String name; int tally; // Tally. Panel constructor public Tally. Panel( String vote. Name, int vote. Tally ) { name = vote. Name; tally = vote. Tally; Fig. 16. 9 Tally. Panel class displays candidate name and tally. name. Label = new JLabel( name ); tally. Field = new JText. Field( Integer. to. String( tally ), 10 ); tally. Field. set. Editable( false ); tally. Field. set. Background( Color. white ); add( name. Label ); add( tally. Field ); } // end Tally. Panel constructor 2002 Prentice Hall. All rights reserved.
36 37 38 39 40 41 42 // update tally by one vote public void update. Tally() { tally++; tally. Field. set. Text( Integer. to. String( tally ) ); } Outline } increments tally by one Fig. 16. 9 Tally. Panel class displays candidate name and tally. 2002 Prentice Hall. All rights reserved.
16. 3. 4 Voter Application: Configuring and Running 1. Start J 2 EE server j 2 ee –verbose 2. Create Votes queue (in new window) j 2 eeadmin –add. Jms. Destination Votes queue 3. Verify queue was created j 2 eeadmin –list. Jms. Destination 4. Create connection factory j 2 eeadmin –add. Jms. Factory VOTE_FACTORY queue 5. Start Vote. Collector java –classpath %J 2 EE_HOME%libj 2 ee. jar; . -Djms. properties=%J 2 EE_HOME%configjms_client. properties com. deitel. advjhtp 1. jms. voter. Vote. Collector 6. Start Voter (in new window) java –classpath %J 2 EE_HOME%libj 2 ee. jar; . -Djms. properties=%J 2 EE_HOME%configjms_client. properties com. deitel. advjhtp 1. jms. voter. Voter 2002 Prentice Hall. All rights reserved.
16. 3. 4 Voter Application: Configuring and Running (cont. ) • Once application finished – remove connection factory j 2 eeadmin –remove. Jms. Factory VOTE_FACTORY – remote topic j 2 eeadmin –remove. Jms. Destination Votes – stop J 2 EE server j 2 ee -stop 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 2002 Prentice Hall. All rights reserved.
16. 4 Publish/Subscribe Messaging (cont. ) Fig. 16. 10 Publish/subscribe messaging model. 2002 Prentice Hall. All rights reserved.
16. 4. 1 Weather Application: Overview • Class Weather. Publisher – retrieves weather updates from URL – publishes information as messages to topic • Class Weather. Subscriber – provides GUI • enables user to select desired cities – subscribes to Weather topic • receives corresponding messages • uses message selector 2002 Prentice Hall. All rights reserved.
16. 4. 1 Weather Application: Overview (cont. ) Fig. 16. 11 Weather application overview. 2002 Prentice Hall. All rights reserved.
16. 4. 2 Weather Application: Publisher Side • Class Weather. Publisher – retrieves weather updates from National Weather Service – publishes weather updates to Weather topic – messages of type Object. Message • String property City specifies corresponding city 2002 Prentice Hall. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Weather. Publisher. java // Weather. Publisher retrieves weather conditions from the National // Weather Service and publishes them to the Weather topic // as Object. Messages containing Weather. Beans. The city name is // used in a String property "City" in the message header. package com. deitel. advjhtp 1. jms. weather; // Java core packages import java. io. *; import java. net. *; import java. util. *; // Java extension packages import javax. jms. *; import javax. naming. *; // Deitel packages import com. deitel. advjhtp 1. rmi. weather. Weather. Bean; public class Weather. Publisher extends Timer. Task { Outline Fig. 16. 12 Weather. Publisher class publishes messages to Weather topic. private Buffered. Reader in; private Topic. Connection topic. Connection ; // Weather. Publisher constructor public Weather. Publisher() { // update weather conditions every minute Timer timer = new Timer(); timer. schedule. At. Fixed. Rate( this, 0, 60000 ); // allow user to quit Input. Stream. Reader input. Stream. Reader = new Input. Stream. Reader( System. in ); char answer = '