Skip to content

Instantly share code, notes, and snippets.

@alexterman
Created September 18, 2016 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexterman/cc39288cbb8e01cda3bcdfd618de114d to your computer and use it in GitHub Desktop.
Save alexterman/cc39288cbb8e01cda3bcdfd618de114d to your computer and use it in GitHub Desktop.
Hazelcast TTL delayed and executes in batches ~15 entries each time
package initial;
import com.hazelcast.config.Config;
import com.hazelcast.config.EntryListenerConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryEvictedListener;
import java.io.Serializable;
import java.time.LocalTime;
import java.util.Collections;
import static java.lang.System.setProperty;
public class Main {
private static final int TTL = 5;
public static void main(String[] args) throws InterruptedException {
setProperty("hazelcast.internal.map.expiration.task.period.seconds", "1");
EntryListenerConfig entryListenerConfig = createEntryListenerConfig();
MapConfig mapConfig = createMapConfig(entryListenerConfig);
Config config = createInstanceConfig(mapConfig);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
final IMap<String, Order> map = instance.getMap("testMap");
insertData(map);
}
private static Config createInstanceConfig(MapConfig mapConfig) {
Config config = new Config();
config.setInstanceName("testInstance");
config.addMapConfig(mapConfig);
return config;
}
private static EntryListenerConfig createEntryListenerConfig() {
EntryListenerConfig entryListenerConfig = new EntryListenerConfig();
entryListenerConfig.setImplementation(new EvictionListener());
return entryListenerConfig;
}
private static MapConfig createMapConfig(EntryListenerConfig entryListenerConfig) {
MapConfig mapConfig = new MapConfig();
mapConfig.setName("testMap");
mapConfig.setMaxIdleSeconds(TTL);
mapConfig.setEntryListenerConfigs(Collections.singletonList(entryListenerConfig));
return mapConfig;
}
private static void insertData(IMap<String, Order> map) {
for (int i = 0; i < 100; i++) {
map.put(i + "_", new Order());
}
System.err.println("finished inserting to map");
}
private static class Order implements Serializable {
public LocalTime localTime;
public Order() {
this.localTime = LocalTime.now();
}
}
private static class EvictionListener implements EntryEvictedListener<String, Order> {
@Override
public void entryEvicted(EntryEvent<String, Order> event) {
final LocalTime now = LocalTime.now();
System.err.println(event.getKey());
System.err.println("time inserted:\t" + event.getOldValue().localTime);
System.err.println("time evicted:\t" + now);
System.err.println("");
}
}
}
@alexterman
Copy link
Author

The application logs:

PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.1] Prefer IPv4 stack is true.
ספט 18, 2016 6:34:24 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.1] Picked [10.240.196.163]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
ספט 18, 2016 6:34:24 PM com.hazelcast.system
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Hazelcast 3.7.1 (20160905 - 1f47990) starting at [10.240.196.163]:5701
ספט 18, 2016 6:34:24 PM com.hazelcast.system
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
ספט 18, 2016 6:34:24 PM com.hazelcast.system
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Configured Hazelcast Serialization version : 1
ספט 18, 2016 6:34:25 PM com.hazelcast.spi.impl.operationservice.impl.BackpressureRegulator
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Backpressure is disabled
ספט 18, 2016 6:34:26 PM com.hazelcast.instance.Node
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Creating MulticastJoiner
ספט 18, 2016 6:34:26 PM com.hazelcast.core.LifecycleService
INFO: [10.240.196.163]:5701 [dev] [3.7.1] [10.240.196.163]:5701 is STARTING
ספט 18, 2016 6:34:26 PM com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Starting 4 partition threads
ספט 18, 2016 6:34:26 PM com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Starting 3 generic threads (1 dedicated for priority tasks)
ספט 18, 2016 6:34:26 PM com.hazelcast.nio.tcp.nonblocking.NonBlockingIOThreadingModel
INFO: [10.240.196.163]:5701 [dev] [3.7.1] TcpIpConnectionManager configured with Non Blocking IO-threading model: 3 input threads and 3 output threads
ספט 18, 2016 6:34:28 PM com.hazelcast.internal.cluster.impl.MulticastJoiner
INFO: [10.240.196.163]:5701 [dev] [3.7.1]

