栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

activeMq安装及使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

activeMq安装及使用

activeMq
  • 一: linux安装mq
    • 1.下载路径
    • 2. 配置jdk
    • 3.上传apache-activemq-5.16.0-bin.tar.gz
    • 4.配置外部访问
    • 5. 启动
    • 6.浏览器访问
  • 二: java连接mq
    • 1.准备工作pom.xml
    • 2.编写生产者代码
    • 3.编写消费者代码

一: linux安装mq 1.下载路径
	http://activemq.apache.org/
	https://activemq.apache.org/components/classic/download/

2. 配置jdk
[root@oracle bin]# vim /etc/profile
JAVA_HOME=/opt/jdk1.8.0_221;
export PATH=$JAVA_HOME/bin:$PATH
3.上传apache-activemq-5.16.0-bin.tar.gz

解压tar包

tar -zxvf         apache-activemq-5.16.0-bin.tar.gz     -C  ./mq/
4.配置外部访问
cd  /opt/mq/apache-activemq-5.16.0/conf
[root@oracle conf]# vim jetty.xml

5. 启动
cd  /opt/mq/apache-activemq-5.16.0/bin/linux-x86-64
[root@oracle linux-x86-64]# ./activemq  start
6.浏览器访问

二: java连接mq 1.准备工作pom.xml


    4.0.0

    org.example
    ActiveMqJmsProduce
    1.0-SNAPSHOT

    
        8
        8
    

    
        
            org.apache.activemq
            activemq-all
            5.15.9
        

        
            org.apache.xbean
            xbean-spring
            3.16
        


        
            org.slf4j
            slf4j-api
            1.7.30
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
        
            org.projectlombok
            lombok
            1.18.16
        
        
            junit
            junit
            4.13.1
        
    

2.编写生产者代码
package com.yy;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;

import javax.jms.*;
import java.io.IOException;



public class JmsConsumer {

    private static final String brokerURL="tcp://192.168.138.131:61616";

    public static void main(String[] args) throws JMSException, IOException, InterruptedException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        // 事务  签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("first quese");
        MessageConsumer consumer = session.createConsumer(destination);



//        Message receive = consumer.receive();
//        String name = receive.getStringProperty("name");
//        System.out.println(name);

        


        consumer.setMessageListener(x->{

            if(x!=null&& x instanceof  TextMessage){
                String text = null;
                try {
                    text = ((TextMessage) x).getText();
                    System.out.println(text);
                } catch (JMSException e) {
                    e.printStackTrace();
                }



            }
        });

        Thread.currentThread().join();

        //System.in.read();
        consumer.close();
        session.close();
        connection.close();



    }


}

3.编写消费者代码
package com.yy;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;


import javax.jms.*;



public class JmsProduce {

    private static final String brokerURL="tcp://192.168.138.131:61616";

    public static void main(String[] args) throws JMSException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        // 事务  签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);



        Destination destination = session.createQueue("first quese");
        MessageProducer producer = session.createProducer(destination);


//        Message msg = new ActiveMQTextMessage();
//        msg.setStringProperty("name","tomcat");

        for (int i = 0; i <3 ; i++) {
            Message msg = session.createTextMessage(i + "msg");
            producer.send(msg);
        }



        producer.close();

        session.close();

        connection.close();




    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/307054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号