• ActiveMQ demo


    Maven 配置文件  

    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.9.1</version>
    </dependency>

    provider 生产者代码

    import static commons.Constants.*;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageProducer;
    import javax.jms.Queue;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class Provider {
        public static void main(String[] args) {
            ConnectionFactory cf = new ActiveMQConnectionFactory(URL); //创建工厂链接对象
            Connection connection = null;
            Session session  = null;
            Queue queue  = null;
            MessageProducer producer  = null;
            try {
                connection = cf.createConnection(); //使用工厂创建链接
                connection.start(); //开启连接
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //使用连接对象创建会话
                queue = session.createQueue(QUEUE_ONE); //使用会话创建目标对象
                producer = session.createProducer(queue); // 使用会话、目标对象创建生产者对象
                Message message = session.createTextMessage("hello ,i'm  not good"); //使用会话创建消息对象
                producer.send(message);//发送消息
                System.out.println("send:"+message.toString());
            } catch (JMSException e) {
                e.printStackTrace();
            }finally {
                if(producer != null) {
                    try {
                        producer.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
                if(session != null) {
                    try {
                        session.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
                if(connection != null) {
                    try {
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }

    Consumer 消费者

    import static commons.Constants.*;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageListener;
    import javax.jms.Queue;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class Consumer {
    
        public static void main(String[] args) {
            ConnectionFactory cf = new ActiveMQConnectionFactory(URL);
            Connection connection = null;
            Session session = null;
            MessageConsumer consumer = null;
            try {
                connection = cf.createConnection();
                connection.start();
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Queue queue = session.createQueue(QUEUE_ONE);
                consumer = session.createConsumer(queue);
                //向consumer对象中设置一个messageListener对象,用来接收消息
                consumer.setMessageListener(new MessageListener() {
                    public void onMessage(Message message) {
                        if(message instanceof TextMessage) { //当前测试,仅测试Text类型的消息
                            TextMessage text = (TextMessage)message;
                            try {
                                System.out.println(text.getText());
                            } catch (JMSException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                System.out.println("consumer : "+System.currentTimeMillis());
                System.in.read();
            }catch(Exception e) {
                e.printStackTrace();
            }finally {
                if(consumer != null) {
                    try {
                        consumer.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
                if(session != null) {
                    try {
                        session.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
                if(connection != null) {
                    try {
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    Constants 常量类

    package commons;
    
    public interface Constants {
    
        String URL = "tcp://192.168.49.128:61616";
        
        String QUEUE_ONE = "CQC_ONE";
            
    }
  • 相关阅读:
    概率期望,数学,贪心策略——2020-camp-day1-A
    k染色——2020-camp-day3-C
    树形dp——2020-camp-day3-G
    欧拉回路/路径——2020-camp-day2-H
    dsu on tree——2020-camp-day2-E
    Nim博弈,异或性质——2020-camp-day2-C
    一些视频资料
    开发人员收藏的网站
    各行公认的好书
    资料库链接
  • 原文地址:https://www.cnblogs.com/chen--biao/p/10127173.html
Copyright © 2020-2023  润新知