ActiveMQ基本应用

一、参考资料

JMS与ActiveMQ的关系,概念:http://boy00fly.iteye.com/blog/1103586
ActiveMQ的热备份与集群配置:http://www.open-open.com/lib/view/open1400126457817.html
queue与topic代码:http://longdick.iteye.com/blog/465229
实际应用案例:http://www.codeceo.com/article/be-a-polyglot-programmer.html

二、基本概念理解

Apache ActiveMQ是一个消息传递的中间件,符合JMS的规范实现。
JMS里面的角色:
     1、Producer,产生消息;
     2、Consumer,接收消息;
     3、Provider,消息中间人,用于接收Producer的消息并发送到Consumer,类似产品有:Apache的ActiveMQ。RabbitMQ使用AMQP协议

三、ActiveMQ的应用

1、ActiveMQ服务器基本应用

 ·官网下载ActiveMQ服务器,http://activemq.apache.org/
 ·解压ZIP,进入conf,确认activemq.xml中的tcp://127.0.0.1:61616,供Producer与Consumer访问的地址与端口。

<transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://127.0.0.1:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

 ·同是conf目录下,确认jetty.xml中的端口号,这个用于进入ActiveMQ的管理页面:http://127.0.0.1:8162/admin,可以查看消息的消费情况。

    <bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
             <!-- the default port number for the web console -->
        <property name="host" value="0.0.0.0"/>
        <property name="port" value="8162"/>
    </bean>

2、Queue模式与Topic模式
      Queue模式:点对点的传递模式,Producer发送消息到Provider,若有多个Consumer在Provider的监听队列,则采用先到先得的原则,先到的先获取消息。若没有Consumer在Provider的监听队列,则直到有Consumer在监听队列为止。若Consumer收到消息不响应Provider,则Provider再发送给另外一个Consumer。该模式是由Consumer请求Provider获取消息的。

添加Maven
        <dependencies>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-all</artifactId>
                <version>5.13.1</version>
            </dependency>
        </dependencies>



//Producer
public static void send(){
        MessageProducer messageProducer=null;//消息发送者
        //连接工厂,配置连接JMSProvider的用户,密码,地址
        ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "failover:(tcp://127.0.0.1:61616)");
        Connection connection=null;
        try {
            connection=connectionFactory.createConnection();//连接JMSProvider
            Session session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
            //创建Queue,发送一个消息到JMSProvider,只能有一个接收者接收,若没有则存在JMSProvider中
            Destination destination=session.createQueue("TEST_QUEUE");
            //创建Topic,发送消息到订阅者们
            //destination=session.createTopic("TEST_TOPIC");
            messageProducer=session.createProducer(destination);//消息生产者
            //设置持久化模式则重启ActiveMQ服务,消息还在队列中
            //设置非持久化,则重启ActiveMQ服务,消息不在队列中
            //messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
            //定义消息
            TextMessage message=session.createTextMessage("Hello,This is Sender");
            //ObjectMessage objectMessage=session.createObjectMessage();
            messageProducer.send(message);//发送消息
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if(connection!=null){
                try {
                    connection.close();//关闭连接
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
//Customer
public static void receive(){
        ConnectionFactory connectionFactory;
        Connection connection=null;
        Session session;
        Destination destination;
        MessageConsumer messageConsumer;
        connectionFactory=new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "failover:(tcp://127.0.0.1:61616)");
        try {
            connection=connectionFactory.createConnection();
            session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination=session.createQueue("TEST_QUEUE");
            messageConsumer=session.createConsumer(destination);
            messageConsumer.setMessageListener(new MyListener());
            connection.start();
        } catch (JMSException e) {
            e.printStackTrace();
        }
}

      Topic模式:发布/订阅消息,Producer产生消息发送到Provider,发送到订阅了该Producer且处于活动状态的Consumer处。与Queen模式不同,消息可以被多个Consumer接收,Provider一直保存所有消息,直到消息被所有Consumer接收。与Queue不同的是,Provider会自动广播消息到Consumer,不需要Consumer来拉取

//Producer
public static void send(){
        MessageProducer messageProducer;//消息发送者
        //连接工厂,配置连接JMSProvider的用户,密码,地址
        ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "failover:(tcp://127.0.0.1:61616)");
        Connection connection=null;
        try {
            connection=connectionFactory.createConnection();//连接JMSProvider
            connection.start();
            Session session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);//设置true表示开启事务
            //创建Topic,发送消息到订阅者们
            Destination destination=session.createTopic("TEST_TOPIC");
            messageProducer=session.createProducer(destination);//消息生产者
//            messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);//持久化模式
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//非持久化模式
            //定义消息
            TextMessage mapMessage=session.createTextMessage("Hello,This is Sender by Topic");
            messageProducer.send(mapMessage);//发送消息
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if(connection!=null){
                try {
                    connection.close();//关闭连接
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
//Customer
public static void receive() {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        Session session;
        Destination destination;
        MessageConsumer messageConsumer;
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "failover:(tcp://127.0.0.1:61616)");
        try {
            connection = connectionFactory.createConnection();
            connection.setClientID("");//在持久模式下需要设置CLIENTID
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic("TEST_TOPIC");
            messageConsumer = session.createConsumer(destination);
            messageConsumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    try {
                        String receiveMessage = ((TextMessage)message).getText();
                        System.out.println("接收到的消息:" + receiveMessage);
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });
            connection.start();
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
//            if (connection != null) {
//                try {
//                    connection.close();
//                } catch (JMSException e) {
//                    e.printStackTrace();
//                }
//            }
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值