• Java连接ActiveMQ代码示例(Producer和Consumer)


    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    import javax.jms.*;
    
    public class JMSProducer {
        //默认连接用户名
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        //默认连接密码
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
        //默认连接地址
        private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;
        //发送的消息数量
        private static final int SENDNUM = 10;
    
        public static void main(String[] args) {
            //连接工厂
            ConnectionFactory connectionFactory;
            //连接
            Connection connection = null;
            //会话 接受或者发送消息的线程
            Session session;
            //消息的目的地
            Destination destination;
            //消息生产者
            MessageProducer messageProducer;
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
    
            try {
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
                //创建一个名称为HelloWorld的消息队列
                destination = session.createQueue("HelloWorld");
                //创建消息生产者
                messageProducer = session.createProducer(destination);
                //发送消息
                sendMessage(session, messageProducer);
    
                session.commit();
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(connection != null){
                    try {
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
        /**
         * 发送消息
         * @param session
         * @param messageProducer  消息生产者
         * @throws Exception
         */
        public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
            for (int i = 0; i < JMSProducer.SENDNUM; i++) {
                //创建一条文本消息
                TextMessage message = session.createTextMessage("消息提供者发送到MQ的第" + i + "个报文!");
                System.out.println("消息提供者发送到MQ的第" + i + "个报文!");
                //通过消息生产者发出消息
                messageProducer.send(message);
            }
    
        }
    }
    JMSProducer
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    import javax.jms.*;
    
    public class JMSConsumer {
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//默认连接用户名
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//默认连接密码
        private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;//默认连接地址
    
        public static void main(String[] args) {
            ConnectionFactory connectionFactory;//连接工厂
            Connection connection = null;//连接
    
            Session session;//会话 接受或者发送消息的线程
            Destination destination;//消息的目的地
    
            MessageConsumer messageConsumer;//消息的消费者
    
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
    
            try {
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                //创建一个连接HelloWorld的消息队列
                destination = session.createQueue("HelloWorld");
                //创建消息消费者
                messageConsumer = session.createConsumer(destination);
    
                while (true) {
                    TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
                    if(textMessage != null){
                        System.out.println("收到的消息:" + textMessage.getText());
                    }else {
                        break;
                    }
                }
    
            } catch (JMSException e) {
                e.printStackTrace();
            }
    
        }
    }
    JMSConsumer

     转自:https://blog.csdn.net/jiuqiyuliang/article/details/48608237

  • 相关阅读:
    最小生成树(二)Prim算法
    红黑树
    最短路径:Dijstra(迪杰斯特拉)算法
    30岁的程序员,未来之路-写于疫情笼罩下的北京-中国-地球
    Spring Cloud 系列之Hystrix、Ribbon、Feign 源码剖析(一)引子
    A left join B B表有多条记录,max(create_time)取最新一条
    最新idea破解码,亲测可用
    Spring Cloud Gateway简单使用
    开源的C#实现WebSocket协议客户端和服务器websocket-sharp组件解析
    简单易用的.NET免费开源RabbitMQ操作组件EasyNetQ解析
  • 原文地址:https://www.cnblogs.com/dicke/p/8666279.html
Copyright © 2020-2023  润新知