Developing a Simple Example The Chat Application - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Developing a Simple Example The Chat Application

Description:

Using the JMS pub/sub API to build a simple chat application. ... a chat session uses this Chat program to join a specific chat room (topic), and ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 19
Provided by: Peng92
Category:

less

Transcript and Presenter's Notes

Title: Developing a Simple Example The Chat Application


1
Developing a Simple Example The Chat Application
  • Peng-Sheng,Chen

2
Overview
  • Using the JMS pub/sub API to build a simple chat
    application.
  • The requirements of Internet chat map neatly onto
    the publish-and-subscribe messaging model.
  • In this model, a producer can send a message to
    many consumers by delivering the message to a
    single topic.

3
Cont..
  • A message producer is also called a publisher and
    a message consumer is also called a subscriber.
  • Every participant in a chat session uses this
    Chat program to join a specific chat room
    (topic), and deliver and receive messages to and
    from that room

4
Chat Application
5
JNDI JMS
  • In JMS, JNDI is used mostly as a naming service
    to locate administered objects.
  • Administered objects are JMS objects that are
    created and configured by the system
  • administrator.
  • Administered objects include JMS
    ConnectionFactory and Destination objects like
    topics and queues.

6
Cont.
  • Using JNDI, JMS clients can browse a naming
    service and obtain references to administered
    objects without knowing the details of the naming
    service or how it is implemented.
  • JMS servers are usually be used in combination
    with a standard JNDI driver and directory service
    like LDAP, or provide a proprietary JNDI service
    provider and directory service.

7
Obtaining a JNDI connection
  • The chat client starts by obtaining a JNDI
    connection to the JMS messaging server.
  • Using JNDI, a JMS client can obtain access to a
    JMS provider by first looking up a
    ConnectionFactory.
  • JMS servers will either work with a separate
    directory service (e.g., LDAP) or provide
  • their own directory service that supports the
    JNDI API.

8
Cont.
  • The constructor of the Chat class starts by
    obtaining a connection to the JNDI naming
  • service used by the JMS server
  • Creating a connection to a JNDI naming service
    requires that a javax.naming.InitialContext
    object be created.

9
Sessions and Threading
  • The Chat application uses a separate session for
    the publisher and subscriber, pubSession and
    subSession, respectively.
  • According to the JMS specification, a session may
    not be operated on by more than one thread at a
    time.
  • If both the publisher and subscriber had been
    created by the same session, the two threads
    could operate on these methods at the same time

10
Constructor. Establish JMS publisher and
subscriber
  • public class Chat implements javax.jms.MessageList
    ener
  • private TopicSession pubSession
  • private TopicSession subSession
  • private TopicPublisher publisher
  • private TopicConnection connection
  • private String username
  • public Chat(String topicName, String
    username, String password) throws Exception
  • .

11
Initialize the instance variables
  • public void set(TopicConnection con, TopicSession
    pubSess,
  • TopicSession subSess, TopicPublisher
    pub,String username)
  • ..

12
Receive message from topic subscriber
  • public void onMessage(Message message)
  • try
  • TextMessage textMessage (TextMessage) message
  • String text textMessage.getText( )
  • System.out.println(text)
  • catch (JMSException jmse) jmse.printStackTrace
    ( )

13
Create and send message using topic publisher
  • protected void writeMessage(String text) throws
    JMSException
  • TextMessage message pubSession.createTextMessag
    e( )
  • message.setText(username" "text)
  • publisher.publish(message)

14
Close the JMS connection
  • public void close( ) throws JMSException
  • connection.close( )

15
Run the Chat client
  • public static void main(String args)
  • try
  • .
  • Chat chat new Chat(args0,args1,args2)
  • // args0topicName args1username
    args2password
  • BufferedReader commandLine new
  • java.io.BufferedReader(new InputStreamReader(Syst
    em.in)) // Read from command line

16
Getting Started with the Chat Example
  • Compile it like any other Java program.
  • Start your JMS server, setting up whatever
    topics, usernames, and passwords you want.
  • It can be run as a standalone Java
  • application. It's executed from the command line
    as follows
  • java chap2.chat.Chat topic username password

17
Cont.
  • Run at least two chat clients in separate command
    windows and try typing into one you should see
    the text you type displayed by the other client.

18
Cont.
Write a Comment
User Comments (0)
About PowerShow.com