title: Hazelcast Add Local Entry Listener Only Once
body: <p>I have a 3-node application that includes an embedded Hazelcast instance (so, 3 instances). These are used in the application for session sharing across the nodes. In the configuration, I added a local entry listener to write an audit record to the database.</p>
  <p>Unfortunately, this caused something of a race condition because the listener ended up being added to the internal map of listeners 3 times. I'd end up getting a &quot;connection has already been closed&quot;. I ended up switching to an interceptor which did NOT seem to get added multiple times.</p>
  <p>I don't consider the interceptor method to be a great solution. However, the <code>addLocalEntryListener()</code> method returns a random UUID. So, it's not a matter of determining if the key exists in the map.</p>
  <p>Ideally, I'd like something like this:</p>
  <pre><code>@Bean
  public HazelcastInstance instance() {
  
      HazelcastInstance instance = Hazelcast.getInstance();
      
      IMap&lt;String, ?&gt; sessionsMap = instance.getMap(&quot;spring:session:sessions&quot;);
  
      if (/* something something to ensure the listener wasn't already there */) {
         sessionsMap.addLocalEntryListener(new MyLocalListener());
      }
  
  }
  </code></pre>