简体   繁体   中英

How to initialize java Condition in Hazelcast

I'm new to Hazelcast and I'm trying to create java Condition in Hazelcast but I have no idea how to do so. Is it possible? if so, anyone know how to do that?

@Getter
@Service
public class HazelcastService {

    private FencedLock lock;
    private PNCounter counter;
    private Condition notEmpty;
    private IList<PushStatementObjectRequestDTO> listData;

    @Autowired
    HazelcastInstance hazelcastInstance;

    @PostConstruct
    public void init(){
        lock = hazelcastInstance.getCPSubsystem().getLock(LIST_LOCK);
        counter = hazelcastInstance.getPNCounter(COUNTER);
        notEmpty = lock.newCondition();
        listData = hazelcastInstance.getList(LIST_DATA);
    }
}

I found ICondition interface, but im not sure how to use it

I find the Java API documentation very clear about how to use java.util.concurrent.locks.Condition interface. It also provides a very good example of how to block writing items into a buffer when the buffer is full or block taking items from it while the buffer is empty.

Please have a look at this: https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/concurrent/locks/Condition.html

In Hazelcast the only difference is that you have a distributed condition com.hazelcast.core.ICondition implementing the java.util.concurrent.locks.Condition .

Hope this helps

UPDATE:

From Hazelcast IMDG 4.0 or later you can instantiate a java.util.concurrent.locks.Condition on a distributed lock using code similar with below:

public void howToGetCondition() {
    var hazelcastInstance = Hazelcast.newHazelcastInstance();
    var cpSubsystem = hazelcastInstance.getCPSubsystem();
    
    var bufferLock = cpSubsystem.getLock("buffer-lock");
    var condition = bufferLock.newCondition();
    // Do stuff with your condition
}

Of course it can no longer be an com.hazelcast.core.ICondition which was removed in 4.0

Hope this helped too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM