• ActiveMQ


    参考:http://yufenfei.iteye.com/blog/1874089      dome::http://blog.csdn.net/jiuqiyuliang/article/details/48608237

    1>ActiveMq的下载

      http://activemq.apache.org/activemq-5144-release.html

    2>ActiveMq的安装

      解压任意目录下即可

    3>启动ActiveMq服务器

      直接运行binactivemq.bat

    4>测试

      在cmd命令行执行: netstat -an|find"61616"

      C:>netstat -an|find "61616"

          TCP        0.0.0.0:61616                    0.0.0.0:0                            LISTENING

    5>监控

      ActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用

      Url:  http://127.0.0.1:8161/admin   需要输入登录用户名与密码;默认用户名与密码是 admin,admin

      注意:

       (1)、登录的用户与密码配置

               conf/jetty-realm.properties

       (2)、8161端口配置

               conf/jetty.xml

    6>点对点Dome

     (1)先将activemq-all-5.14.4.jar 引入工程的lib目录下

     (2)消息生产者

    package com.wyx.test;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class JmsProducer {
        //默认连接用户名
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        //默认连接密码
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
        //默认连接URL
        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 session;
            //消息目的地(Queue)
            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"的Queue
                destination = session.createQueue("HelloWorld");
                //创建消息生产者
                messageProducer = session.createProducer(destination);
                //发送消息
                sendMessage(session,messageProducer);
                //消息确认
                session.commit();            
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(connection != null){
                    try {
                        connection.close();
                    } catch (JMSException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        
        public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
            for(int i=0;i<JmsProducer.SENDNUM;i++){
                //创建一条文本消息
                TextMessage message = session.createTextMessage("ActiveMq 发送消息=>"+ i);
                System.out.println("发送消息---"+i);
                messageProducer.send(message);
            }
        }
    }

     (3)消息消费者

    package com.wyx.test;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageConsumer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.swing.plaf.synth.SynthSeparatorUI;
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class JmsConsumer {
        //默认连接名
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        //默认连接密码
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
        //默认连接URL
        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 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                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;
                    }
                }
                //确认消息
                session.commit();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    JAVA学习笔记 -- 包资源文件jar包裹
    Hibernate一个简短的引论
    JMS分布式应用程序异步消息解决方案EhCache 高速缓存同步问题
    一个红
    Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
    hdu 2243 考研绝望——复杂的文字(AC自己主动机+矩阵高速功率)
    我在这里3在引发众1.8万的经验分享
    关系数据库的基本概念和MySQL说明
    2015第5周日
    HTML5
  • 原文地址:https://www.cnblogs.com/yuxin-555xt/p/6560017.html
Copyright © 2020-2023  润新知