Introduction to JMS
JMS (Java Message Service ). It is used to transmit messages between applications.
Enterprise Message
WebSphere MQ, SonicMQ, Microsoft Message Queue (MSMQ), ActiveMQ, Message Bean in EJB, and ESB (Enterprise Message Bus) in SOA are all Enterprise messages.
Enterprise Message can be used in the following ways:
Centralized
Distributed
Hybrid
Install a Daemon program on each Client. The Client and Daemon use TCP communication and use MultiCast between Daemon and other Daemon.
JMS message model
JMS supports two message models: point-to-point, publish-and-subscribe.
Point-to-point
In the P2P model, a JMS Client can send and receive messages to (from) a queue. It can be synchronous or asynchronous. Messages are obtained in PULL (PULL) mode. The sender is called the sender, and the receiver is called the receiver.
Publish-and-subscribe
Messages are sent in push mode. The subscriber sends messages even if it is offline. The sender is publisher and the receiver is subscriber.
Jms api Overview
JMS only defines interfaces without specific implementation. The implementation is completed by the relevant vendors. This is similar to JDBC.
JMS APIs can be divided into three main parts: 1) General API, 2) p2p API, 3) pub/sub API.
The following are the main interfaces:
· ConnectionFactory
· Destination
· Connection
· Session
· Message
· MessageProducer
· MessageConsumer
In the above interfaces, ConnectionFactory and Destination must be obtained through JNDI. In fact, all interfaces are created based on these two interfaces. After ConnectionFactory is obtained, you can obtain the Connection. With Connection, you can create a Session. With Session, you can create Message, MessageProducer, and MessageConsumer.
The instance creation relationship is as follows:
In JMS, Session objects hold transactional instead of Connection, which is different from JDBC. That is to say, when using JMS, an application creates only one Connection object, and multiple Session objects can be created based on this Connection object.
Point-to-point
This is an interface designed for P2P. One-to-one correspondence with common interfaces.
Publish/Subscribe
Similarly, an interface is specially designed for pub/sub:
JMS Pub/Sub example
To run JMS, a jms proxy server is required. In the following example, activemq is used as the proxy server.
After activemq is installed, start it.
The example program is a simple Chat Room.
package com.fjn.java.jms.topic;import java.io.IOException;import java.util.Properties;import java.util.Scanner;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.Session;import javax.jms.TextMessage;import javax.jms.Topic;import javax.jms.TopicConnection;import javax.jms.TopicConnectionFactory;import javax.jms.TopicPublisher;import javax.jms.TopicSession;import javax.jms.TopicSubscriber;import javax.naming.InitialContext;import javax.naming.NamingException;public class Chat implements MessageListener { private TopicSession pubSession; private TopicPublisher publisher; private TopicConnection connection; private String username; public Chat(String factoryJdni, String topicName, String username) throws NamingException, JMSException, IOException { Properties props=new Properties(); props.load(Chat.class.getResourceAsStream("jndi.properties")); InitialContext ctx = new InitialContext(props); TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx .lookup(factoryJdni); Topic topic = (Topic) ctx.lookup(topicName); TopicConnection conn = connFactory.createTopicConnection(); TopicSession pubSession = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicSession subSession = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher publisher = pubSession.createPublisher(topic); TopicSubscriber subscriber = subSession.createSubscriber(topic, null, true); subscriber.setMessageListener(this); this.connection = conn; this.publisher = publisher; this.pubSession = pubSession; this.username = username; conn.start(); } @Override public void onMessage(Message msg) { try { TextMessage text = (TextMessage) msg; System.out.println(text.getText()); } catch (JMSException e) { e.printStackTrace(); } } protected void writeMessage(String text) throws JMSException{ TextMessage msg=pubSession.createTextMessage(); msg.setText(username+": "+text); publisher.publish(msg); } public void close() throws JMSException{ connection.close(); } public static void main(String[] args) throws NamingException, JMSException, IOException { Chat chat=new Chat("TopicCF","topic1","ZhangSan"); Scanner scanner=new Scanner(System.in); while(true){ String line=scanner.nextLine(); if("exit".equals(line)){ chat.close(); System.exit(0); scanner.close(); }else{ chat.writeMessage(line); } } }}package com.fjn.java.jms.topic;import java.io.IOException;import java.util.Scanner;import javax.jms.JMSException;import javax.naming.NamingException;public class Chat2 { public static void main(String[] args) throws NamingException, JMSException, IOException { Chat chat=new Chat("TopicCF","topic1","LiSi"); Scanner scanner=new Scanner(System.in); while(true){ String line=scanner.nextLine(); if("exit".equals(line)){ chat.close(); System.exit(0); scanner.close(); }else{ chat.writeMessage(line); } } }}
In fact, you Can slightly change the main method of the program to get the three parameters for creating Chat from the nameline. In this way, you can start any client.
Content of the Jndi. properties file:
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
In this way, you can have multiple client dialogs, that is, group dialogs.