activemq 지연 메 시 지 를 보 내 는 도구 클래스

11967 단어 도구 클래스자바
1.demo
package com.util;

import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.*;
import java.io.Serializable;

@Component
public class MqProducerUtil {
     
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     *     
     *
     * @param name           
     * @param data           
     * @param delayTime      (   )
     */
    public <T extends Serializable> void delaySend(String name, T data, int delayTime) {
     
        delaySend(name, data, delayTime, 1, 1);
    }

    /**
     *     
     *
     * @param name            
     * @param data            
     * @param delayTime       (   )
     * @param repeatTime        (   )
     * @param repeatNum        
     */
    public <T extends Serializable> void delaySend(String name, T data, int delayTime, int repeatTime, int repeatNum) {
     
        Destination destination = new ActiveMQQueue(name);
        //       
        ConnectionFactory connectionFactory = jmsMessagingTemplate.getConnectionFactory();
        try (Connection connection = connectionFactory.createConnection();
             //   session,true    ,false    
             Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
             //         
             MessageProducer producer = session.createProducer(destination);
        ) {
     
            connection.start();
            producer.setDeliveryMode(JmsProperties.DeliveryMode.PERSISTENT.getValue());
            ObjectMessage message = session.createObjectMessage(data);
            //      
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delayTime * 1000L);//      
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, repeatTime * 1000L);//        (   ,      )
            message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeatNum);//      (   ,      )
            //     
            producer.send(message);
            session.commit();
            logger.info("name:[{}],data:[{}]", name, data);
        } catch (Exception e) {
     
            e.printStackTrace();
        }
    }
}

2. 글 참조 링크 a.https://blog.csdn.net/wanghao112956/article/details/103042773 b.https://www.cnblogs.com/yjmyzz/p/active-mq-delay-and-schedule-message-delivery.html

좋은 웹페이지 즐겨찾기