RabbitMQ - 项目实战

rabbitmq.properties 

#本地环境配置项-开始######################
#rabbitmq.host=127.0.0.1
#rabbitmq.port=5672
#rabbitmq.username=baseup
#rabbitmq.password=wukong@123
#rabbitmq.vhost=/baseup
#本地环境配置项-结束#######################


#正式环境配置项-开始#######################
rabbitmq.host=139.159.138.142
rabbitmq.port=5672
rabbitmq.username=baseup
rabbitmq.password=whkj@2019
rabbitmq.vhost=/baseup
#正式环境配置项-结束#######################

baseup-rabbitmq.xml





    
    

    
    

    
    
    

    
    
    

    
    
    

    
    

    
    
        
            
            
        
    
    
    
        
            
            
        
    


    
    
        
        
    

    
        
        
    


    
    
    

    
    

User01.java

package com.tyh.baseup.web.server.mq.consumer;

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

/**
 * 消费者1
 * 交换机为- 》direct:  代表 "路由模式"  《可选择性接收消息, 指定路由Key》
 */
public class User01 implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(User01.class);

    @Override
    public void onMessage(Message message) {

        logger.info("消费者User01->>开始######################################");
        try {
            logger.info("消费者User01->>Body:"  + message.getBody());
            logger.info("消费者User01->>Body.转换:"  +  new String(message.getBody(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        logger.info("消费者User01->>toString:"  + message.toString());
        logger.info("消费者User01->>MessageProperties:"  + message.getMessageProperties());
        logger.info("消费者User01->>MessageProperties.AppId:"  + message.getMessageProperties().getAppId());
        logger.info("消费者User01->>MessageProperties.ClusterId:"  + message.getMessageProperties().getClusterId());
        logger.info("消费者User01->>MessageProperties.ConsumerQueue:"  + message.getMessageProperties().getConsumerQueue());
        logger.info("消费者User01->>MessageProperties.ConsumerTag:"  + message.getMessageProperties().getConsumerTag());
        logger.info("消费者User01->>MessageProperties.ContentEncoding:"  + message.getMessageProperties().getContentEncoding());
        logger.info("消费者User01->>MessageProperties.ContentType:"  + message.getMessageProperties().getContentType());
        logger.info("消费者User01->>MessageProperties.ContentLength:"  + message.getMessageProperties().getContentLength());
        logger.info("消费者User01->>MessageProperties.CorrelationIdString:"  + message.getMessageProperties().getCorrelationIdString());
        logger.info("消费者User01->>MessageProperties.Delay:"  + message.getMessageProperties().getDelay());
        logger.info("消费者User01->>MessageProperties.DeliveryMode:"  + message.getMessageProperties().getDeliveryMode());
        logger.info("消费者User01->>MessageProperties.DeliveryTag:"  + message.getMessageProperties().getDeliveryTag());
        logger.info("消费者User01->>MessageProperties.Expiration:"  + message.getMessageProperties().getExpiration());
        logger.info("消费者User01->>MessageProperties.Headers:"  + message.getMessageProperties().getHeaders());
        logger.info("消费者User01->>MessageProperties.InferredArgumentType:"  + message.getMessageProperties().getInferredArgumentType());
        logger.info("消费者User01->>MessageProperties.ReplyTo:"  + message.getMessageProperties().getReplyTo());
        logger.info("消费者User01->>MessageProperties.ReceivedExchange:"  + message.getMessageProperties().getReceivedExchange());
        logger.info("消费者User01->>MessageProperties.ReceivedRoutingKey:"  + message.getMessageProperties().getReceivedRoutingKey());
        logger.info("消费者User01->>MessageProperties.ReceivedUserId:"  + message.getMessageProperties().getReceivedUserId());
        logger.info("消费者User01->>MessageProperties.ReceivedDelay:"  + message.getMessageProperties().getReceivedDelay());
        logger.info("消费者User01->>MessageProperties.ReceivedDeliveryMode:"  + message.getMessageProperties().getReceivedDeliveryMode());
        logger.info("消费者User01->>MessageProperties.Redelivered:"  + message.getMessageProperties().getRedelivered());
        logger.info("消费者User01->>MessageProperties.ReplyToAddress:"  + message.getMessageProperties().getReplyToAddress());
        logger.info("消费者User01->>MessageProperties.Type:"  + message.getMessageProperties().getType());
        logger.info("消费者User01->>MessageProperties.TargetBean:"  + message.getMessageProperties().getTargetBean());
        logger.info("消费者User01->>MessageProperties.TargetMethod:"  + message.getMessageProperties().getTargetMethod());
        logger.info("消费者User01->>MessageProperties.Timestamp:"  + message.getMessageProperties().getTimestamp());
        logger.info("消费者User01->>MessageProperties.UserId:"  + message.getMessageProperties().getUserId());
        logger.info("消费者User01->>MessageProperties.MessageId:"  + message.getMessageProperties().getMessageId());
        logger.info("消费者User01->>结束######################################");

    }
}

User02.java

package com.tyh.baseup.web.server.mq.consumer;

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

/**
 * 消费者2
 * 交换机为- 》direct:  代表 "路由模式"  《可选择性接收消息, 指定路由Key》
 */
public class User02 implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(User02.class);

    @Override
    public void onMessage(Message message) {
        logger.info("消费者User02->>开始######################################");
        try {
            logger.info("消费者User02->>Body:"  + message.getBody());
            logger.info("消费者User02->>Body.转换:"  +  new String(message.getBody(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        logger.info("消费者User02->>toString:"  + message.toString());
        logger.info("消费者User02->>MessageProperties:"  + message.getMessageProperties());
        logger.info("消费者User02->>MessageProperties.AppId:"  + message.getMessageProperties().getAppId());
        logger.info("消费者User02->>MessageProperties.ClusterId:"  + message.getMessageProperties().getClusterId());
        logger.info("消费者User02->>MessageProperties.ConsumerQueue:"  + message.getMessageProperties().getConsumerQueue());
        logger.info("消费者User02->>MessageProperties.ConsumerTag:"  + message.getMessageProperties().getConsumerTag());
        logger.info("消费者User02->>MessageProperties.ContentEncoding:"  + message.getMessageProperties().getContentEncoding());
        logger.info("消费者User02->>MessageProperties.ContentType:"  + message.getMessageProperties().getContentType());
        logger.info("消费者User02->>MessageProperties.ContentLength:"  + message.getMessageProperties().getContentLength());
        logger.info("消费者User02->>MessageProperties.CorrelationIdString:"  + message.getMessageProperties().getCorrelationIdString());
        logger.info("消费者User02->>MessageProperties.Delay:"  + message.getMessageProperties().getDelay());
        logger.info("消费者User02->>MessageProperties.DeliveryMode:"  + message.getMessageProperties().getDeliveryMode());
        logger.info("消费者User02->>MessageProperties.DeliveryTag:"  + message.getMessageProperties().getDeliveryTag());
        logger.info("消费者User02->>MessageProperties.Expiration:"  + message.getMessageProperties().getExpiration());
        logger.info("消费者User02->>MessageProperties.Headers:"  + message.getMessageProperties().getHeaders());
        logger.info("消费者User02->>MessageProperties.InferredArgumentType:"  + message.getMessageProperties().getInferredArgumentType());
        logger.info("消费者User02->>MessageProperties.ReplyTo:"  + message.getMessageProperties().getReplyTo());
        logger.info("消费者User02->>MessageProperties.ReceivedExchange:"  + message.getMessageProperties().getReceivedExchange());
        logger.info("消费者User02->>MessageProperties.ReceivedRoutingKey:"  + message.getMessageProperties().getReceivedRoutingKey());
        logger.info("消费者User02->>MessageProperties.ReceivedUserId:"  + message.getMessageProperties().getReceivedUserId());
        logger.info("消费者User02->>MessageProperties.ReceivedDelay:"  + message.getMessageProperties().getReceivedDelay());
        logger.info("消费者User02->>MessageProperties.ReceivedDeliveryMode:"  + message.getMessageProperties().getReceivedDeliveryMode());
        logger.info("消费者User02->>MessageProperties.Redelivered:"  + message.getMessageProperties().getRedelivered());
        logger.info("消费者User02->>MessageProperties.ReplyToAddress:"  + message.getMessageProperties().getReplyToAddress());
        logger.info("消费者User02->>MessageProperties.Type:"  + message.getMessageProperties().getType());
        logger.info("消费者User02->>MessageProperties.TargetBean:"  + message.getMessageProperties().getTargetBean());
        logger.info("消费者User02->>MessageProperties.TargetMethod:"  + message.getMessageProperties().getTargetMethod());
        logger.info("消费者User02->>MessageProperties.Timestamp:"  + message.getMessageProperties().getTimestamp());
        logger.info("消费者User02->>MessageProperties.UserId:"  + message.getMessageProperties().getUserId());
        logger.info("消费者User02->>MessageProperties.MessageId:"  + message.getMessageProperties().getMessageId());
        logger.info("消费者User02->>结束######################################");
    }
}

Customer01.java

package com.tyh.baseup.web.server.mq.consumer;


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

/**
 * 消费者1
 * 交换机为- 》topic:  代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》
 */
public class Customer01 implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(Customer01.class);

    @Override
    public void onMessage(Message message) {

        logger.info("消费者Customer01->>开始######################################");
        try {
            logger.info("消费者Customer01->>Body:"  + message.getBody());
            logger.info("消费者Customer01->>Body.转换:"  +  new String(message.getBody(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        logger.info("消费者Customer01->>toString:"  + message.toString());
        logger.info("消费者Customer01->>MessageProperties:"  + message.getMessageProperties());
        logger.info("消费者Customer01->>MessageProperties.AppId:"  + message.getMessageProperties().getAppId());
        logger.info("消费者Customer01->>MessageProperties.ClusterId:"  + message.getMessageProperties().getClusterId());
        logger.info("消费者Customer01->>MessageProperties.ConsumerQueue:"  + message.getMessageProperties().getConsumerQueue());
        logger.info("消费者Customer01->>MessageProperties.ConsumerTag:"  + message.getMessageProperties().getConsumerTag());
        logger.info("消费者Customer01->>MessageProperties.ContentEncoding:"  + message.getMessageProperties().getContentEncoding());
        logger.info("消费者Customer01->>MessageProperties.ContentType:"  + message.getMessageProperties().getContentType());
        logger.info("消费者Customer01->>MessageProperties.ContentLength:"  + message.getMessageProperties().getContentLength());
        logger.info("消费者Customer01->>MessageProperties.CorrelationIdString:"  + message.getMessageProperties().getCorrelationIdString());
        logger.info("消费者Customer01->>MessageProperties.Delay:"  + message.getMessageProperties().getDelay());
        logger.info("消费者Customer01->>MessageProperties.DeliveryMode:"  + message.getMessageProperties().getDeliveryMode());
        logger.info("消费者Customer01->>MessageProperties.DeliveryTag:"  + message.getMessageProperties().getDeliveryTag());
        logger.info("消费者Customer01->>MessageProperties.Expiration:"  + message.getMessageProperties().getExpiration());
        logger.info("消费者Customer01->>MessageProperties.Headers:"  + message.getMessageProperties().getHeaders());
        logger.info("消费者Customer01->>MessageProperties.InferredArgumentType:"  + message.getMessageProperties().getInferredArgumentType());
        logger.info("消费者Customer01->>MessageProperties.ReplyTo:"  + message.getMessageProperties().getReplyTo());
        logger.info("消费者Customer01->>MessageProperties.ReceivedExchange:"  + message.getMessageProperties().getReceivedExchange());
        logger.info("消费者Customer01->>MessageProperties.ReceivedRoutingKey:"  + message.getMessageProperties().getReceivedRoutingKey());
        logger.info("消费者Customer01->>MessageProperties.ReceivedUserId:"  + message.getMessageProperties().getReceivedUserId());
        logger.info("消费者Customer01->>MessageProperties.ReceivedDelay:"  + message.getMessageProperties().getReceivedDelay());
        logger.info("消费者Customer01->>MessageProperties.ReceivedDeliveryMode:"  + message.getMessageProperties().getReceivedDeliveryMode());
        logger.info("消费者Customer01->>MessageProperties.Redelivered:"  + message.getMessageProperties().getRedelivered());
        logger.info("消费者Customer01->>MessageProperties.ReplyToAddress:"  + message.getMessageProperties().getReplyToAddress());
        logger.info("消费者Customer01->>MessageProperties.Type:"  + message.getMessageProperties().getType());
        logger.info("消费者Customer01->>MessageProperties.TargetBean:"  + message.getMessageProperties().getTargetBean());
        logger.info("消费者Customer01->>MessageProperties.TargetMethod:"  + message.getMessageProperties().getTargetMethod());
        logger.info("消费者Customer01->>MessageProperties.Timestamp:"  + message.getMessageProperties().getTimestamp());
        logger.info("消费者Customer01->>MessageProperties.UserId:"  + message.getMessageProperties().getUserId());
        logger.info("消费者Customer01->>MessageProperties.MessageId:"  + message.getMessageProperties().getMessageId());
        logger.info("消费者Customer01->>结束######################################");
    }
}

Customer02.java

package com.tyh.baseup.web.server.mq.consumer;

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

/**
 * 消费者2
 * 交换机为- 》topic:  代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》
 */
public class Customer02 implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(Customer02.class);


    @Override
    public void onMessage(Message message) {
        logger.info("消费者Customer02->>开始######################################");
        try {
            logger.info("消费者Customer02->>Body:"  + message.getBody());
            logger.info("消费者Customer02->>Body.转换:"  +  new String(message.getBody(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        logger.info("消费者Customer02->>toString:"  + message.toString());
        logger.info("消费者Customer02->>MessageProperties:"  + message.getMessageProperties());
        logger.info("消费者Customer02->>MessageProperties.AppId:"  + message.getMessageProperties().getAppId());
        logger.info("消费者Customer02->>MessageProperties.ClusterId:"  + message.getMessageProperties().getClusterId());
        logger.info("消费者Customer02->>MessageProperties.ConsumerQueue:"  + message.getMessageProperties().getConsumerQueue());
        logger.info("消费者Customer02->>MessageProperties.ConsumerTag:"  + message.getMessageProperties().getConsumerTag());
        logger.info("消费者Customer02->>MessageProperties.ContentEncoding:"  + message.getMessageProperties().getContentEncoding());
        logger.info("消费者Customer02->>MessageProperties.ContentType:"  + message.getMessageProperties().getContentType());
        logger.info("消费者Customer02->>MessageProperties.ContentLength:"  + message.getMessageProperties().getContentLength());
        logger.info("消费者Customer02->>MessageProperties.CorrelationIdString:"  + message.getMessageProperties().getCorrelationIdString());
        logger.info("消费者Customer02->>MessageProperties.Delay:"  + message.getMessageProperties().getDelay());
        logger.info("消费者Customer02->>MessageProperties.DeliveryMode:"  + message.getMessageProperties().getDeliveryMode());
        logger.info("消费者Customer02->>MessageProperties.DeliveryTag:"  + message.getMessageProperties().getDeliveryTag());
        logger.info("消费者Customer02->>MessageProperties.Expiration:"  + message.getMessageProperties().getExpiration());
        logger.info("消费者Customer02->>MessageProperties.Headers:"  + message.getMessageProperties().getHeaders());
        logger.info("消费者Customer02->>MessageProperties.InferredArgumentType:"  + message.getMessageProperties().getInferredArgumentType());
        logger.info("消费者Customer02->>MessageProperties.ReplyTo:"  + message.getMessageProperties().getReplyTo());
        logger.info("消费者Customer02->>MessageProperties.ReceivedExchange:"  + message.getMessageProperties().getReceivedExchange());
        logger.info("消费者Customer02->>MessageProperties.ReceivedRoutingKey:"  + message.getMessageProperties().getReceivedRoutingKey());
        logger.info("消费者Customer02->>MessageProperties.ReceivedUserId:"  + message.getMessageProperties().getReceivedUserId());
        logger.info("消费者Customer02->>MessageProperties.ReceivedDelay:"  + message.getMessageProperties().getReceivedDelay());
        logger.info("消费者Customer02->>MessageProperties.ReceivedDeliveryMode:"  + message.getMessageProperties().getReceivedDeliveryMode());
        logger.info("消费者Customer02->>MessageProperties.Redelivered:"  + message.getMessageProperties().getRedelivered());
        logger.info("消费者Customer02->>MessageProperties.ReplyToAddress:"  + message.getMessageProperties().getReplyToAddress());
        logger.info("消费者Customer02->>MessageProperties.Type:"  + message.getMessageProperties().getType());
        logger.info("消费者Customer02->>MessageProperties.TargetBean:"  + message.getMessageProperties().getTargetBean());
        logger.info("消费者Customer02->>MessageProperties.TargetMethod:"  + message.getMessageProperties().getTargetMethod());
        logger.info("消费者Customer02->>MessageProperties.Timestamp:"  + message.getMessageProperties().getTimestamp());
        logger.info("消费者Customer02->>MessageProperties.UserId:"  + message.getMessageProperties().getUserId());
        logger.info("消费者Customer02->>MessageProperties.MessageId:"  + message.getMessageProperties().getMessageId());
        logger.info("消费者Customer02->>结束######################################");
    }
}

 

 

 

你可能感兴趣的:(RabbitMQ)