springmvc 整合rabbitmq 配置

1.springmvc配置



    rabbitmq 连接服务配置
    
    
    
    
    
    
    

    
    
    
    
        
        
        
        
        
        
        
    

2.监听类

@Slf4j
@Component
public class ServerMessageListener implements ChannelAwareMessageListener {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onMessage(Message message, Channel channel) {
        String msg = new String(message.getBody());
        log.info("消费{}", msg);
        try {
            //消息的标识,false只确认当前一个消息收到,true确认所有consumer获得的消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.生产类

@Resource
private AmqpTemplate amqpTemplate;
private void sendDataToQueue(String routingKey, Object object) {
    logger.info("生产消息:{}", object);
    amqpTemplate.convertAndSend(routingKey, object);
}

4.maven依赖:


    com.rabbitmq
    amqp-client
    5.4.3


    org.springframework.amqp
    spring-rabbit
    2.1.4.RELEASE

 

 

你可能感兴趣的:(springmvc 整合rabbitmq 配置)