前提:运行环境
ActiveMQ v5.16.5
系统:Windows 10;
.NET环境:VS2019,Sql Server 2019;
JDK:18.0.2.1(注意:此版本不会生成jre文件夹,可观看其他博主解决此问题)
1、首先修改 apache-activemq-5.16.5/conf/activemq.xml 文件,详细修改如下:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:${activemq.conf}/credentials.properties</value>
</property>
</bean>
<bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"
lazy-init="false" scope="singleton"
init-method="start" destroy-method="stop">
</bean>
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=">" >
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<persistenceAdapter>
<!--<kahaDB directory="${activemq.data}/kahadb"/>-->
<!--消息持久化设置-->
<jdbcPersistenceAdapter useDatabaseLock="false" dataSource="#mssql_ds" createTablesOnStartup="true"/>
</persistenceAdapter>
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage percentOfJvmHeap="70" />
</memoryUsage>
<storeUsage>
<storeUsage limit="100 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="50 gb"/>
</tempUsage>
</systemUsage>
</systemUsage>
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?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"/>
</transportConnectors>
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>
</broker>
<!--消息持久化到 mssql 数据库配置-->
<bean id="mssql_ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DataBaseName=databasename;username=username;encrypt=true; trustServerCertificate=true;password=password"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
<import resource="jetty.xml"/>
</beans>
具体修改如上图所示:
2、apache-activemq-5.16.5/lib 文件夹内添加:mssql-jdbc-11.2.0.jre18.jar(下载地址:下载 - JDBC Driver for SQL Server | Microsoft Learn)
3、由于 BLOB 字段类型,在mssql 中不存在,需要在指定的数据库中创建自定义类型脚本如下:
use databasename
go
CREATE TYPE [dbo].[BLOB] FROM [image]
go
4、以上操作配置完毕后,执行 apache-activemq-5.16.5\bin\win64 文件夹内 activemq.bat 文件,运行成功后,可在mssql 中看到如下所示表:
ACTIVEMQ_ACKS:ActiveMQ的签收信息
ACTIVEMQ_LOCK:ActiveMQ的锁信息
ACTIVEMQ_MSGS:ActiveMQ的消息的信息
注意:如果上图所示三张表创建成功后,修改如下配置:
到此配置结束。
程序中消息持久化需要修改,即 MsgDeliveryMode.Persistent :