JMS/JNDI Information Transmission Principle

Source: Internet
Author: User
Tags ldap

JNDI -- Java Naming and Directory Interface

The jndi api is used to run the name and directory services. It provides a consistent model to access and operate enterprise-level resources such as DNS and LDAP, local file systems, and the latter's objects on the application server.

Sun only provides the JNDI interface (standard), IBM, Novell, sun, WebLogic, and JBoss have provided service providers for JNDI,

In JNDI, each node in the directory structure is called context. Each JNDI name is relative to context. There is no concept of absolute name here. For an application, it can obtain its first context by using the initialcontext class:

Context CTX = new initialcontext ();

CTX. BIND ("name", object );

CTX. Lookup ("name ");

Context: context. My understanding is equivalent to the directory in the file system (the Naming Service of JNDI can use the File System of the operating system, haha ).

Entry/object: A node, equivalent to a directory or file in the file system.

Filter: the query/filter condition is a string expression such as: (& (objectclass = Top) (CN = *). The query results show that the objectclass attribute is top and the CN attribute is all entries.

Attribute: the attribute of entry/object can be understood as the attribute of a Java object. The difference is that this attribute can be assigned multiple times.

A. Divide interfaces into context and dircontext

JNDI has two core interfaces: context and dircontext. Context contains basic name operations, and dircontext extends these operations to the directory service. Dircontext extends context and provides basic directory service operations, maintenance of name object attributes, and attribute-based name search.

B. Multiple methods of context list

In general, there are two kinds of applications for context list: the application for context browsing and the application for actual operations on objects in the context.

Generally, a context browser only needs to display the name of the content contained in the context, or obtain information such as the object type. This type of application is generally interactive and allows users to select some of the listed context lists for further display.

In addition, some applications need to perform actual operations on objects in the context. For example, a backup program needs to perform operations on the statuses of all files in the directory, or a printer administrator may need to reset all the printers in the building. To perform this operation, the program needs to obtain the actual object in the context.

For these two types of applications, the context interface provides two context list methods: List () and listbindings (). Here, list () Only returns a series of name/class ing, while listbindings () returns the name, class, and object itself. Obviously, list () is used for context browsing and listbindings () is used for applications that require actual operations on objects.

Hashtable Env = new hashtable ();

Env. Put (context. initial_context_factory, "com. Sun. JNDI. fscontext. reffscontextfactory ");

Context CTX = new initialcontext (ENV );

Object OBJ = CTX. Lookup ("C:/aaa.txt ");

Hashtable Env = new hashtable ();

Env. Put (context. initial_context_factory, "com. Sun. JNDI. LDAP. ldapctxfactory ");

Env. Put (context. provider_url, "LDAP: // 127.0.0.1: 10389 /");

Dircontext CTX = new initialdircontext (ENV );

Java MES (JMS) is an application interface (API) used to communicate with message-oriented middleware ). It supports point-to-point domains, publish/subscribe domains, and supports the following types: Approved message transmission and transactional message transmission, supports consistent messages and persistent subscribers.

The JMS standard is the most widely used message interface standard.

JMS is generally divided into two functional blocks: Message production and message consumption. The message-driven bean provides the ability to asynchronously consume messages.

The main function of jmstemplate in spring is to generate messages.

The message service provided by JMS includes two mechanisms:

1. Point-to-Point P2P single message consumer consumption (queue Queue)

Client1 sends a message to a queue on the Message Server, while Client2 extracts the message from the queue.

2. Publish/subscribe publish/subscribe. If it is a persistent subscriber, it will be saved to MQ and monitored. (Topic)

Similar to broadcast, Client1 sends a message to a message topic on the server. Clients interested in this topic can reserve a message topic. When the topic has data, therefore, clients that have made an appointment for this topic will receive this topic.

Sun has two versions of JMS

1.02 is mainly divided into two destination types: topicconnectionfactory queueconnectionfactory

1.1 called connectionfactory

Understand the basic concepts in JMS

Destination destination

For example, a message queue resource must be searched through the JNDI method, and the destination is used to represent the abstract destination resources of these management objects.

Session

A session object is a single-threaded context object used to generate messages and consume messages. It is a resource provided by JVM and a JMS object.

Session is the factory that generates message producers and consumers.

Topicpublisher publisher = session. createpublisher (topic );

Topicsubscriber subscriber = session. createsubscriber (topic );

Queuesender sender = session. createsender (Queue );

Queuereceiver receiver = session. createreceiver (Queue );

