java之JMS
一、简介:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持。
二、JMS对象模型包含如下几个要素:
//初始化工厂 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); //设置链接地址 activeMQConnectionFactory.setBrokerURL("tcp://localhost:61616"); //创建链接 Connection connection = activeMQConnectionFactory.createConnection(); //启动服务 connection.start();
3)我这里简单写了一个activemq的具体实现过程,供参考
a、目录结构和依赖包
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.4</version> </dependency>
b、核心功能,消息处理中心
package com.pinnet.center; import com.pinnet.consumer.queue.Queue; import com.pinnet.consumer.topic.Topic1; import com.pinnet.consumer.topic.Topic2; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQObjectMessage; import javax.jms.*; import java.io.Serializable; public class MessageCenter { private static Session session; public static void init() throws JMSException { initConnection(); initRegister(); } private static void initConnection() throws JMSException { //初始化工厂 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); //设置链接地址 activeMQConnectionFactory.setBrokerURL("tcp://localhost:61616"); //创建链接 Connection connection = activeMQConnectionFactory.createConnection(); //创建会话 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //启动服务 connection.start(); } /** * 用于注册对应消息队列 * @throws JMSException */ public static void initRegister() throws JMSException { registerQueue(QueueType.QUEUE, new Queue()); registerTopic(TopicType.TOPIC, new Topic1()); registerTopic(TopicType.TOPIC, new Topic2()); } /** * 注册topic监听 * @param topicType * @param messageListener * @throws JMSException */ public static void registerTopic(TopicType topicType, MessageListener messageListener) throws JMSException { //将会话转成topic TopicSession topicSession = (TopicSession) session; //创建订阅者 TopicSubscriber topicSubscriber = topicSession.createSubscriber(topicSession.createTopic(topicType.name())); //设置监听 topicSubscriber.setMessageListener(messageListener); } /** * 注册queue监听 * @param queueType * @param messageListener * @throws JMSException */ public static void registerQueue(QueueType queueType, MessageListener messageListener) throws JMSException { //将会话转成queue QueueSession queueSession = (QueueSession) session; //创建接收者 QueueReceiver queueReceiver = queueSession.createReceiver(queueSession.createQueue(queueType.name())); //设置监听 queueReceiver.setMessageListener(messageListener); } /** * 发送topic消息 * @param topicType * @param serializable * @throws JMSException */ public static void sendMessageTopic(TopicType topicType, Serializable serializable) throws JMSException { TopicSession topicSession = (TopicSession) session; //创建发布者 TopicPublisher topicPublisher = topicSession.createPublisher(topicSession.createTopic(topicType.name())); //这里数据发布形式采用ObjectMessage ActiveMQObjectMessage activeMQObjectMessage = new ActiveMQObjectMessage(); activeMQObjectMessage.setObject(serializable); //发布消息 topicPublisher.publish(activeMQObjectMessage); } /** * 发送queue消息 * @param queueType * @param serializable * @throws JMSException */ public static void sendMessageQueue(QueueType queueType, Serializable serializable) throws JMSException { QueueSession queueSession = (QueueSession) session; //创建发送者 QueueSender queueSender = queueSession.createSender(queueSession.createQueue(queueType.name())); ActiveMQObjectMessage activeMQObjectMessage = new ActiveMQObjectMessage(); activeMQObjectMessage.setObject(serializable); //发送消息 queueSender.send(activeMQObjectMessage); } //使用枚举的目的是更好的管理 public enum TopicType { TOPIC } public enum QueueType { QUEUE } }
c、消息处理抽象类
package com.pinnet.consumer; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import java.io.Serializable; /** * 抽象类的目的是,让编写者,只管处理消息,不用管理中间过程 */ public abstract class MessageConsumer implements MessageListener { public void onMessage(Message message) { try { ObjectMessage objectMessage = (ObjectMessage) message; handleMessage(objectMessage.getObject()); } catch (JMSException e) { e.printStackTrace(); } } //实现类处理对应消息 public abstract void handleMessage (Serializable serializable); }
d、2个实现类型
package com.pinnet.consumer.queue; import com.pinnet.consumer.MessageConsumer; import java.io.Serializable; public class Queue extends MessageConsumer { /** * 消息处理 * @param serializable */ public void handleMessage(Serializable serializable) { System.out.println(serializable+"queue"); } }
package com.pinnet.consumer.topic; import com.pinnet.consumer.MessageConsumer; import java.io.Serializable; public class Topic1 extends MessageConsumer { /** * 消息处理 * @param serializable */ public void handleMessage(Serializable serializable) { System.out.println(serializable+"topic1"); } }
package com.pinnet.consumer.topic; import com.pinnet.consumer.MessageConsumer; import java.io.Serializable; public class Topic2 extends MessageConsumer { /** * 消息处理 * @param serializable */ public void handleMessage(Serializable serializable) { System.out.println(serializable+"topic2"); } }
e、测试:
package com.pinnet; import com.pinnet.center.MessageCenter; import javax.jms.JMSException; public class Main { public static void main(String[] args) throws JMSException, InterruptedException { MessageCenter.init(); while (true) { MessageCenter.sendMessageQueue(MessageCenter.QueueType.QUEUE, "queue"); MessageCenter.sendMessageTopic(MessageCenter.TopicType.TOPIC, "topic"); Thread.sleep(5000); } } }
六、我这里是采用纯代码的方式,在spring中jmsTemplate的应用,就像session一样,就可以。
springmvc的配置方式参考:https://www.cnblogs.com/ll409546297/p/6898155.html
配置方式也可以采用activemq的工厂连接模式一样实现

