.JMS介绍
JMS源于企业应用对于消息中间件的需求,使应用程序可以通过消息进行异步处理而互不影响。Sun公司和它的合作伙伴设计的JMS API定义了一组公共的应用程序接口和相应语法,使得Java程序能够和其他消息组件进行通信。JMS有四个组成部分:JMS服务提供者、消息管理对象、消息的生产者消费者和消息本身。
1)JMS服务提供者实现消息队列和通知,同时实现消息管理的API。JMS已经是J2EE API的一部分,J2EE服务器都提供JMS服务。
2) 消息管理对象提供对消息进行操作的API。JMS API中有两个消息管理对象:创建jms连接使用的工厂(ConnectionFactory)和目的地(Destination),根据消息的消费方式的不同ConnectionFactory可以分为QueueConnectionFactory和TopicConnectionFactory,目的地(Destination)可以分为队列(Queue)和主题(Topic)两种。
3)消息的生产者和消费者。消息的产生由JMS的客户端完成,JMS服务提供者负责管理这些消息,消息的消费者可以接收消息。消息的生产者可以分为――点对点消息发布者(P2P)和主题消息发布者(TopicPublisher)。所以,消息的消费者分为两类:主题消息的订阅者(TopicSubscriber)和点对点消息的接收者(queue receiver)
4)消息。消息是服务提供者和客户端之间传递信息所使用的信息单元。JMS消息由以下三部分组成:
消息头(header)――JMS消息头包含了许多字段,它们是消息发送后由JMS提供者或消息发送者产生,用来表示消息、设置优先权和失效时间等等,并且为消息确定路由。
属性(property)――用来添加删除消息头以外的附加信息。
消息体(body)――JMS中定义了5种消息体:ByteMessage、MapMessage、ObjectMessage、StreamMessage和TextMessage。
2.Messages 通信方式
上面提到JMS通信方式分为点对点通信和发布/订阅方式
1)点对点方式(point-to-point)
点对点的消息发送方式主要建立在 Message Queue,Sender,reciever上,Message Queue 存贮消息,Sneder 发送消息,receive接收消息.具体点就是Sender Client发送Message Queue ,而 receiver Cliernt从Queue中接收消息和"发送消息已接受"到Quere,确认消息接收。消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到Queue,而不需要知道接收客户端是不是在运行
2)发布/订阅 方式(publish/subscriber Messaging)
发布/订阅方式用于多接收客户端的方式.作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。作为subscriber ,在接收消息时有两种方法,destination的receive方法,和实现message listener 接口的onMessage 方法。
3.为什么选用ActiveMQ
1)ActiveMQ是一个开放源码
2)基于Apache 2.0 licenced 发布并实现了JMS 1.1。
3)ActiveMQ现在已经和作为很多项目的异步消息通信核心了
4)在很多中小型项目中采用ActiveMQ+SPRING+TOMCAT开发模式。
4.编程模式
4.1消息产生者向JMS发送消息的步骤
(1)创建连接使用的工厂类JMS ConnectionFactory
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息生产者MessageSender
(5)使用消息生产者MessageSender发送消息
4.2消息消费者从JMS接受消息的步骤
(1)创建连接使用的工厂类JMS ConnectionFactory
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息消费者MessageReceiver
(5)使用消息消费者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver
消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。
5.ActiveMQ运行
ActiveMQ5.0版本默认启动时,启动了内置的jetty服务器,提供一个demo应用和用于监控ActiveMQ的admin应用。运行%activemq_home%bin/目录下的 activemq.bat , 之后你会看见如下一段话表示启动成功。
打开http://localhost:8161/admin/queues.jsp ,可以查看相应的queue中是否有消息
服务端TopicPublisher.java
6 可用的例子
客户端:TopicListener.java
- public class TopicListener implements MessageListener {
- private Connection connection;
- private MessageProducer producer;
- private Session session;
- private int count;
- private long start;
- private Topic topic;
- private Topic control;
- private String url = "tcp://localhost:61616";
- private static MyConsumerFrame frame= new MyConsumerFrame();;
- private static Long countMsg = 1L;
- public static void main(String[] argv) throws Exception {
- TopicListener l = new TopicListener();
- String[] unknown = CommandLineSupport.setOptions(l, argv);
- if (unknown.length > 0) {
- System.out.println("Unknown options: " + Arrays.toString(unknown));
- System.exit(-1);
- }
- l.run();
- }
- public void run() throws JMSException {
- ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
- connection = factory.createConnection();
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- topic = session.createTopic("topictest.messages");
- control = session.createTopic("topictest.control");
- MessageConsumer consumer = session.createConsumer(topic);
- consumer.setMessageListener(this);
- connection.start();
- producer = session.createProducer(control);
- System.out.println("Waiting for messages...");
- }
- private static boolean checkText(Message m, String s) {
- try {
- return m instanceof TextMessage && ((TextMessage)m).getText().equals(s);
- } catch (JMSException e) {
- e.printStackTrace(System.out);
- return false;
- }
- }
- public void onMessage(Message message) {
- if (message instanceof ExitRecord) {
- System.out.println("___________________Object Message!");
- ExitRecord record = (ExitRecord)message;
- Long msgCpuId = record.getCpucardId();
- frame.getTableModle().addRow(new String[]{String.valueOf(countMsg++), String.valueOf(msgCpuId)});
- }
- if (checkText(message, "SHUTDOWN")) {
- try {
- connection.close();
- } catch (Exception e) {
- e.printStackTrace(System.out);
- }
- } else if (checkText(message, "REPORT")) {
- // send a report:
- try {
- long time = System.currentTimeMillis() - start;
- String msg = "Received " + count + " in " + time + "ms";
- producer.send(session.createTextMessage(msg));
- } catch (Exception e) {
- e.printStackTrace(System.out);
- }
- count = 0;
- } else {
- if (count == 0) {
- start = System.currentTimeMillis();
- }
- if (++count % 1000 == 0) {
- System.out.println("Received " + count + " messages.");
- }
- }
- }
- public void setUrl(String url) {
- this.url = url;
- }
- }
- public class TopicPublisher implements MessageListener {
- private static final char[] DATA = "abcdefghijklmnopqrstuvwxyz".toCharArray();
- private final Object mutex = new Object();
- private Connection connection;
- private Session session;
- private MessageProducer publisher;
- private Topic topic;
- private Topic control;
- private String url = "tcp://localhost:61616";
- private int size = 256;
- private int subscribers = 1;
- private int remaining;
- private int messages = 10000;
- private long delay;
- private int batch = 2000;
- private byte[] payload;
- public static void main(String[] argv) throws Exception {
- TopicPublisher p = new TopicPublisher();
- String[] unknown = CommandLineSupport.setOptions(p, argv);
- if (unknown.length > 0) {
- System.out.println("Unknown options: " + Arrays.toString(unknown));
- System.exit(-1);
- }
- p.run();
- }
- private void run() throws Exception {
- ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
- connection = factory.createConnection();
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- topic = session.createTopic("topictest.messages");
- control = session.createTopic("topictest.control");
- publisher = session.createProducer(topic);
- publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- payload = new byte[size];
- for (int i = 0; i < size; i++) {
- payload[i] = (byte)DATA[i % DATA.length];
- }
- session.createConsumer(control).setMessageListener(this);
- connection.start();
- long[] times = new long[batch];
- for (int i = 0; i < batch; i++) {
- if (i > 0) {
- Thread.sleep(delay * 1000);
- }
- times[i] = batch(messages);
- System.out.println("Batch " + (i + 1) + " of " + batch + " completed in " + times[i] + " ms.");
- }
- long min = min(times);
- long max = max(times);
- System.out.println("min: " + min + ", max: " + max + " avg: " + avg(times, min, max));
- // request shutdown
- publisher.send(session.createTextMessage("SHUTDOWN"));
- connection.stop();
- connection.close();
- }
- private long batch(int msgCount) throws Exception {
- long start = System.currentTimeMillis();
- remaining = subscribers;
- publish();
- waitForCompletion();
- return System.currentTimeMillis() - start;
- }
- private void publish() throws Exception {
- // send events
- BytesMessage msg = session.createBytesMessage();
- msg.writeBytes(payload);
- for (int i = 0; i < messages; i++) {
- publisher.send(msg);
- if ((i + 1) % 1000 == 0) {
- System.out.println("Sent " + (i + 1) + " messages");
- }
- }
- // request report
- publisher.send(session.createTextMessage("REPORT"));
- }
- private void waitForCompletion() throws Exception {
- System.out.println("Waiting for completion...");
- synchronized (mutex) {
- while (remaining > 0) {
- mutex.wait();
- }
- }
- }
- public void onMessage(Message message) {
- synchronized (mutex) {
- System.out.println("Received report " + getReport(message) + " " + --remaining + " remaining");
- if (remaining == 0) {
- mutex.notify();
- }
- }
- }
- Object getReport(Message m) {
- try {
- return ((TextMessage)m).getText();
- } catch (JMSException e) {
- e.printStackTrace(System.out);
- return e.toString();
- }
- }
- static long min(long[] times) {
- long min = times.length > 0 ? times[0] : 0;
- for (int i = 0; i < times.length; i++) {
- min = Math.min(min, times[i]);
- }
- return min;
- }
- static long max(long[] times) {
- long max = times.length > 0 ? times[0] : 0;
- for (int i = 0; i < times.length; i++) {
- max = Math.max(max, times[i]);
- }
- return max;
- }
- static long avg(long[] times, long min, long max) {
- long sum = 0;
- for (int i = 0; i < times.length; i++) {
- sum += times[i];
- }
- sum -= min;
- sum -= max;
- return sum / times.length - 2;
- }
- public void setBatch(int batch) {
- this.batch = batch;
- }
- public void setDelay(long delay) {
- this.delay = delay;
- }
- public void setMessages(int messages) {
- this.messages = messages;
- }
- public void setSize(int size) {
- this.size = size;
- }
- public void setSubscribers(int subscribers) {
- this.subscribers = subscribers;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- }