Sessions are also the factory for creating topic message objects and point-to-point message objects.

Textmessage MSG = publisher. session. createtextmessage ();

Textmessage MSG = sender. session. createtextmessage ();

Connection

A connection object is the connection between the client and the service provider activated by the client.

Message

It refers to the Information Unit used to transmit information between the service provider and the client.

Each message is composed of three parts: headers, properties, and body.

A. Message Header metadata and message routing information.

Jmsdeliverymode: deliverymode. non_persistent when a server error occurs, the message will be lost. deliverymode. Persistent when a server error occurs, the message will not be lost.

Jmsexpiration: the message expiration time limit controls the time when the message is alive in the server container. If the time is 0, the message will not expire.

Jmspriority indicates the priority of a message. The value 0-4 indicates the normal level, and the value 5-9 indicates that the message needs to be sent quickly.

If the value of jmsredelivered is true, if a message receiver fails to receive the message, the message will be resent to the receiver of the message.

ID of the jmsmessageid message, which is a character value specified by the container.

Jmstimestamp long type value, message timestamp, set by container

B. Message attributes are defined by the sending end of JMS. They are a bunch of key-value pairs. They are read-only to the receiving end!

You can use getpropertynames () to return all attribute names.

Setbooleanproperty ("name", Boolean );

. (Eight basic types)

The following method can be used to filter the receiver to receive only the values of attributes whose name is silent.

Queuereceiver extends ER = session. createreceiver (queue, "name = 'silent '");

C. The message body is responsible for loading the message data content. It is read-only to the receiving end!

Message textmessage objectmessage bytesmessage streammessage mapmessage

They are all created through session. createxxxmessage ();

Note: all are interfaces. In fact, the existing spyxxxmessage... is managed by the session.

Messageproducer message producer

Message generation is completed by the JMS client, and the service provider is only responsible for managing these messages.

Use void publish (message) and void send (message)

Messageconsumer message consumer

A message consumer can receive messages from a queue.

All receive messages from the Message Queue through Message Receiver ().

JMS usage steps: (9 steps)

Use JNDI to find the factory class connectionfactory used to create the connection, use these factory classes to create a connection instance, and create a session for the connected instance, this session class represents a session resource. We can use the session class to generate a producer for creating new messages, or generate a consumer for receiving messages ), regardless of message sending or receiving, you need to access the destination object (destination) of JMS ).

When using the consumption method, you can also set up the message Monitor (listener). When a new message is generated, the listener will be notified,

1) create a JNDI context and find the factory class used to create a JMS connection

Hashtable Env = new hashtable ();

Env. Put (context. initial_context_factory, "org. jnp. Interfaces. namingcontextfactory ");

Env. Put (context. provider_url, "LDAP: // 192.168.1.106: 1039 /");

Context inictx = new initialcontext (ENV );

Topicconnectionfactory Tcf = (topicconnectionfactory) inictx. Lookup ("connectionfactory ");

2) Find the target JMS object

Topic topic = (topic) inictx. Lookup ("Topic/testtopic ");

3) create a JMS connection

Topicconnection conn = TCF. createtopicconnection (); if the user and PASS Parameters are added here, the message is a persistent message.

4) create a session

Topicsession session = conn. createtopicsession (false, session. auto_acknowledge );

5) Create the producer and consumer of the message

Topicpublisher publisher = session. createpublisher (topic );

Topicsubscriber subscriber = session. createsubscriber (topic );

6) register the message listener:

Textlistener listener = new textlistener ();

Subscriber. setmessagelistener (listener );

7) Start the JMS connection

Conn. Start ();

8) send and receive messages

Publisher. Publish (message );

Message message = subscriber. Receive ();

9) stop and close the JMS connection

Conn. Stop ();

Session. Close ();

Conn. Close ();

 

JMS is encapsulated in spring, and it becomes easier to use and write. jmstemplate is available for 1.1 of JMS, while jmstemplate102 is available for 1.02.

JMS in spring is not concerned about the queue and topic methods. It is integrated and used together !!

In jmstemp, You need to specify connectionfactory (Org. springframework. JNDI. jndiobjectfactorybean) and defaultdestination (Org. springframework. JNDI. jndiobjectfactorybean). Note that the pubsubdomain attribute must be set in section 1.02, and whether to send it

JMS sending Method

Jmstemplate. Send (New messagecreator (){

Public message createmessage (session) throws jmsexception {

// Use session to create a message and return the message instance.

}

}

JMS Receiving Method

Message MSG = jmstemplate. Receive ();

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.