• ActiveMQ使用例子


    网上收集的例子:有broker,producer,consumer

    public class MqApp {
        public static void main(String[] args) throws Exception {
            // 启动broker
            BrokerService broker = new BrokerService();
            // configure the broker
            broker.addConnector("tcp://localhost:61616");
            broker.start();
            
            // 创建producer和consumer
            makeThread(new HelloProducer(), false);
            makeThread(new HelloConsumer(), false);
            
    //        LockSupport.park();
        }
     
        public static void makeThread(Runnable runnable, boolean daemon) {
            Thread t = new Thread(runnable);
            t.setDaemon(daemon);
            t.start();
        }
     
        public static class HelloProducer implements Runnable {
            public void run() {
                try {
                    // Create a ConnectionFactory
                    ActiveMQConnectionFactory connectionFactory =
    //                        new ActiveMQConnectionFactory("vm://localhost");
                            new ActiveMQConnectionFactory("tcp://localhost:61616");
     
                    // Create a Connection
                    Connection connection = connectionFactory.createConnection();
                    connection.start();
     
                    // Create a Session
                    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
     
                    // Create the destination (Topic or Queue)
                    Destination destination = session.createQueue("TEST.FOO");
     
                    // Create a MessageProducer from the Session to the Topic or Queue
                    MessageProducer producer = session.createProducer(destination);
                    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
     
                    // Create a messages
                    String text = "Hello world! From: " + Thread.currentThread().getName() + ", " + this.hashCode();
                    TextMessage message = session.createTextMessage(text);
     
                    // Tell the producer to send the message
                    System.out.println("Sent message: "+ message.hashCode() + ", " + Thread.currentThread().getName());
                    producer.send(message);
     
                    // Clean up
                    session.close();
                    connection.close();
                }
                catch (Exception e) {
                    System.out.println("Caught: " + e);
                    e.printStackTrace();
                }
            }
        }
     
        public static class HelloConsumer implements Runnable, ExceptionListener {
            public void run() {
                try {
                    // Create a ConnectionFactory
                    ActiveMQConnectionFactory connectionFactory =
    //                        new ActiveMQConnectionFactory("vm://localhost");
                            new ActiveMQConnectionFactory("tcp://localhost:61616");
     
                    // Create a Connection
                    Connection connection = connectionFactory.createConnection();
                    connection.start();
     
                    connection.setExceptionListener(this);
     
                    // Create a Session
                    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
     
                    // Create the destination (Topic or Queue)
                    Destination destination = session.createQueue("TEST.FOO");
     
                    // Create a MessageConsumer from the Session to the Topic or Queue
                    MessageConsumer consumer = session.createConsumer(destination);
     
                    // Wait for a message
                    Message message = consumer.receive(1000);
     
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        String text = textMessage.getText();
                        System.out.println("Received: " + text);
                    } else {
                        System.out.println("Received: " + message);
                    }
     
                    consumer.close();
                    session.close();
                    connection.close();
                } catch (Exception e) {
                    System.out.println("Caught: " + e);
                    e.printStackTrace();
                }
            }
     
            public synchronized void onException(JMSException ex) {
                System.out.println("JMS Exception occured.  Shutting down client.");
            }
        }
    }
  • 相关阅读:
    mysql leetcode 1445. 苹果和桔子 分组求差值
    前后端分离的理解
    mysql leetcode 178. 分数排名 自定义排名序号列 1,2,3
    mysql 距离今日,过去一年/一个月/一天 表达式
    mysql leetcode 1280. 学生们参加各科测试的次数 解题思路 一步一步来
    mysql leetcode 1435. 制作会话柱状图 解题思路 一步一步来
    leetcode 刷题笔记 寻找数组的中心索引 二分法查找
    mysql case when 理解和应用
    三角函数
    冒泡排序,选择排序,插入排序
  • 原文地址:https://www.cnblogs.com/allenwas3/p/8509099.html
Copyright © 2020-2023  润新知