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();
            
        }
    
    }
  • 相关阅读:
    springboot + rabbitmq 做智能家居,我也没想到会这么简单
    分享 10个我常逛的国外技术社区,真的受益匪浅!
    被迫重构代码,这次我干掉了 if-else
    过滤器 和 拦截器 6个区别,别再傻傻分不清了
    看了 100多份简历后,想给找工作的程序员几点建议
    不会看 Explain执行计划,劝你简历别写熟悉 SQL优化
    友情链接
    关于我
    10w行级别数据的Excel导入优化记录
    kafka 监控工具 eagle 的安装(内附高速下载地址)
  • 原文地址:https://www.cnblogs.com/phyxis/p/5195670.html
Copyright © 2011-2022 走看看