zoukankan      html  css  js  c++  java
  • JMS

    基本概念

    JMS是java的消息服务,JMS的客户端之间可以通过JMS服务进行异步的消息传输。

    简单的说就是 消息的生产者把消息发布出来,消息的消费者把获取订阅的消息。

    生产者实现:

    package com.ijiami.main.shiro.test;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.DeliveryMode;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class JmsMessageProducer {
        
        private static final String HOST = "127.0.0.1";
        private static final String PORT = "61616";
        
        public static void main(String[] args) {
            ConnectionFactory factory = null;
            Connection connection = null ;
            Destination destination = null ;
            Session session = null ;
            MessageProducer producer = null;
            
            try {
                //1 、创建ConnecttionFactory.  
                factory = new ActiveMQConnectionFactory("tcp://"+HOST+":"+PORT);
                
                //2、 创建Connecttion.
                connection = factory.createConnection();
                connection.start();
                
                //3、 创建Session.
                session = connection.createSession(false, session.AUTO_ACKNOWLEDGE);
                
                //4 、创建Destination.
                destination = session.createQueue("testList");
                
                //5、 创建MessageProducer.
                producer = session.createProducer(destination);
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                
                //6、 发送消息
                sentMessage(session, producer);
                session.commit();
                
            } catch (JMSException e) {
                e.printStackTrace();
            }finally{
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
            
        }
        
        public static void sentMessage(Session session , MessageProducer producer){
            
            try {
                for (int j = 0; j < 10; j++) {
                    Message message = session.createTextMessage();
                    message.setStringProperty("message","message :"+j);
                    producer.send(message);
                    Thread.sleep(2000);
                }
            } catch (JMSException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }

    消费者实现:

    package com.ijiami.main.shiro.test;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageListener;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class JmsMessageConsumer implements MessageListener{
    
        private static final String HOST = "127.0.0.1";
        private static final String PORT = "61616";
        
        @Override
        public void onMessage(Message message) {
            try {
                if (message instanceof TextMessage) {
                    TextMessage message2 = (TextMessage) message;
                    String mes = message2.getStringProperty("message");
                    System.out.println("receive message:"+mes);
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        
        public void receive(){
            
            ConnectionFactory factory = null;
            Connection connection = null;
            Session session = null;
            Destination destination = null;
            MessageConsumer consumer = null;
            
            try {
                //1 、创建ConnecttionFactory.
                factory = new ActiveMQConnectionFactory("tcp://"+HOST+":"+PORT);
                
                //2、 创建Connecttion.
                connection = factory.createConnection();
                connection.start();
                
                //3、 创建Session.
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                
                //4 、创建Destination.
                destination = session.createQueue("testList");
                
                //5、 创建MessageConsumer.
                consumer = session.createConsumer(destination);
                
                //监听消息
                consumer.setMessageListener(this);
            } catch (JMSException e) {
                e.printStackTrace();
            }
            
        }
        public static void main(String[] args) {
            new JmsMessageConsumer().receive();
            
        }
    
    }
  • 相关阅读:
    Github 上 36 个最实用的 Vue 开源库
    C 语言快速入门,21 个小项目足矣!「不走弯路就是捷径」
    18个挑战项目带你快速入门深度学习
    Linux 运维入门到跑路书单推荐
    Python 网络爬虫的常用库汇总
    45 个常用Linux 命令,让你轻松玩转Linux!
    [新手必备]Python 基础入门必学知识点笔记
    快速入门 Python 数据分析实用指南
    18位不重复订单号
    相对路径转绝对路径
  • 原文地址:https://www.cnblogs.com/phyxis/p/5195670.html
Copyright © 2011-2022 走看看