zoukankan      html  css  js  c++  java
  • springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列
    本篇主要在上篇基础上进行activiemq消息队列的接收
    springboot整合mq发送消息队列
    第一步:新建marven项目,配置pom文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.sunjian.activitymq.receiver</groupId>
      <artifactId>receiver</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <parent>
             <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
             <version>1.5.4.RELEASE</version>
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
             <java.version>1.8</java.version>
        </properties>
        <dependencies>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter</artifactId>
             </dependency>
             <!-- spring boot web支持:mvc,aop... -->
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-web</artifactId>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-test</artifactId>
                 <scope>test</scope>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-activemq</artifactId>
             </dependency>
             <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
             <dependency>
                 <groupId>com.alibaba</groupId>
                 <artifactId>fastjson</artifactId>
                 <version>1.2.38</version>
             </dependency>
        </dependencies>
        <build>
             <plugins>
                 <plugin>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-maven-plugin</artifactId>
                 </plugin>
             </plugins>
        </build>
    </project>
    

    第二步:配置application.yml,注意port端口号必须与发送消息的端口号不一样,否则会端口冲突

    spring:
      activemq:
        broker-url: tcp://127.0.0.1:61616
        user: admin
        password: admin
    queue: sunjian
    server:
      port: 8083
    

    第三步:新建Consumer消息队列

    package com.sunjian.consumer;
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    import com.alibaba.fastjson.JSONObject;
    import com.sunjian.entity.UserEntity;
    @Component
    public class Consumer {
        @JmsListener(destination = "${queue}")//activeMq监听监听接收消息队列
        public void receive(String msg){//这个msg就是从消息队列获得到的参数
             System.out.println(msg);
             JSONObject jsonObject = new JSONObject();
             UserEntity userEntity = jsonObject.parseObject(msg,UserEntity.class);
            System.out.println(userEntity.getName()+"---"+userEntity.getId());
        }
    }
    

    第四步:新建UserEntity实体类

    package com.sunjian.entity;
    public class UserEntity {
        private Long id;
        private String name;
        private Integer age;
        public UserEntity(Long id, String name, Integer age) {
             super();
             this.id = id;
             this.name = name;
             this.age = age;
        }
        public Long getId() {
             return id;
        }
        public void setId(Long id) {
             this.id = id;
        }
        public String getName() {
             return name;
        }
        public void setName(String name) {
             this.name = name;
        }
        public Integer getAge() {
             return age;
        }
        public void setAge(Integer age) {
             this.age = age;
        }
    }
    

    第五步:新建App执行程序,这里不在讲关于mq的安装问题,如果要看清浏览
    springboot整合mq发送消息队列

    package com.sunjian;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
    }
    

    第六步:运行以后查看效果

     
    Image.png
     
    Image.png
    原文链接:
    http://www.jianshu.com/p/b93d54d3584b
     

    关注我的公众号,都是满满的干货!
  • 相关阅读:
    MongoDB分片集群还原
    集群因子(Clustering Factor)
    Sunisoft.IrisSkin.SkinEngine 设置winform皮肤
    17monipdb根据IP获得区域
    ArraySegmentSample
    RichTextBox指定全部文字显示不同颜色及部分文字高亮颜色显示
    Git 基本分支规范
    C# 获取方法内参数名称
    (转)C#中的Predicate<T>与Func<T, bool>
    EF 多线程TransactionScope事务异常"事务EFTransaction类定义:与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务。"
  • 原文地址:https://www.cnblogs.com/springboot/p/8051897.html
Copyright © 2011-2022 走看看