ActiveMQ JBDC巨坑记录:java.sql.SQLException: Cannot create PoolableConnectionFactory (Communications link failure,Cannot create PoolableConnectionFactory (Communications link failure
1.解决ActiveMq与JDBC持久化巨坑、我用的mysql5.7,提示日志如下:
日志在:data/activemq.log
- JDBCPersistenceAdapter(org.apache.commons.dbcp2.BasicDataSource@18bcceb4)
- Could not get JDBC connection: Cannot create PoolableConnectionFactory (Communications link failure
- java.sql.SQLException: Cannot create PoolableConnectionFactory (Communications link failure
-
cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService#0' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.io.IOException: Cannot create PoolableConnectionFactory (Communications link failure
2.找了正正3小时,完美解决
在stackoverflow上找到,是因为没有配置SSL,连接:https://stackoverflow.com/questions/63988683/active-mq-able-to-connect-to-other-machine-mysql-but-not-local-machine-mysql
<bean id="mysql-ds" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/activemq?relaxAutoCommit=true&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
3.附加说一句:网上什么电脑主机有下划线,DBCP2和DBCP1我都试过了,快怀疑人生了........幸好找到解决方法了
4.ActiveMq和jdbc配置详细步骤:
-
1.在mq中加入mysql驱动,,我是5.7加入的mysql-connector-java-5.1.38.jar也能用。
-
2.配置mysql持久注入连接池,注意 在 </broker>下 和 <import resource="jetty.xml"/>上面配置
<!-- mysql 持久配置 --> <!--创建mysql数据源,官网示例使用的dbcp2,若要使用其他数据源需要拷贝jar包到lib下 --> <!-- bean标签需要写在</broker>之后,<import /> 之前 --> <bean id="mysql-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/activemq?relaxAutoCommit=true&useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="poolPreparedStatements" value="true"/> </bean>
- 3.配置msyql持久化:
<!-- <persistenceAdapter> <kahaDB directory="${activemq.data}/kahadb"/> </persistenceAdapter>-->
<!--这行代码需要在mysql里面手动建库activemq, 标签createTableOnStartup他会在mq启动时候自己创建三张表,主要为了创建表 -->
<persistenceAdapter> <jdbcPersistenceAdapter dataSource="#mysql-ds" createTablesOnStartup="true" /> </persistenceAdapter>
- 4.注释第三部,加入高性能的持久方式
<!--<persistenceAdapter> <jdbcPersistenceAdapter dataSource="#mysql-ds" createTablesOnStartup="true"/> </persistenceAdapter> --> <!--消息存储持久化JDBC message store with activemq journal--> <persistenceFactory> <journalPersistenceAdapterFactory journalLogFiles="4" journalLogFileSize="32768" useJournal="true" useQuickJournal="true" dataSource="#mysql-ds" dataDirectory="activemq-data"/> </persistenceFactory>
- 5.可以配置客户端nio高性能方式调用
<transportConnector name="auto+nio" uri="auto+nio://0.0.0.0:61608?maximumConnections=1000&wireFormat.maxFrameSize=104857600&org.apache.activemq.transport.nio.SelectorManager.corelPoolSize=20&org.apache.activemq.transport.nio.SelectorManager.maximumPoolSize=50"/>
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:55672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="auto+nio" uri="auto+nio://0.0.0.0:61608?maximumConnections=1000&wireFormat.maxFrameSize=104857600&
org.apache.activemq.transport.nio.SelectorManager.corelPoolSize=20&org.apache.activemq.transport.nio.SelectorManager.maximumPoolSize=50"/>
</transportConnectors>