|
Articles Index
Part 1: Shall we chat?
By Alan Beecraft
November, 2001
Project JXTA
is an open source effort for the research and development of a
network programming and computing platform for peer-to-peer
computing (P2P).
One of the fundamental requirements for any peer-to-peer system is a
communication mechanism.
A Message-Oriented Middleware (MOM) system can be used to provide
peer-to-peer communication between applications: a messaging client
can send messages to, and receive messages from, any other client
using the communication facilities provided by the messaging system.
The Java Message Service (JMS) API allows
Java applications to create, send, receive and read messages using a messaging
product. The JMS API is now an integral part of the Java 2 SDK, Enterprise
Edition (J2EETM SDK) Version 1.3.
The inclusion of an implementation of the JMS API allows for loosely coupled
interaction between J2EE applications and any other system capable of
messaging.
This article compares and contrasts the P2P features provided by the initial
implementation of JXTA technology with the distributed computing features
provided by the J2EE 1.3 platform, in particular the implementation of the JMS
API.
Despite the choice of name, Project JXTA is not confined to the Java programming
language. However, the initial implementation uses Java technology, which makes
a comparison with other, existing Java technologies appropriate.
This first article begins with a brief introduction to the concept of
peer-to-peer computing and introduces example code for you to try.
Peer-to-Peer (P2P)
The problem with a generic term like Peer-to-Peer (P2P) network computing is
that it inevitably means different things to different people, and so you
will find the term applied to a variety of systems. For example, you will find
the term applied to:
-
Centralized systems where every peer connects to a server which coordinates
and manages communication.
-
Brokered systems where peers connect to a server in order to discover other
peers, but then manage the communication themselves.
-
Decentralized systems, where peers run independently without the need for
centralized services.
Project JXTA defines a set of protocols that can be used to
construct peer-to-peer systems using any of the centralized, brokered and
decentralized approaches. J2EE is intended for server-side computing, which
suggests it is most suited for a centralized peer-to-peer system.
A good way to get a better appreciation of the differences is to study a
simple example application. This first article will introduce a very simple
JMS chat application for the exchange of messages between users. The choice of a
chat application is deliberate because Project JXTA already supplies a
demonstration application called myJXTA (which is also known by its
former name of InstantP2P) that includes a sophisticated chat capability.
This can be obtained from the Project JXTA
download area where you can download the binaries, documentation and source
code for the myJXTA (InstantP2P) demo.
A later article will examine the myJXTA demo in more detail,
highlighting the specific peer-to-peer facilities required for a generic
solution. This article examines how an existing Java API can be used as
the basis of peer-to-peer communication and introduces the JMS example code.
Peer-to-peer example code using JMS
The JMS chat application was deployed and tested using the
Java 2 SDK, Enterprise Edition Version 1.3 so this is the
environment that will be assumed. To run the JMSChat example you
must have installed the latest version of the Java 2 SDK, Enterprise
Edition Version 1.3. You should first install the required version of the Java 2 SDK, Standard
Edition, if not already installed.
This section contains instructions on how to create and run a simple JMS
chat program. It is an adaptation of the publish/subscribe (pub/sub)
example provided by the
JMS
tutorial.
Before you can run the example, you need to make sure your environment is
set appropriately. Table 1 shows how
to set the environment variables needed to run J2EE applications on Windows
and UNIX platforms.
Table 1: Environment Settings
for Compiling and Running J2EE Applications
| Platform |
Variable Name |
Values |
| Windows |
%JAVA_HOME% |
Directory where the Java 2 SDK, Standard Edition, version 1.3.1 is
installed |
%J2EE_HOME% |
Directory where the J2EE 1.3 SDK is installed, usually
C:\j2sdkee1.3 |
%CLASSPATH |
Include the following:
.;%J2EE_HOME%\lib\j2ee.jar;%J2EE_HOME%\lib\locale |
%PATH% |
Include %J2EE_HOME%\bin |
| UNIX |
$JAVA_HOME |
Directory where the Java 2 SDK, Standard Edition, version 1.3.1 is
installed |
$J2EE_HOME |
Directory where the J2EE 1.3 SDK is installed, usually
$HOME/j2sdkee1.3 |
$CLASSPATH |
Include the following:
.:$J2EE_HOME/lib/j2ee.jar:$J2EE_HOME/lib/locale |
$PATH |
Include $J2EE_HOME/bin |
You can download more examples of JMS client programs from the JMS
Documentation web site.
This section describes the publishing and subscribing parts of the example.
It then explains how to compile and run the example using the J2EE 1.3 SDK.
The following sections describe these steps:
- Writing the chat client
- Compiling the chat client
- Starting the JMS provider
- Creating the JMS administered objects
- Running the chat client
- Deleting the topic and stopping the server
Writing the Chat Client Program
The JMSChat client is a multi-threaded Java
program that uses a pub/sub topic for broadcasting messages to all
subscribers to the topic.
It uses separate threads for publishing messages to the topic and for
receiving messages from the topic. These threads are implemented by
the JMSChatThread class.
The publishing thread does the following:
- Performs a JNDI lookup of the
TopicConnectionFactory
and topic
- Creates a connection and session
- Creates a
TopicPublisher
- Creates a
TextMessage
- Publishes messages to the topic
- Closes the connection, which automatically closes the session and
TopicPublisher
The receiving thread does the following:
- Performs a JNDI lookup of the
TopicConnectionFactory and topic
- Creates a connection and session
- Creates a
TopicSubscriber
- Starts the connection, causing message delivery to begin
- Listens for the messages published to the topic
- Closes the connection, which automatically closes the session
and
TopicSubscriber
The complete application consists of the following code:
JMSChat.java
The chat client program is JMSChat.java.
It creates the user interface and several threads; one to send messages, one
to receive messages, and one to update the user interface.
JMSChatThread.java
The message sender or receiver thread is
JMSChatThread.java.
When running as a sender (publisher), it connects to a JMS topic and sends
messages. When running as a receiver (subscriber), it connects to the same JMS
topic and receives messages.
ChatData.java
The threads exchange information as control data implemented by
ChatData.java.
It provides the control data and access methods to allow the different
threads to exchange information. For example, the user interface passes
outbound messages to the sender thread to be forwarded to the JMS topic.
Conversely, the receiver thread passes inbound message to the user interface
to be displayed.
CardPanel.java
The user interface uses CardPanel.java.
The login panel and chat panel are implemented as CardPanel objects
to make it easy for the user interface to switch between the login panel and
chat panel.
Compiling the example code
To compile the example, do the following:
- Make sure you have set the environment variables shown in Table 1.
- Go to
download_dir, where download_dir is the directory where you
downloaded the example.
- Compile the required classes:
javac JMSChat.java
javac JMSChatThread.java
javac ChatData.java
javac CardPanel.java
Starting the JMS Provider
If you did not do so before, start the J2EE server in another terminal window:
j2ee -verbose
Wait until the server displays the message J2EE server startup
complete.
Creating the JMS Administered Objects
In the window where you compiled the clients, use the j2eeadmin
command to create a topic named ChatTopic. The last argument tells
the command what kind of destination to create.
j2eeadmin -addJmsDestination ChatTopic topic
To verify that the topic has been created, use the following command:
j2eeadmin -listJmsDestination
You should see ChatTopic appear in the output:
JmsDestination
--------------
< JMS Destination : ChatTopic , javax.jms.Topic >
This example uses the default TopicConnectionFactory object
supplied with the J2EE 1.3 SDK. With a different J2EE product, you might need to
create a connection factory.
Running the JMSChat Client
Run the client as follows:
- Run the
JMSChat program. When the login panel appears
specify the user name and topic name (you do not need to enter a topic name if
you want to use the default topic name of ChatTopic).
You need to define a value for jms.properties.
The Windows command to run the program looks like this (it should be on one
line):
java -Djms.properties=%J2EE_HOME%\config\jms_client.properties
JMSChat
The UNIX command looks like this (again, it should be on one line):
java -Djms.properties=$J2EE_HOME/config/jms_client.properties
JMSChat
- When the login panel appears enter a user name (and a topic name if not
using the default of "ChatTopic"), and hit
Login
to establish the JMS connections to the chosen topic.
-
After a short delay while the JMS connections are established, the program
displays the first chat panel ready for input.
-
In another terminal window, run the
JMSChat
program again and login with a different user name (but using the same
topic name).
-
After a short delay while the JMS connections are established, the program
displays the second chat panel ready for input.
-
In one of the chat panels enter a message in the "Send" text field and
hit
the
Enter key to submit the message. The message
should then appear in the "Receive" text area of both chat panels.
-
Repeat several times, submitting messages from alternate chat panels. You
should see the messages appear in the order in which they were submitted.
-
The output of the program is in the form
<username>: <message>
where <username>
is the login name of the user and <message>
is the message that the user submitted in their "Send" field
of
the panel.
For example:
user 1: message 1 from user 1
user 2: message 1 from user 2
user 2: message 2 from user 2
user 1: message 2 from user 1
...
The output should be the same in both panels. If you run a third
JMSChat program
(with a different user name but the same topic name), the third panel
will not display the messages sent before that user joined the chat
session but it will display messages sent after that user joined. For example:
user 3: message 1 from user 3
user 2: message 3 from user 2
user 1: message 3 from user 1
user 3: message 2 from user 3
...
Use the Exit button to stop each
JMSChat program.
Deleting the Topic and Stopping the Server
- If you wish, you can delete the topic you created:
j2eeadmin -removeJmsDestination ChatTopic
- If you wish, you can stop the J2EE server as well:
j2ee -stop
Conclusion
This introductory article has introduced a simple JMS chat application as
an example of peer-to-peer communication using existing Java technology.
One advantage of JMS is that it is possible to create a simple peer-to-peer
application with just a few JMS API calls, leaving the underlying messaging
infrastructure to handle the communication between the end users.
One disadvantage is that the peers have no real knowledge of other peers.
Instead of peer-to-peer discovery, each peer needs to know the name of the
JMS topic that provides the central point of communication between the peers.
This implies that it is more suited to use within an enterprise as it relies
on mutual knowledge of which JMS topic to use as a chat facility.
The second article in this series will contrast the
JMSChat application
with the Project JXTA myJXTA demo, (which is also known by its
former name of InstantP2P), to highlight differences between a simple
peer-to-peer application developed with existing messaging technology and a
generic approach specifically intended for peer-to-peer network computing.
Developers interested in developing peer-to-peer applications are encouraged
to study the myJXTA demo. which can be
downloaded from the Project JXTA web site.
For more information
JXTA.org
Project JXTA
J2EE
JMS
JMS
Tutorial
About the Author
Alan Beecraft is a Software Engineer in the Sun Developer Network (SDN)
group at Sun Microsystems.
Have a question about Java programming? Use
Java Online
Support.
|