RabbitMQ入门-10-1(整合spring-发送同步消息)

时间:2024-04-08 17:58:31

写在前面的话

系列教程都是从网络上收集和本人的理解所编辑而成,仅供广大爱好者学习所用,请尊重本人的劳动成果。欢迎评论指正和转帖!(请保留连接谢谢!)


一、项目结构

RabbitMQ入门-10-1(整合spring-发送同步消息)


二、POM.XML

RabbitMQ入门-10-1(整合spring-发送同步消息)

RabbitMQ入门-10-1(整合spring-发送同步消息)

RabbitMQ入门-10-1(整合spring-发送同步消息)


三、Producer.java

package com.rabbitmq;

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

//生产者
public class Producer {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
        amqpTemplate.convertAndSend("test spring sync");
    }
}

四、Consumer.java

package com.rabbitmq;

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

//消费者
public class Consumer {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
        System.out.println("Received: " + amqpTemplate.receiveAndConvert());
    }
}

五、ApplicationContext.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd"
       default-lazy-init="false">

    <description>Spring公共配置 </description>
    <context:component-scan base-package="com.rabbitmq" />
    <aop:aspectj-autoproxy />
    <import resource="applicationContext-rabbitmq-sync.xml"/>
</beans>

六、applicationContext-rabbitmq-async.xml

RabbitMQ入门-10-1(整合spring-发送同步消息)


注意:请在网站上自行创建队列,名称为:spring. queue.async




写在后面的话

The worldi is like mirror:Frown at itand it frowns at you;smile,and it smiles too.

世界犹如一面镜子:朝它皱眉它就朝你皱眉,朝它微笑它就朝你微笑。