Search code examples
activemq-classicapache-tomee

How to change ActiveMQ KahaDB directory in TomEE


From the TomEE 9 console I can see that the activeMQ KahaDB is using C:\Windows\system32\activemq-data\localhost\KahaDB as its default storage directory.

Jun 29, 2023 10:00:01 AM org.apache.activemq.broker.BrokerService <clinit>
INFO: Loaded the Bouncy Castle security provider at position: 14
Jun 29, 2023 10:00:02 AM org.apache.activemq.broker.BrokerService 
doStartPersistenceAdapter
INFO: Using Persistence Adapter: 
KahaDBPersistenceAdapter[C:\Windows\system32\activemq-data\localhost\KahaDB]
Jun 29, 2023 10:00:02 AM org.apache.activemq.store.SharedFileLocker doStart
INFO: Database activemq-data\localhost\KahaDB\lock is locked by another server. This broker is now in slave mode waiting a lock to be acquired

Since C:\Windows\system32 is a protected folder it is causing the ActiveMQ to split out this INFO

INFO: Database activemq-data\localhost\KahaDB\lock is locked by another server. This broker is now in slave mode waiting a lock to be acquired

I found this link that documents how I can custom configure the KahaDB. The configuration looks simple enough, but I just don't know what xml file to put the configuration in? I tried server.xml but it didn't work.

Below is the content of server.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
  <Server port="18005" shutdown="SHUTDOWN">
  <!-- TomEE plugin for Tomcat -->
  <Listener className="org.apache.tomee.catalina.ServerListener"/>
  <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!-- APR library loader. Documentation at /docs/apr.html -->
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
    
  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         HTTP Connector: /docs/config/http.html
         AJP  Connector: /docs/config/ajp.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector connectionTimeout="20000" port="18080" protocol="HTTP/1.1" redirectPort="18443" server="Apache TomEE" xpoweredBy="false"/>
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="18080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="18443" />
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the SSLImplementation selected. JSSE style configuration is used below.
    -->
    <!--
    <Connector port="18443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" xpoweredBy="false" server="Apache TomEE" >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 18009 -->
    <!--
    <Connector protocol="AJP/1.3"
               address="::1"
               port="18009"
               redirectPort="18443" />
    -->

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine defaultHost="localhost" name="Catalina">

      <broker brokerName="broker">
        <persistenceAdapter>
            <kahaDB directory="/var/apps/apache-tomee-plus-9.0/boss/usmo/Boss/resources/jmsQueues" journalMaxFileLength="32mb"/>
        </persistenceAdapter>
      </broker>
    
      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>

      <Context docBase="Boss" path="/Boss" reloadable="true" source="org.eclipse.jst.jee.server:Boss"/><Context docBase="dhiveCommandXML" path="/dhiveCommandXML" reloadable="true" source="org.eclipse.jst.jee.server:dhiveCommandXML"/></Host>
    </Engine>
  </Service>
</Server>

Solution

  • Finally able to configure the TOMEE activemq to use a different KahaDB folder. I followed the instructions from document (See the 'Internal ActiveMQ Broker with activemq.xml' section). Below is a short summary of the instructions and some additional notes:

    1. download and add the following jars into tomee server's lib folder:
    spring-beans-3.2.9.RELEASE.jar
    spring-context-3.2.9.RELEASE.jar
    spring-core-3.2.9.RELEASE.jar
    spring-web-3.2.9.RELEASE.jar
    xbean-spring-3.9.jar
    activemq-spring-5.8.0.jar (This one is not mentioned in the document)
    activemq_spring-expression-5.3.29.jar (This one is not mentioned in the document)
    
    1. Add tomee.xml to the tomee server's conf folder. See example below. Note: BrokerXmlConfig is where you specify the url of the activemq configure file (note: I was unable to use relative path (like conf/activemq.xml). So, I ended up using absolute path)
    <?xml version="1.0" encoding="UTF-8"?>
    <tomee>
      <!-- see http://tomee.apache.org/containers-and-resources.html -->
    
      <!-- activate next line to be able to deploy applications in apps -->
      <!-- <Deployments dir="apps" /> -->
      
    
        <Resource id="BossJmsResourceAdapter" type="ActiveMQResourceAdapter">
            BrokerXmlConfig =  xbean:file:/var/apps/apache-tomee-plus-9.0/boss/usmo/Boss/resources/properties/activemq.xml
            ServerUrl       =  vm://localhost
        </Resource>
    
        <Resource id="BossJmsConnectionFactory" type="jakarta.jms.ConnectionFactory">
            ResourceAdapter = BossJmsResourceAdapter
        </Resource>
    
        <Container id="BossJmsMdbContainer" ctype="MESSAGE">
            ResourceAdapter = BossJmsResourceAdapter
        </Container>
    
        <Resource id="jms/TransactionManager" type="jakarta.jms.Queue"/>
        <Resource id="jms/EmailManager" type="jakarta.jms.Queue"/>
    
    </tomee>
    
    1. Create an activemq.xml file at the url specified in step 2. Below is an example of one:
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" 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-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
    <!--  Allows us to use external properties as variables in this configuration file  -->
        <broker xmlns="http://activemq.apache.org/schema/core" useJmx="true" brokerName="localhost" useShutdownHook="false" persistent="true" start="false" schedulerSupport="false" enableStatistics="false" offlineDurableSubscriberTimeout="259200000" offlineDurableSubscriberTaskSchedule="3600000">
            <persistenceAdapter>
                <kahaDB directory="/var/apps/apache-tomee-plus-9.0/boss/usmo/Boss/resources/jmsQueues/activemq-data/kaha" indexCacheSize="20000" ignoreMissingJournalfiles="true" checkForCorruptJournalFiles="true" checksumJournalFiles="true"/>
            </persistenceAdapter>
            <!--  The transport connectors ActiveMQ will listen to. Set the same for the serverUrl in the tomee.xml file  -->
            <transportConnectors>
                <!-- Local broker -->
                <transportConnector name="vm" uri="vm://localhost"/>
            </transportConnectors>
        </broker>
    </beans>