How to get hazelcast instance in lifecycle listener?

587 views
Skip to first unread message

Igor Aptekar

unread,
Apr 5, 2016, 5:34:42 AM4/5/16
to Hazelcast
I need to run some code when my client connects to a hazelcast cluster. I have added my lifecycle listener to the hazelcast listener config. The listener is made HazelcastInstanceAware because I need a hazelcast instance to create an executor to run my code on. However, the instance is never set. Is there a way to do this? It does work if I set the listener via the lifecycle service on the hazelcast instance - but by then I find it's too late and i have missed the client connection event.

cheers

vass...@hazelcast.com

unread,
Apr 5, 2016, 11:08:11 AM4/5/16
to Hazelcast
Hi Igor,

thanks for your post. Injection of HazelcastInstance in HazelcastInstanceAware classes does not currently work for LifecycleListener or ClientListeners. The javadoc for HazelcastInstanceAware reads "Used to get HazelcastInstance reference when submitting a Runnable/Callable using Hazelcast ExecutorService.", however it would certainly be useful to have the HazelcastInstance set in Lifecycle/ClientListeners, so please open an issue on github (https://github.com/hazelcast/hazelcast/issues) for this enhancement.

Cheers!
Vassilis

mann

unread,
Feb 14, 2017, 8:35:36 AM2/14/17
to Hazelcast, vass...@hazelcast.com
Hi Vass,

Do we have this feature available now? I am using 3.7.1 enterprise version of Hazelcast.

Vassilis Bekiaris

unread,
Feb 14, 2017, 10:03:51 AM2/14/17
to haze...@googlegroups.com

Hi Mann,

yes, HazelcastInstance is injected in a LifecycleListener when you declare it in your Config either as implementation or by class name. Both of the following work in 3.7.1:

public class HzAwareListener implements HazelcastInstanceAware, LifecycleListener {
...
}
// set implementation to an existing instance of your listener class
Config cfg  = new Config();
cfg.addListenerConfig(new ListenerConfig().setImplementation(new HzAwareListener()));
HazelcastInstance hz = Hazelcast.newHazelcastInstance(cfg);

// set class name in listener config; an instance will be created by Hazelcast
Config cfg  = new Config();
cfg.addListenerConfig(new ListenerConfig().setClassName("org.domain.HzAwareListener"));
HazelcastInstance hz = Hazelcast.newHazelcastInstance(cfg);

Best,

Vassilis

-- You received this message because you are subscribed to the Google Groups "Hazelcast" group. To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com. To post to this group, send email to haze...@googlegroups.com. Visit this group at https://groups.google.com/group/hazelcast. To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/6e473af8-5d8e-4601-b0d8-a54c8d04c597%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

mann

unread,
Feb 14, 2017, 10:33:06 AM2/14/17
to Hazelcast
Hi Vass,

thanks for quick reply.

Just to be more specific, is this feature only on LifecycleListener? I need this for EntryAddedListener, EntryUpdatedListener.

Vassilis Bekiaris

unread,
Feb 20, 2017, 5:13:36 AM2/20/17
to Hazelcast
Hi Mann,

yes, it works the same for entry listeners you configure in your MapConfig's.

Best,
Vassilis
Reply all
Reply to author
Forward
0 new messages