Analyze the source code of the chat example

Source: Internet
Author: User

Here we will explain in detail the example in the previous section: chat. First, you can understand the configuration information of the topicconnectionfactory and topic of the chat application. In the example, they are named topiccf and topic1 respectively. For example, with activemq, you can create a JNDI. properties file in the class path and set the topicconnectionfactory name and a topic for the chat application:

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactoryjava.naming.provider.url = tcp://localhost:61616java.naming.security.principal = systemjava.naming.security.credentials = managerconnectionFactoryNames = TopicCFtopic.topic1 = jms.topic1

The JNDI. properties file also contains the JNDI connection information of the JMS provider. You need to set the initialization context factory class, provider URL, user name and password required to connect to the JMS server. Each vendor will have a different context factory class and URL name for connecting to the server. To obtain these values, you need to check the relevant documents of the specific JMS provider or javaee container.

After configuring and starting the JMS server, you need to compile the chat application. If we use activemq, The activemq-all-xxx.jar file must be added to our class path.

The chat application is a chat room program. The chat client creates a JMS publisher and subscriber for a specific topic. This topic represents a chat room. The JMS server registers all JMS clients that want to publish or subscribe to a specific topic. When you input text in the command line of a chat client, it will publish a message to the transfer server. The message transmission server identifies topics related to the publisher. The message is sent to all JMS clients that have subscribed to the topic.

Obtain a JNDI connection

JNDI is an API unrelated to the specific implementation, used for the Directory and naming service system. The JMS client can use a directory service to access connectionfactory and destination (topic and queue) objects. Connectionfactory and destination objects are unique objects that cannot be obtained using JMS APIs. JNDI provides a convenient, location-transparent, configurable, and portable mechanism for obtaining connectionfactory and destination objects. These objects are also called JMS managed objects, because they are created and configured by a system administrator. With JNDI, the JMS client can first find a connectionfactory to access a JMS provider. Connectionfactory is used to create a JMS connection, which can then be used to send and receive messages. You can also obtain the destination object through JNDI, that is, the virtual channel (topic and queue) in JMS, which is used by the JMS client.

To create a connection to the JNDI Naming Service, you must first create a javax. Naming. initialcontext object. An initialcontext is the starting point for all JNDI queries. It is similar to the file system root directory. Initialcontext provides a network connection to the directory service, which serves to access the root directory of the JMS managed object.

Topicconnectionfactory

Once a JNDI initialcontext object is instantiated, you can use it to search for topicconnectionfactory In the Naming Service of the message transmission server:

InitialContext ctx = new InitlalContext();TopicConnectionFactory conFactory = (TopicConnectionFactory) ctx.lookup(topicFactory);

Topicconnectionfactory provides two overloaded versions of the createtopicconnection () method:

public TopicConnection createTopicConnection()public TopicConnection createTopicConnection(String username,String password)        throws JMSException,JMSSecurityException


Analyze the source code of the chat example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.