Search code examples
javaehcache

running multiple instances of ehcache on the same machine with a single config


im trying to set up a replicated ehcache that can run both over a local network and on the same machine. my current config looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<diskStore path="java.io.tmpdir"/>

<!-- clustering -->
<cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, multicastPacketTimeToLive=255"
        propertySeparator=","/>

<cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="hostname=localhost, port=40001, socketTimeoutMillis=2000"
        propertySeparator=","/>

<cache name="demo-cache"
       maxEntriesLocalHeap="0"
       maxEntriesLocalDisk="0"
       eternal="true"
       overflowToDisk="true"
       maxBytesLocalHeap="10M"
       diskPersistent="false"
       diskSpoolBufferSizeMB="5"
       clearOnFlush="false">
    <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true "/>
</cache>

this works across different machines, but does not work with multiple instances on the same machine. i know that playing around with the peer listener factory port (40001, 40002 etc) will probbaly work, but im hoping that there's a configuration that will "just work" in both scenarios.


Solution

  • short answer is i cant. the jgroups support in ehcache 2.5 doesnt work and with RMI each node needs its own port.

    what i eventually did was create the conf from the xml, parse the port out of the properties field for the peer listener factory and run a loop that creates ServerSockets until it finds a free port (meaning the ServerSocket constructor doesnt throw an exception) and uses that. its ugly but it works