23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

}<br />

// args[0]=topicName; args[1]=username; args[2]=password<br />

Chat chat = new Chat(args[0],args[1],args[2]);<br />

// Read from command line<br />

BufferedReader commandLine = new<br />

java.io.BufferedReader(new InputStreamReader(System.in));<br />

// Loop until the word "exit" is typed<br />

while(true){<br />

String s = commandLine.readLine( );<br />

if (s.equalsIgnoreCase("exit")){<br />

chat.close( ); // close down connection<br />

System.exit(0);// exit program<br />

} else<br />

chat.write<strong>Message</strong>(s);<br />

}<br />

} catch (Exception e){ e.printStackTrace( ); }<br />

2.1.1 Getting Started with the Chat Example<br />

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

To put this client to use, compile it like any other <strong>Java</strong> program. Then start your JMS<br />

server, setting up whatever topics, usernames, and passwords you want. Configuration of a<br />

JMS server is vendor-dependent, and won't be discussed here.<br />

The Chat class includes a main( ) method so that it can be run as a standalone <strong>Java</strong><br />

application. It's executed from the command line as follows:<br />

java chap2.chat.Chat topic username password<br />

The topic is the destination that we want to publish-and-subscribe to; username and<br />

password make up the authentication information for the client. Run at least two chat<br />

clients in separate command windows and try typing into one; you should see the text you<br />

type displayed by the other client.<br />

Figure 2.1. The Chat application<br />

21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!