spring boot整合ArtemisMQ进行手动消息确认
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSConstants;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import javax.jms.JMSException;
import javax.jms.Session;
@Configuration
public class ArtemisConfig {
@Bean(name = "artemisConnectionFactory")
public ActiveMQConnectionFactory artemisConnectionFactory() throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.setBrokerURL("tcp://127.0.0.1:61616");
factory.setUser("admin");
factory.setPassword("xxx");
return factory;
}
/**
* 生产者配置
* @param connectionFactory
* @return
*/
@Bean(name = "artemisJmsTemplate")
public JmsTemplate activeMQJmsTemplate(@Qualifier("artemisConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.setPubSubDomain(false);
return jmsTemplate;
}
/**
* 消费者配置
* @param connectionFactory
* @return
*/
@Bean(name = "artemisListenerContainerFactory")
public DefaultJmsListenerContainerFactory artemisListenerContainerFactory(@Qualifier("artemisConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// 设置手动确认机制
factory.setSessionTransacted(false);
factory.setSessionAcknowledgeMode(ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE); // 每个消息监听时都需要手动确认
factory.setConnectionFactory(connectionFactory);
// 设置为true以启用发布/订阅模式
factory.setPubSubDomain(false);
return factory;
}
}