上一篇讲到在整合activeMQ如何将queue和topic两种模式共存: spring boot2.0整合activeMQ,实现p2p和topic两者消息模式兼容共存
然后还遗留了一个问题,有读者评论因为配置文件里一句启用连接池导致项目启动报错,只要把那句配置注释掉或者改为false就可以了,这实际上是治标不治本,因毕竟流量大的时候不启用连接池效率会很低:

# 启用连接池
spring.activemq.pool.enabled=true

诚然,如果不想使用连接池,那么快速的办法的确是把这个配置直接干掉,或者改为false,其是默认就是false,但是我们来看一看启用连接池后启动项目提示JmsMessagingTemplate无法注入真正的原因是什么

开启连接池,启动项目会报错,提示JmsMessagingTemplate无法注入、

目前使用springboot最新版本是2.1+,我先直接告诉你解决方法,如果要启用连接池:

  • 使用springboot2.0+及以下版本时候,maven配置依赖是:
<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-pool</artifactId>
</dependency>
  • 使用springboot2.1+时候,maven配置依赖是:
<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>

请按照自己使用的springboot版本更换对应的连接池依赖就可以解决问题。但为什么一样呢?我们来看看springboot的自动配置的部分源码:

  • 使用springboot2.0+及以下版本时候:
@Configuration
@ConditionalOnMissingBean(ConnectionFactory.class)
class ActiveMQConnectionFactoryConfiguration {
    @Bean
    @ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "false", matchIfMissing = true)
    public ActiveMQConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
            ObjectProvider<List<ActiveMQConnectionFactoryCustomizer>> factoryCustomizers) {
        return new ActiveMQConnectionFactoryFactory(properties,
                factoryCustomizers.getIfAvailable())
                        .createConnectionFactory(ActiveMQConnectionFactory.class);
    @Configuration
    @ConditionalOnClass(PooledConnectionFactory.class)
    static class PooledConnectionFactoryConfiguration {
        @Bean(destroyMethod = "stop")
        @ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true", matchIfMissing = false)
        public PooledConnectionFactory pooledJmsConnectionFactory(
                ActiveMQProperties properties,
                ObjectProvider<List<ActiveMQConnectionFactoryCustomizer>> factoryCustomizers) {
            PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(
                    new ActiveMQConnectionFactoryFactory(properties,
                            factoryCustomizers.getIfAvailable()).createConnectionFactory(
                                    ActiveMQConnectionFactory.class));
            ActiveMQProperties.Pool pool = properties.getPool();
            pooledConnectionFactory.setBlockIfSessionPoolIsFull(pool.isBlockIfFull());
            if (pool.getBlockIfFullTimeout() != null) {
                pooledConnectionFactory.setBlockIfSessionPoolIsFullTimeout(
                        pool.getBlockIfFullTimeout().toMillis());
            pooledConnectionFactory
                    .setCreateConnectionOnStartup(pool.isCreateConnectionOnStartup());
            if (pool.getExpiryTimeout() != null) {
                pooledConnectionFactory
                        .setExpiryTimeout(pool.getExpiryTimeout().toMillis());
            if (pool.getIdleTimeout() != null) {
                pooledConnectionFactory
                        .setIdleTimeout((int) pool.getIdleTimeout().toMillis());
            pooledConnectionFactory.setMaxConnections(pool.getMaxConnections());
            pooledConnectionFactory.setMaximumActiveSessionPerConnection(
                    pool.getMaximumActiveSessionPerConnection());
            pooledConnectionFactory
                    .setReconnectOnException(pool.isReconnectOnException());
            if (pool.getTimeBetweenExpirationCheck() != null) {
                pooledConnectionFactory.setTimeBetweenExpirationCheckMillis(
                        pool.getTimeBetweenExpirationCheck().toMillis());
            pooledConnectionFactory
                    .setUseAnonymousProducers(pool.isUseAnonymousProducers());
            return pooledConnectionFactory;
  • 使用springboot2.1+时候,maven配置依赖是:
@Configuration
@ConditionalOnMissingBean(ConnectionFactory.class)
class ActiveMQConnectionFactoryConfiguration {
    @Configuration
    @ConditionalOnClass(CachingConnectionFactory.class)
    @ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "false", matchIfMissing = true)
    static class SimpleConnectionFactoryConfiguration {
        private final JmsProperties jmsProperties;
        private final ActiveMQProperties properties;
        private final List<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers;
        SimpleConnectionFactoryConfiguration(JmsProperties jmsProperties,
                ActiveMQProperties properties,
                ObjectProvider<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers) {
            this.jmsProperties = jmsProperties;
            this.properties = properties;
            this.connectionFactoryCustomizers = connectionFactoryCustomizers
                    .orderedStream().collect(Collectors.toList());
        @Bean
        @ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true", matchIfMissing = true)
        public CachingConnectionFactory cachingJmsConnectionFactory() {
            JmsProperties.Cache cacheProperties = this.jmsProperties.getCache();
            CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
                    createConnectionFactory());
            connectionFactory.setCacheConsumers(cacheProperties.isConsumers());
            connectionFactory.setCacheProducers(cacheProperties.isProducers());
            connectionFactory.setSessionCacheSize(cacheProperties.getSessionCacheSize());
            return connectionFactory;
        @Bean
        @ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
        public ActiveMQConnectionFactory jmsConnectionFactory() {
            return createConnectionFactory();
        private ActiveMQConnectionFactory createConnectionFactory() {
            return new ActiveMQConnectionFactoryFactory(this.properties,
                    this.connectionFactoryCustomizers)
                            .createConnectionFactory(ActiveMQConnectionFactory.class);
    @Configuration
    @ConditionalOnClass({ JmsPoolConnectionFactory.class, PooledObject.class })
    static class PooledConnectionFactoryConfiguration {
        @Bean(destroyMethod = "stop")
        @ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true", matchIfMissing = false)
        public JmsPoolConnectionFactory pooledJmsConnectionFactory(
                ActiveMQProperties properties,
                ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(
                    properties,
                    factoryCustomizers.orderedStream().collect(Collectors.toList()))
                            .createConnectionFactory(ActiveMQConnectionFactory.class);
            return new JmsPoolConnectionFactoryFactory(properties.getPool())
                    .createPooledConnectionFactory(connectionFactory);

很明显我们看的出来,2.1和2.0以下版本使用连接池时候的连接池对象不一样:

  • 2.0+以下版本使用的是PooledConnectionFactory,它存在于org.apache.activemq.pool.PooledConnectionFactory
  • 2.1+版本使用的是JmsPoolConnectionFactory,它存在于org.messaginghub.pooled.jms.JmsPoolConnectionFactory

由此我们就明白了为什么更换连接池依赖就ok,因为JmsMessagingTemplate本身依赖连接工厂,启用连接池后就会依赖连接池工厂,因为版本的原因,如果没有正确的引入连接池依赖,那么必定会出现文章一开头说的,项目启动报错:JmsMessagingTemplate无法注入

最后在提醒一下注意源码中的注解:

@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true", matchIfMissing = false)

当前缀为:spring.activemq.pool 的配置的属性:enabled的值为:true 的时候就会实例化bean,如果没有正确引入连接池依赖就会报错。

上一篇讲到在整合activeMQ如何将queue和topic两种模式共存:spring boot2.0整合activeMQ,实现p2p和topic两者消息模式兼容共存然后还遗留了一个问题,有读者评论因为配置文件里一句启用连接池导致项目启动报错,只要把那句配置注释掉或者改为false就可以了,这实际上是治标不治本,因毕竟流量大的时候不启用连接池效率会很低:# 启用连接池spring.activ...
&lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-activemq&lt;/artifactId&gt; &lt;version&gt;2.0.0.BUILD-SNAPSHO...
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependen import lombok.extern.slf4j.Slf4j; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.RedeliveryPolicy; import o <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> 创建WebSocketStompConfig.java package com.by4cloud.platf
记录下修复ActiveMQ Artemis使用jms的时候阻塞的问题。 我在我的本地机器上安装了Artemis,根据说明创建了一个新的代理,并将其设置为Windows服务. Windows服务启动和停止就好了.我没有对broker.xml文件进行任何更改. 对于我的第一次测试,我正在尝试从独立的Java程序执行JMS Queue生成/使用.我正在使用“使用JMS”部分中的Artemis用户手册中的代码(不使用JNDI): TransportConfiguration transportConfigurati
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 在yml文件中配置active
配置Spring Boot整合ActiveMQ,你可以按照以下步骤进行操作: 1. 添加ActiveMQ依赖: 在你的项目中的pom.xml文件中,添加以下依赖项: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 2. 配置ActiveMQ连接信息: 在你的application.properties或application.yml文件中,添加以下配置: spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin 这里,你需要设置ActiveMQ的通信地址,以及账户名和密码。 3. 配置队列或主题模式: 默认情况下,ActiveMQ提供的是队列模式。如果你想使用主题模式,可以添加以下配置: spring.jms.pub-sub-domain=true 4. 启动ActiveMQ服务器: 你需要启动ActiveMQ服务器来进行消息的发送和接收。可以通过在浏览器中访问http://localhost:8161,使用管理员账户名和密码(admin/admin)登录ActiveMQ管理界面来启动服务器。 这样,你就可以在Spring Boot项目中成功配置和使用ActiveMQ了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java springboot整合activemq工程](https://download.csdn.net/download/weixin_47315082/88136003)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Springboot整合ActiveMQ](https://blog.csdn.net/weixin_45698637/article/details/123443728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [《SpringBoot篇》25.SpringBoot整合ActiveMQ](https://blog.csdn.net/weixin_47343544/article/details/128164353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
抚琴听弦: 我觉得第1种添加@DateTimeFormat注解的办法最好。先在配置文件中配置全局转换格式spring.jackson.time-zone:=GMT+8,spring.jackson.date-format=yyyy-MM-dd HH:mm:ss。再在需要转换的日期字段添加@DateTimeFormat注解,不需要设置转换格式,如果需要设置其他转换格式就设置pattern的值@DateTimeFormat(pattern="yyyy-MM-dd")。我觉得挺方便的。楼主的第3种办法有一个缺点,就是假如我日期是2012-09-24 10:40:39这种格式,我想转为2021-09-24这种格式,就不行,它只会给你转为yyyy-MM-dd hh:mm:ss这种格式。 springboot整合rabbitmq实现延时队列之rabbitmq_delayed_message_exchange插件方式 CaptainKs: