阿里云AMQP Spring集成

简介: 本文主要演示如何使用Spring集成开发阿里云的AQMP。

概述

本文主要演示如何使用Spring集成开发阿里云的AMQP。

主要配置及步骤

1、pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.6.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.mq-amqp</groupId>
            <artifactId>mq-amqp-client</artifactId>
            <version>1.0.3</version>
        </dependency>
    </dependencies>

2、spring-rabbitmq.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd  http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <!--组件扫描,需要添加pom依赖 spring-context  注意根据自己的包名称配置 -->
    <context:component-scan  base-package="com.alibaba.taro" />

    <bean id="userUtils" class="com.alibaba.mq.amqp.utils.UserUtils"/>

    <!--配置连接-->
    <!--参数获取参考链接:https://yq.aliyun.com/articles/693979?spm=a2c4e.11155435.0.0.4a6126a2PDBrRl -->
    <rabbit:connection-factory id="connectionFactory" host="18****7816617278.mq-amqp.cn-hangzhou-a.aliyuncs.com" port="5672"
                               username="#{userUtils.getUserName('********',18****7816617278L)}" password="#{userUtils.getPassord('********')}"  virtual-host="yutaoamqptest" requested-heartbeat="60" />

    <!--配置RabbitTemplate-->
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
                     exchange="demo-test-exchange1" routing-key="foo.bar"/>

    <!--配置RabbitAdmin-->
    <rabbit:admin connection-factory="connectionFactory" />

    <!--配置队列名-->
    <rabbit:queue name="demo-test-queue" />

    <!--配置topic类型exchange-->
    <rabbit:topic-exchange name="demo-test-exchange1">
        <rabbit:bindings>
            <rabbit:binding queue="demo-test-queue" pattern="foo.*" />
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--&lt;!&ndash;&lt;!&ndash;配置监听&ndash;&gt;&ndash;&gt;   默认或不设置均为自动确认-->
    <!--<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">-->
        <!--<rabbit:listener ref="foo" method="onMessage" queue-names="demo-test-queue" />-->
    <!--</rabbit:listener-container>-->

    <!--&lt;!&ndash;配置监听&ndash;&gt;   手动确认-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
        <rabbit:listener ref="receiveConfirmTestListener" method="onMessage" queue-names="demo-test-queue" />
    </rabbit:listener-container>

</beans>

3、自动确认模式消息监听类

package com.alibaba.taro;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Service;

// 自动确认模式的消息消费
@Service
public class Foo implements MessageListener {
    public void onMessage(Message message) {
        try {
            String body=new String(message.getBody(),"UTF-8");
            System.out.println(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4、手动确认模式监听类

package com.alibaba.taro;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Service;

// 带有确认模式的消息消费
@Service("receiveConfirmTestListener")
public class ReceiveConfirmTestListener implements ChannelAwareMessageListener {
    public void onMessage(Message message, Channel channel) throws Exception {
        try{
            System.out.println("consumer--:"+message.getMessageProperties()+":"+new String(message.getBody()));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch(Exception e){
            e.printStackTrace();//TODO 业务处理
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
        }
    }
}

5、main方法

package com.alibaba.taro;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMain {

    public static void main(final String... args) throws Exception {

        AbstractApplicationContext ctx =
                new ClassPathXmlApplicationContext("spring-rabbitmq.xml");
        RabbitTemplate template = ctx.getBean(RabbitTemplate.class);

        int i = 0;
        while(i<100){
            Thread.sleep(100);
            template.convertAndSend("Hello, world!" + i);
            System.out.println("i: " + i);
            i++;
        }
        //销毁对象
        ctx.destroy();
    }
}

6、项目结构目录
_

7、测试运行效果
_

参考链接

AliwareMQ/amqp-demos

RabbitMQ(四)消息确认(发送确认,接收确认)

相关实践学习
快速体验阿里云云消息队列RocketMQ版
本实验将带您快速体验使用云消息队列RocketMQ版Serverless系列实例进行获取接入点、创建Topic、创建订阅组、收发消息、查看消息轨迹和仪表盘。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
11月前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
4230 166
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
541 1
将 Spring 微服务与 BI 工具集成:最佳实践
|
弹性计算 运维 安全
云迁移最佳实践:HyperMotion助中小企业高效上云,阿里云工具集深度集成三方迁移工具
中小企业上云需求强烈,但面临缺乏了解、无合适方案及成本过高等挑战。为解决这些问题,推出“云迁移HyperMotion阿里云集成版”,提供三步上云、自助迁移、自动适配等能力,助力企业高效、低成本完成迁移。
398 0
|
XML 人工智能 Java
Spring Boot集成Aviator实现参数校验
Aviator是一个高性能、轻量级的Java表达式求值引擎,适用于动态表达式计算。其特点包括支持多种运算符、函数调用、正则匹配、自动类型转换及嵌套变量访问,性能优异且依赖小。适用于规则引擎、公式计算和动态脚本控制等场景。本文介绍了如何结合Aviator与AOP实现参数校验,并附有代码示例和仓库链接。
786 0
|
安全 Java 数据库
第16课:Spring Boot中集成 Shiro
第16课:Spring Boot中集成 Shiro
1244 0
|
消息中间件 存储 Java
第15课: Spring Boot中集成ActiveMQ
第15课: Spring Boot中集成ActiveMQ
775 0
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1466 0
|
安全 Java Apache
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
732 0
|
安全 Java 数据安全/隐私保护
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
576 0

热门文章

最新文章