zoukankan      html  css  js  c++  java
  • spring-AMQP-RabbitMQ

    1.spring整合rabbitMQ配置文件   rabbitmq-context.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-1.4.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

    <!-- 定义RabbitMQ的连接工厂 -->
    <rabbit:connection-factory id="connectionFactory"
    host="127.0.0.1" port="5672" username="xuejianjun" password="xuejianjun"
    virtual-host="/xuejianjun" />

    <!-- 定义Rabbit模板,指定连接工厂以及定义exchange -->
    <!-- 定义模板,将消息发送到交换机 -->
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="fanoutExchange" />
    <!-- 将消息发送到队列,需要做出如下修改: -->
    <!-- <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" queue="对列名" /> -->
    <!-- <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
    exchange="fanoutExchange" routing-key="foo.bar" /> -->

    <!-- MQ的管理,包括队列、交换器等 -->
    <!--配置管理的目的,就是为了自动声明下列的队列,交换器等的 -->
    <rabbit:admin connection-factory="connectionFactory" />

    <!-- 定义队列,自动声明 -->
    <rabbit:queue name="myQueue" auto-declare="true"/>

    <!-- 定义交换器,自动声明, -->
    <!-- 交换机不存在就自动声明,存在就不声明 -->
    <rabbit:fanout-exchange name="fanoutExchange" auto-declare="true">
    <rabbit:bindings>
    <rabbit:binding queue="myQueue"/>
    </rabbit:bindings>
    </rabbit:fanout-exchange>

    <!-- <rabbit:topic-exchange name="myExchange">
    <rabbit:bindings>
    <rabbit:binding queue="myQueue" pattern="foo.*" />
    </rabbit:bindings>
    </rabbit:topic-exchange> -->

    <!-- 队列监听 -->
    <rabbit:listener-container connection-factory="connectionFactory">
    <rabbit:listener ref="foo" method="listen" queue-names="myQueue" />
    </rabbit:listener-container>

    <bean id="foo" class="cn.itcast.rabbitmq.spring.Foo" />

    </beans>

    2.测试类

     SpringMain.java

    ================================================================================================================================

    package cn.itcast.rabbitmq.spring;

    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(
    "classpath:spring/rabbitmq-context.xml");
    //RabbitMQ模板
    RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
    //发送消息
    template.convertAndSend("Hello, world!");
    Thread.sleep(1000);// 休眠1秒
    ctx.destroy(); //容器销毁
    }
    }

    =============================================================================================================================

    Foo.java     注意:Foo的含义,就是距离的意思,例如中文中有:张三,李四等 

    package cn.itcast.rabbitmq.spring;

    /**
    * 消费者
    * @author zhijun
    *
    */
    public class Foo {

    //具体执行业务的方法
    public void listen(String foo) {
    System.out.println("消费者: " + foo);
    }
    }

  • 相关阅读:
    异步、+回调机制、线程queue、线程Event、协程、单线程实现遇到IO切换
    GIL、进/线程池、同/异步、阻/非阻塞
    锁——死锁——单个锁锁死
    网络编程之多线程
    后台Response和异常和日志封装、跨域问题及解决、es6的箭头函数、xadmin后台管理
    pip换源、虚拟环境搭建、
    非对称加密和对称加密的区别
    JWT、多方式登录、django缓存
    自定制频率、自动生成接口文档、JWT、自定制auth认证类
    books系列表接口、表断关联、分页器、根据IP限制频率
  • 原文地址:https://www.cnblogs.com/curedfisher/p/11896773.html
Copyright © 2011-2022 走看看