1.消息中间件:采用异步通讯防止,支持点对点以及发布订阅模式,可以解决高并发问题
传统调用接口,可能发生阻塞,重复提交,超时等等问题,可以利用消息中间件发送异步通讯请求
点对点:生产者 消息队列 消费者
发布订阅:生产者 主题 消费者1 消费者N
2.windows安装ActiveMQ
2.1 解压,进入apache-activemq-5.11.1inwin64
2.2 启动,双击activeMQ.bat脚本启动,启动窗口不要关闭,可以设置后台启动
2.3 启动完成后,如果发送消息或者消费消息通过61616端口进行 后台查看信息通过8161端口查看
2.4 进入后台登陆:默认用户名和密码都是admin
3.点对点通讯
生产者:
public static void main(String[] args) throws JMSException { //步骤一:创建连接工厂 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616"); //步骤二:创建连接 Connection connection = activeMQConnectionFactory.createConnection(); //步骤三:启动连接 connection.start(); //步骤四:获取会话工厂 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //步骤五:创建队列 Queue queue = session.createQueue("wdksoft_queue"); //创建消息生产者 MessageProducer producer = session.createProducer(queue); //消息持久化 producer.setDeliveryMode(2); //模拟消息 TextMessage textMessage = session.createTextMessage("hello activeMQ"); //发送消息 producer.send(textMessage); System.out.println("生产者生产消息完毕~"); //回收资源 session.close(); connection.close(); }
消费者:
public static void main(String[] args) throws JMSException { //步骤一:创建连接工厂 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); //步骤二:创建连接 Connection connection = activeMQConnectionFactory.createConnection(); //步骤三:开启连接 connection.start(); //创建会话对象 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //获取到接受消息的队列 Queue queue = session.createQueue("wdksoft_queue"); //创建消费者 MessageConsumer consumer = session.createConsumer(queue); while(true){ //获取消息 TextMessage message = (TextMessage)consumer.receive(); if(message!=null){ System.out.println("消费者获取消息:"+message.getText()); }else{ break; } } //回收资源 session.close(); connection.close(); }
4.发布订阅模式:消费者先订阅Topic制图,再生产消息
4.1 消费者:
public static void main(String[] args) throws JMSException { //步骤一:创建连接工厂 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); //步骤二:创建连接 Connection connection = activeMQConnectionFactory.createConnection(); //步骤三:开启连接 connection.start(); //创建会话对象 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //获取到接受消息的队列 Topic topic = session.createTopic("wdksoft_topic"); //创建消费者 MessageConsumer consumer = session.createConsumer(topic); while(true){ //获取消息 TextMessage message = (TextMessage)consumer.receive(); if(message!=null){ System.out.println("消费者获取消息:"+message.getText()); }else{ break; } } //回收资源 session.close(); connection.close(); }
4.2 生产者:
public static void main(String[] args) throws JMSException { //步骤一:创建连接工厂 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616"); //步骤二:创建连接 Connection connection = activeMQConnectionFactory.createConnection(); //步骤三:启动连接 connection.start(); //步骤四:获取会话工厂 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //步骤五:创建主题 Topic topic = session.createTopic("wdksoft_topic"); //创建消息生产者 MessageProducer producer = session.createProducer(null); //消息持久化 producer.setDeliveryMode(2); //模拟消息 TextMessage textMessage = session.createTextMessage("hello activeMQ pub"); //发送消息 producer.send(topic,textMessage); System.out.println("生产者生产消息完毕~"); //回收资源 session.close(); connection.close(); }