问题一:
activemq:
pool:
enabled: true
开启连接池,启动项目会报错,提示JmsMessagingTemplate无法注入,只要加上依赖即可。
<!--springboot2.X 要开启activemq pool必须引入此连接池依赖-->
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
因为JmsMessagingTemplate本身依赖连接工厂,启用连接池后就会依赖连接池工厂,因为版本的原因,如果没有正确的引入连接池依赖,那么项目启动肯定会报错:JmsMessagingTemplate无法注入。
因为你不引入,IDEA会标红显示无这个类(JmsPoolConnectionFactory)
package org.springframework.boot.autoconfigure.jms.artemis;
import javax.jms.ConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.commons.pool2.PooledObject;
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(ConnectionFactory.class)
class ArtemisConnectionFactoryConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ JmsPoolConnectionFactory.class, PooledObject.class })
static class PooledConnectionFactoryConfiguration {
@Bean(destroyMethod = "stop")
@ConditionalOnProperty(prefix = "spring.artemis.pool", name = "enabled", havingValue = "true",
matchIfMissing = false)
JmsPoolConnectionFactory pooledJmsConnectionFactory(ListableBeanFactory beanFactory,
ArtemisProperties properties) {
ActiveMQConnectionFactory connectionFactory = new ArtemisConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(ActiveMQConnectionFactory.class);
return new JmsPoolConnectionFactoryFactory(properties.getPool())
.createPooledConnectionFactory(connectionFactory);
}
}
}
问题二:
控制台一直打印,不断重复,会很浪费空间,所以取消掉此类的打印日志
[20:09:59:585] [DEBUG] - org.apache.activemq.ActiveMQSession.commit(ActiveMQSession.java:580) - ID:DESKTOP-98F4QUU-56334-1582027509219-1:1:1 Transaction Commit :null
[20:10:00:594] [DEBUG] - org.apache.activemq.ActiveMQSession.commit(ActiveMQSession.java:580) - ID:DESKTOP-98F4QUU-56334-1582027509219-1:1:1 Transaction Commit :null
解决办法一:治标不治本
使用log4j2的话,使用以下配置就行,其他无关问题配置省略空间,就不写了
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="ERROR" monitorInterval="30">
<loggers>
<!--过滤掉spring和mybatis的一些无用的DEBUG信息和打印事务为空的日志-->
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.mybatis" level="INFO"></logger>
<logger name="org.apache.activemq.ActiveMQSession" level="ERROR"></logger>
</loggers>
</configuration>
解决办法二:手动确认消息
1.添加以下配置,参考https://blog.csdn.net/p_programmer/article/details/88384138
@Bean
public DefaultJmsListenerContainerFactory myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
factory.setSessionTransacted(false);
factory.setSessionAcknowledgeMode(4);
return factory;
}
2.在消费者处,手动确认,以及指定工厂,这里的案例可以看·我写的SpringBoot2.X 使用ActiveMQ实战项目之实现登陆日志保存
//指定自定义的工厂
@JmsListener(destination = "login-info",containerFactory = "myFactory")
public void handle(Message message) {
if (message instanceof ActiveMQObjectMessage) {
ActiveMQObjectMessage activeMQObjectMessage = (ActiveMQObjectMessage) message;
try {
Loginfo loginfo = (Loginfo) activeMQObjectMessage.getObject();
loginfoService.save(loginfo);
//手动确认消息
activeMQObjectMessage.acknowledge();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
来源:oschina
链接:https://my.oschina.net/machell/blog/4298781