@[TOC](Java Message Service (JMS))
JMS API Programming Model
Point to point messaging system
- Each message is addressed to a specific queue.
- Queues retain all messages sent to them until the messages are consumed or expire.
- Each message has only one consumer.
- Connection -> createQueueConnection()
Publish/subscribe messaging system
- Publishers and subscribers can dynamically publish or subscribe to a topic (queue).
- Topics retain messages only as long as it takes to distribute them to subscribers.
- Each message can have multiple consumers.
- A client who subscribes to a topic can consume only messages sent after the client has created a subscription.
- Connection -> createTopicConnection()
Synchronous fashion
- A consumer explicitly fetches the message from the destination by calling the receive() method.
- The receive() method can block until a message arrives or can time out if a message does not arrive within a specified time limit.
Asynchronous fashion
- A client can register a message listener with a consumer.
- Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener’s onMessage() method.
Low level program
- 先下载 Apache ActiveMQ
- 导jar包
- 写 Producer
package point2point;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.Date;
public class synProducer {
public static void main(String[] args) throws JMSException {
// ConnectionFactory
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// ConnectionFactory -> Connection
Connection connection = factory.createQueueConnection();
// Connection -> Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Session -> Destination
Destination destination = session.createQueue("synQueue"); // point 2 point
// Destination destination = session.createTopic("Publisher & Subscriber"); // Publisher / Subscriber
// Session -> MessageProducer
MessageProducer Producer = session.createProducer(destination);
// Session -> Message
Message message = session.createTextMessage("Text message: "+new Date(System.currentTimeMillis()));
// public class Person implements Serializable (!A Serializable object)
// ObjectMessage message = session.createObjectMessage(person);
Producer.send(message);
System.out.println( ((TextMessage) message).getText() );
Producer.close();
session.close();
connection.close();
System.out.println("Message send~");
}
}
- 写 Consumer
package point2point;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class synConsumer {
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createQueueConnection();
// Need start() in order to receive message, don't need when send.
connection.start();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("synQueue");// point 2 point
// Destination destination = session.createTopic("Publisher & Subscriber"); // Publisher / Subscriber
MessageConsumer consumer = session.createConsumer(destination);
//Synchronous
Message message = consumer.receive();
String text = ((TextMessage) message).getText();
System.out.println("Receive message from ActiveMQ");
System.out.println(text);
// Asynchronous
// consumer.setMessageListener(new MessageListener() {
// @Override
// public void onMessage(Message message) {
// String text = ((TextMessage) message).getText();
// System.out.println("Receive message from ActiveMQ");
// System.out.println(text);
// }
// });
// keep it running
// System.out.println("Listener is working... Press any key + Enter to exit.");
// Scanner scanner = new Scanner(System.in);
// scanner.nextLine();
// consumer.setMessageListener(null);
consumer.close();
session.close();
connection.close();
System.out.println("Consumer complete~");
}
}
- Run activeMQ
- Windows
- [activemq_install_dir]\bin\activemq start
- [activemq_install_dir]\bin\activemq stop
- OS
- cd [activemq_install_dir]/bin
- ./activemq start (may take some time)
- ./activemq stop
- Access ActiveMQ console at:
- http://127.0.0.1:8161/admin/ (username and password default:
admin and admin)
- http://127.0.0.1:8161/admin/ (username and password default:
- Windows
- Run Producer and Consumer