zoukankan      html  css  js  c++  java
  • springboot之activemq安装与实践

    环境:腾讯云centos7 

    注意:activemq安装插件,可能会报错。本人是主机名的问题,所以修改了主机名。
    
    vim /etc/hosts
    
    vim /etc/hostname
    
    修改这两个文件,并重启主机,然后再运行activemq,没有问题了。

    1、下载安装包

    https://activemq.apache.org/components/classic/download/

    2、解压安装包

    tar -xzvf apache-activemq-5.15.9-bin.tar.gz

    3、安装

    mkdir /usr/java/
    
    mv apache-activemq-5.15.9 /usr/java/activemq5.15
    
    cd /usr/java/activemq5.15

    4、运行mq

    activemq    start 
                stop
               status

    5、验证是否启动成功

    ss -tnalp 查看端口,如果有61616,则运行成功。

    6、进入管理界面查看

    http://ip:8161   初始密码:admin/admin

    7、设置管理界面密码

        a) 在 mq安装目录/conf/jetty.xml中,添加权限控制。
            <bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
                <property name="name" value="BASIC" />
                <property name="roles" value="admin" />
                <property name="authenticate" value="<span style="color:#ff0000;">true</span>" />
            </bean>
    
        b)在 mq安装目录/conf/jetty-realm.properties中,添加用户
            admin:password,admin

    完毕!

     #########springboot实践##########

    1、pom.xml添加依赖

      <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
      </dependency>

    2、yml文件添加服务ip

    #默认使用配置  #activemq
    spring:
      activemq:
        broker-url: tcp://132.232.44.82:61616

    3、ActiveProducer.java

    package com.cn.commodity.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.HashMap;
    import java.util.Map;
    
    @RestController
    public class ActiveProducer {
        //注入jsmtemplate
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    
        @RequestMapping("/sendMsg")
        public String sendMsg(String msg) {
            jmsMessagingTemplate.convertAndSend("my_msg", msg);
            System.out.println("msg发送成功:"+msg);
            return "正发送邮件以及短信验证码,请注意查收";
        }
    
        @RequestMapping("/sendMap")
        public String sendMap() {
            Map map = new HashMap();
            map.put("mobile", "13888888888");
            map.put("content", "王总喜提兰博基尼");
            jmsMessagingTemplate.convertAndSend("my_map", map);
            System.out.println("map发送成功: "+map);
            return "map发送成功";
        }
    }

    4、ActiveConsumer.java

    package com.cn.commodity.controller;
    
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    import java.util.Map;
    
    @Component
    public class ActiveConsumer {
    
        @JmsListener(destination = "my_msg")
        public void readMsg(String text) {
            if(text!=null){
                System.out.println("接收到消息:" + text);
            }
        }
    
        @JmsListener(destination = "my_map")
        public void readMap(Map map) {
            System.out.println(map);
        }
    }

    启动测试 ,完毕!

  • 相关阅读:
    第一次作业
    第0次作业—姚舜禹17-1
    第三周作业
    第二周作业
    第一周作业
    第零周作业
    第三周作业
    第二周作业
    第一周作业
    第0次作业
  • 原文地址:https://www.cnblogs.com/ywjfx/p/11309778.html
Copyright © 2011-2022 走看看