Members [1] {
Member [10.240.196.163]:5701 - c4687e69-4ee5-4ebb-aaf9-a09e7910fef6 this
}

ספט 18, 2016 6:34:28 PM com.hazelcast.core.LifecycleService
INFO: [10.240.196.163]:5701 [dev] [3.7.1] [10.240.196.163]:5701 is STARTED
ספט 18, 2016 6:34:28 PM com.hazelcast.internal.partition.impl.PartitionStateManager
INFO: [10.240.196.163]:5701 [dev] [3.7.1] Initializing cluster partition table arrangement...
finished inserting to map
57_
71_
time inserted: 18:34:29.236
time evicted: 18:34:34.604
3_
69_
time inserted: 18:34:29.227
time evicted: 18:34:34.604

87_
time inserted: 18:34:29.275
time evicted: 18:34:34.607
time inserted: 18:34:29.077
time evicted: 18:34:34.605

45_
time inserted: 18:34:29.179
time evicted: 18:34:34.607

30_

95_
time inserted: 18:34:29.299
time evicted: 18:34:34.607

50_
time inserted: 18:34:29.193
time evicted: 18:34:34.607

time inserted: 18:34:29.210
time evicted: 18:34:34.604

time inserted: 18:34:29.131
time evicted: 18:34:34.604

11_
time inserted: 18:34:29.106
time evicted: 18:34:34.608

41_
time inserted: 18:34:29.174
54_
time evicted: 18:34:34.608

time inserted: 18:34:29.204
time evicted: 18:34:34.608

72_
time inserted: 18:34:29.239
time evicted: 18:34:35.601

22_
time inserted: 18:34:29.120
time evicted: 18:34:35.602

83_
time inserted: 18:34:29.253
time evicted: 18:34:35.601

91_
time inserted: 18:34:29.291
time evicted: 18:34:35.603

70_
time inserted: 18:34:29.230
time evicted: 18:34:35.603

60_
time inserted: 18:34:29.214
time evicted: 18:34:35.604

66_
time inserted: 18:34:29.223
time evicted: 18:34:35.605

65_
time inserted: 18:34:29.222
time evicted: 18:34:35.605

35_
time inserted: 18:34:29.163
time evicted: 18:34:35.605

63_
time inserted: 18:34:29.217
time evicted: 18:34:35.606

23_
time inserted: 18:34:29.121
time evicted: 18:34:35.606

74_
time inserted: 18:34:29.240
time evicted: 18:34:35.606

38_
time inserted: 18:34:29.166
time evicted: 18:34:35.608

0_
time inserted: 18:34:28.950
time evicted: 18:34:36.601

94_
time inserted: 18:34:29.294
time evicted: 18:34:36.602

43_
time inserted: 18:34:29.178
time evicted: 18:34:36.601

76_
time inserted: 18:34:29.245
time evicted: 18:34:36.602

89_
time inserted: 18:34:29.276
time evicted: 18:34:36.602

24_
time inserted: 18:34:29.121
time evicted: 18:34:36.605

85_
time inserted: 18:34:29.266
time evicted: 18:34:36.606

15_
time inserted: 18:34:29.109
time evicted: 18:34:36.607

34_
time inserted: 18:34:29.162
time evicted: 18:34:36.607

20_
time inserted: 18:34:29.115
time evicted: 18:34:36.608

25_
time inserted: 18:34:29.122
time evicted: 18:34:36.609

49_
time inserted: 18:34:29.189
time evicted: 18:34:36.609

40_
time inserted: 18:34:29.168
time evicted: 18:34:36.609

75_
time inserted: 18:34:29.240
time evicted: 18:34:37.602

67_
time inserted: 18:34:29.226
time evicted: 18:34:37.604

68_
time inserted: 18:34:29.227
time evicted: 18:34:37.605

92_
time inserted: 18:34:29.294
time evicted: 18:34:37.605

96_
time inserted: 18:34:29.304
time evicted: 18:34:37.605

12_
time inserted: 18:34:29.107
time evicted: 18:34:37.606

86_
time inserted: 18:34:29.274
time evicted: 18:34:37.606

6_
time inserted: 18:34:29.099
time evicted: 18:34:37.609

