Is there a Java Lock implementation with timeouts -


i have spring application can run in clustered environment. in environment use redis (and redisson) distributed lock-service.

many of locks used e.g. protect tasks can run once @ time, or may started every x seconds.

the application capable of running in standalone mode (without redis).

however case need different implementation of lockservice. thought extremely simple because need create lock instance locally timeout (e.g. "only run action @ ever 2 minutes"). when looking around not find implementation of java lock interface supports setting timeout lock (so automatically unlocks after time).

is there such thing, or there extremely simple (in terms of lines-of-code) way how can implement myself, i'm missing?

how lock impl should behave:

  • other threads not able lock while it's active (as other lock)
  • ideally, owning thread should able call lock(long timoutms) again extend lock (set timeout given time again)

edit: seems concrete example understand looking for:

  • imagine server has http action "doexpesivetask"
  • whenever task called, application goes ockservice" , calls .tryacquirelock("expensivetaskid", 10, timeunit.minutes) , gets boolean if got lock or not.
  • if got lock starts task
  • if didn't lock doesn't , shows user "you have more patient"

in distributed setup implementation of lockservice uses redis (and redisson library) distributed locks (this works great)! have simple switch between distributed , standalone mode, want have implementation of lockservice doesn't rely on external service. therefore need implementation of lock supports timeout. have concurrenthashmap inside lockservice maps lock-ids these lock instances.

why not use map maps lock-ids time-objects: because need prevent other threads re-locking (extending lifetime) of lock acquired thread.

your description little bit ambiguous, talking locks, not locking resource (or did not provide example). feel problem relates scheduling.

since use spring, have @ scheduling options. recent versions allow use @scheduled annotation trigger that. @enablescheduling fires background task executor. combo spring profiles, ensure these kick in when pass profile, example jvm parameter.

copied docs:

package hello;  import java.text.simpledateformat; import java.util.date;  import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.component;  @component public class scheduledtasks {      private static final simpledateformat dateformat = new simpledateformat("hh:mm:ss");      @scheduled(fixedrate = 5000)     public void reportcurrenttime() {         system.out.println("the time " + dateformat.format(new date()));     } } 

and enable:

package hello;  import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.scheduling.annotation.enablescheduling;  @springbootapplication @enablescheduling public class application {      public static void main(string[] args) throws exception {         springapplication.run(application.class);     } } 

there quick guide here:

spring docs

service code (you want go enumerators, used strings clarity):

import org.apache.commons.collections4.map.passiveexpiringmap;  public class standalonelockservice {     private map ordinarylocks;     private map expiringlocks;      public standalonelockservice() {         this.ordinarylocks = new hashmap<string, long>();         this.expiringlocks = new passiveexpiringmap<string, long>(2l,                 timeunit.minutes);     }      public synchronized boolean accquirelock(string task) {         if (ordinarylocks.containskey("task")                 || expiringlocks.containskey("task")) {             return false;         } else {             return handle("task");         }     }      private boolean handle(string jdk7) {         switch (jdk7) { // logic          }     }     private void releaselock(string task) {         switch (task) { // logic          }     } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -