ssm整合rabbitmq

1. 引入依赖

    
      com.rabbitmq
      amqp-client
      3.6.0
    
    
      org.springframework.amqp
      spring-rabbit
      1.6.5.RELEASE
    
  

2.编写rabbitmq.properties

mq.host=192.168.240.135(这里改成你)
mq.username=guest
mq.password=guest
mq.port=5672

3.编写spring-rabbitmq.xml




    
    
    
    
    
    
        
            
        
    

    
    
        
    

4.模拟测试一下

  1. 发送端
@Service
public class UserService implements IUserService {
    @Autowired
    IUserDao userDao;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public void addUser(User user) {
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTING_KEY_NAME,user);
//        userDao.addUser(user);
    }
}
  1. 接收端
public class EmailSender implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println("接收到了message----");
        User user = ((User) new Jackson2JsonMessageConverter().fromMessage(message));
        System.out.println(user);
    }
}
  1. 补充一下MailConstants
public class MailConstants {
    public static final Integer DELIVERING = 0;//消息投递中
    public static final Integer SUCCESS = 1;//消息投递成功
    public static final Integer FAILURE = 2;//消息投递失败
    public static final Integer MAX_TRY_COUNT = 3;//最大重试次数
    public static final Integer MSG_TIMEOUT = 1;//消息超时时间
    public static final String MAIL_QUEUE_NAME = "teskinfly.xia.email.queue";
    public static final String MAIL_EXCHANGE_NAME = "teskinfly.xia.exch";
    public static final String MAIL_ROUTING_KEY_NAME = "teskinfly.user.email.key";
}

5.显示效果

image.png

参考博客

https://my.oschina.net/u/3387320/blog/2876559

邮件真正实现参考

https://blog.csdn.net/Mr__Viking/article/details/81090046

你可能感兴趣的:(ssm整合rabbitmq)