52_
time inserted: 18:34:29.199
time evicted: 18:34:37.610

17_
time inserted: 18:34:29.112
time evicted: 18:34:37.610

99_
time inserted: 18:34:29.306
time evicted: 18:34:37.610

58_
time inserted: 18:34:29.211
time evicted: 18:34:37.611

31_
time inserted: 18:34:29.132
time evicted: 18:34:38.601

1_
time inserted: 18:34:29.060
time evicted: 18:34:38.601

27_
time inserted: 18:34:29.123
time evicted: 18:34:38.601

47_
time inserted: 18:34:29.180
77_
time evicted: 18:34:38.601

32_
88_
time inserted: 18:34:29.247
time inserted: 18:34:29.276
time evicted: 18:34:38.602

time inserted: 18:34:29.133
time evicted: 18:34:38.602

29_
time inserted: 18:34:29.127
time evicted: 18:34:38.602

time evicted: 18:34:38.601

93_
time inserted: 18:34:29.294
time evicted: 18:34:38.602

19_
time inserted: 18:34:29.112
time evicted: 18:34:38.602

51_
time inserted: 18:34:29.194
time evicted: 18:34:38.602

4_
time inserted: 18:34:29.088
16_
time evicted: 18:34:38.602

55_
time inserted: 18:34:29.204
time evicted: 18:34:38.602

time inserted: 18:34:29.111
time evicted: 18:34:38.602

61_
time inserted: 18:34:29.215
37_
time evicted: 18:34:38.602

62_
time inserted: 18:34:29.216
time evicted: 18:34:38.602

time inserted: 18:34:29.165
time evicted: 18:34:38.602

5_
time inserted: 18:34:29.098
time evicted: 18:34:39.604

44_
time inserted: 18:34:29.179
time evicted: 18:34:39.605

97_
time inserted: 18:34:29.304
time evicted: 18:34:39.605

42_
time inserted: 18:34:29.177
time evicted: 18:34:39.606

26_
time inserted: 18:34:29.123
time evicted: 18:34:39.606

79_
time inserted: 18:34:29.248
time evicted: 18:34:39.609

56_
time inserted: 18:34:29.206
time evicted: 18:34:39.609

84_
time inserted: 18:34:29.262
time evicted: 18:34:39.610

39_
time inserted: 18:34:29.166
time evicted: 18:34:39.610

82_
time inserted: 18:34:29.251
time evicted: 18:34:39.611

59_
time inserted: 18:34:29.214
time evicted: 18:34:39.611

73_
time inserted: 18:34:29.239
time evicted: 18:34:39.612

14_
time inserted: 18:34:29.109
time evicted: 18:34:39.613

13_
time inserted: 18:34:29.108
time evicted: 18:34:39.613

33_
time inserted: 18:34:29.157
time evicted: 18:34:39.613

28_
time inserted: 18:34:29.124
time evicted: 18:34:40.605

81_
time inserted: 18:34:29.249
time evicted: 18:34:40.605

7_
time inserted: 18:34:29.100
time evicted: 18:34:40.606

98_
time inserted: 18:34:29.305
time evicted: 18:34:40.607

8_
time inserted: 18:34:29.101
time evicted: 18:34:40.608

78_
21_
time inserted: 18:34:29.120
time evicted: 18:34:40.609

time inserted: 18:34:29.248
time evicted: 18:34:40.609

53_
time inserted: 18:34:29.199
time evicted: 18:34:40.610

64_
time inserted: 18:34:29.218
time evicted: 18:34:40.610

48_
time inserted: 18:34:29.181
time evicted: 18:34:40.608

10_
time inserted: 18:34:29.105
time evicted: 18:34:40.610

46_
time inserted: 18:34:29.179
time evicted: 18:34:40.610

90_
time inserted: 18:34:29.279
time evicted: 18:34:40.610

36_
time inserted: 18:34:29.164
time evicted: 18:34:40.611

2_
time inserted: 18:34:29.064
time evicted: 18:34:40.611

9_
time inserted: 18:34:29.101
time evicted: 18:34:41.617

18_
time inserted: 18:34:29.112
time evicted: 18:34:41.653

80_
time inserted: 18:34:29.248
time evicted: 18:34:41.661

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment