zoukankan      html  css  js  c++  java
  • 9、RabbitMQ-集成Spring

     spring封装RabbitMQ看官网:https://spring.io/projects/spring-amqp#learn

    开发时根据官网的介绍进行开发,具体的说明都有具体的声明

     1、导入依赖

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

     2、配置文件

    config.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:rabbit="http://www.springframework.org/schema/rabbit"
           xsi:schemaLocation="http://www.springframework.org/schema/rabbit
               http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">
    
            <!-- 1、定义RabbitMQ的连接工厂 -->
            <rabbit:connection-factory id="connectionFactory" host="192.168.1.130"
            username="user" password="user" virtual-host="/user" port="5672"/>
            
            <!-- 消息发送到交换机还是队列 -->
            <!-- 2、定义Rabbit模板指定连接的工厂以及定义的exchange -->
            <!-- 可以指定消息发送到交换机还是队列 -->
            <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
            exchange="spring_exchange" ></rabbit:template>
            
            <!-- MQ的管理:包括队列、交换机等 -->
            <!-- 如交换机、队列是否存在,是否需要进行创建 -->
            <rabbit:admin connection-factory="connectionFactory"/>
                    
            <!-- 定义队列 -->
            <!-- auto-declare:自动声明,交换机不存在的时候进行创建,存在不声明 -->
            <rabbit:queue name="myQueue" auto-declare="true"></rabbit:queue>
            
            <!-- 定义交换机 -->
            <!-- auto-declare:自动声明,交换机不存在的时候进行创建,存在不声明 -->
            <rabbit:fanout-exchange name="spring_exchange" auto-declare="true" >
                <!-- 将队列绑定到交换机 -->
                <rabbit:bindings>
                    <rabbit:binding queue="myQueue"></rabbit:binding>
                </rabbit:bindings>
            </rabbit:fanout-exchange>
    
            <!-- 定义监听容器,当收到消息的时候会执行内部的配置 -->
            <rabbit:listener-container connection-factory="connectionFactory">
                <!-- 定义那个方法用于用于处理到接收到的消息 -->
                <rabbit:listener ref="consumer" method="listen" queue-names="myQueue"/>
            </rabbit:listener-container>
            
            <!-- 消费者 -->
            <bean id="consumer" class="com.rabbitmq.spring.Receive"></bean>
    </beans>

    3、消费者

    package com.rabbitmq.spring;
    
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Receive {
        //接受消息
        public void listen(String foo){
            System.out.println("foo:" + foo);
        }
    }

     4、测试类

    package com.rabbitmq.spring;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class test {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext  app = new ClassPathXmlApplicationContext("classpath:/config.xml");
            RabbitTemplate template = app.getBean(RabbitTemplate.class);
            template.convertAndSend("hello foo");
            app.close();
            System.out.println("send..."); 
        }
    
    }

     结果如图:

     详细具体可以参考:https://blog.csdn.net/leixiaotao_java/article/details/78952930

  • 相关阅读:
    NoSuchMethodError 一般是jar包冲突了
    联通网络环境上无法访问http://repo1.maven.org/maven2/中央库解决,镜像库添加
    实现MySQL数据库的实时备份
    海外支付:遍布全球的Paypal
    .Net分布式缓存应用实例:Couchbase
    海外支付:抵御信用卡欺诈的CyberSource
    那些年,我们开发的接口之:QQ登录(OAuth2.0)
    ES6知识整理(一)--- let/const/箭头函数
    webpack 热更新(实施同步刷新)
    Vue状态管理vuex
  • 原文地址:https://www.cnblogs.com/Mrchengs/p/10533340.html
Copyright © 2011-2022 走看看