SSM集成rabbitMQ

SSM集成rabbitMQ

  • 项目总结构:
  • 1.SSM的pom.xml文件引入依赖:
  • 2.rabbitMQ.xml配置文件:
  • 3.utils包下生产者和消费者:
    • 3.1.生产者:
    • 3.2.消费者:
  • 4.controller的使用
  • 5.日志打印效果:
  • 6.集成过程中出现过的bug:

项目总结构:

SSM集成rabbitMQ_第1张图片
SSM集成rabbitMQ_第2张图片

1.SSM的pom.xml文件引入依赖:

<!--rabitmq-->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.3.5.RELEASE</version>
        </dependency>

2.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:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/rabbit
     http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd"><!--1.2之前有很多不支持-->
    <!--配置connection-factory,指定连接rabbit server参数,一般固定 -->
    <rabbit:connection-factory id="rabbitConnectionFactory" username="guest" password="guest" host="127.0.0.1" port="5672" />


    <!--定义rabbit template用于数据的接收和发送
        定义exchange
    -->
    <rabbit:template id="amqpTemplate"  connection-factory="rabbitConnectionFactory"
                     exchange="exchangeTest" />

    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
    <rabbit:admin connection-factory="rabbitConnectionFactory" />

    <!--定义queue -->
    <rabbit:queue name="queueTest" durable="true" auto-delete="false" exclusive="false" />

    <!-- 定义direct exchange,绑定queueTest -->
    <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false">
        <rabbit:bindings>//key="queueTestKey"的值要与生产者的amqpTemplate.convertAndSend("queueTestKey",message);中双引号内容一致
            <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!-- 消息接收者,根据个人项目构建的不同而异,一定要改 -->
    <bean id="messageReceiver" class="edu.xlh.ssm.utils.MessageProducer2"></bean>

    <!-- queue litener  观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象-->
    <rabbit:listener-container connection-factory="rabbitConnectionFactory">
        <rabbit:listener queues="queueTest" ref="messageReceiver"/>
    </rabbit:listener-container>

</beans>

3.utils包下生产者和消费者:

3.1.生产者:

package edu.xlh.ssm.utils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class MessageProducer2 {
        @Autowired
        private AmqpTemplate  amqpTemplate;
        public void sendMessage(Object message){
                amqpTemplate.convertAndSend("queueTestKey",message);
        }
}

3.2.消费者:

package edu.xlh.ssm.utils;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;


public class MessageConsumer implements MessageListener {
    private Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
    @Override
    public void onMessage(Message message) {
        try {
            String messages = new String(message.getBody(), "UTF-8");
            //String[] str=messages.split(",");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        logger.info("consumer receive message------->:{}", message);
    }
}

4.controller的使用

@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
    private IUserService userService;
    @Autowired
    private RedisTemplate<String,UserInfo> redisTemplate;
    @Autowired
    private RedisTemplate<String,List<UserInfo>>  redisTemplate1;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private MessageProducer2 messageProducer2;
      @RequestMapping("/findById")
    public ModelAndView findById(@RequestParam(name = "id",required = true) String userId){
        ModelAndView mv = new ModelAndView();
        BoundValueOperations<String,UserInfo> boundValueOperations =
                redisTemplate.boundValueOps(userId);
        UserInfo userInfo = boundValueOperations.get();
        if(userInfo!=null){
            //amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", "请求获取id为"+userId+"的用户成功");
            messageProducer2.sendMessage("请求获取id为"+userId+"的用户成功");//只有这一句是使用的
            System.out.println("从redis获取key"+userInfo.toString());
            mv.addObject("user",userInfo);
        }else{
            userInfo = userService.findById(userId);
            System.out.println("从数据库获取key"+userInfo.toString());
            boundValueOperations.set(userInfo);
            mv.addObject("user",userInfo);
        }

        mv.addObject("user",userInfo);
        mv.setViewName("user-show");
        return mv;
    }
}

5.日志打印效果:

在这里插入图片描述

6.集成过程中出现过的bug:

原rabbitMQ.xml配置文件中connection-factory="connectionFactory"出现红色波浪线,,如图所示,分析可知在同目录下的redis的配置文件也有一模一样的connection-factory值,c+鼠标点进去后进入第二张图所示页面,验证了猜想,因此该改为:connection-factory="rabbitConnectionFactory"总结:同目录下的配置文件不可出现同connection-factory属性且同值
SSM集成rabbitMQ_第3张图片

SSM集成rabbitMQ_第4张图片

你可能感兴趣的:(#,ssm,rabbitmq,spring,boot,ssm集成rabbitMQ,bug)