use of javax.cache.expiry.Duration in project ignite by apache.
the class CacheEntryListenersTest method testJCacheListenersExpiredEntries.
/**
* Test JCache entry listeners with expired entries.
*/
@Test
public void testJCacheListenersExpiredEntries() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testJCacheListenersWithExp");
JCacheEntryListener<Integer, Integer> lsnr = new JCacheEntryListener<>();
cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr, null, true, false));
cache = cache.withExpirePolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 1)));
for (int i = 0; i < 100; i++) cache.put(i, i);
int cntCreated = 0;
int cntExpired = 0;
for (int i = 0; i < 200; i++) {
// There should be two events for each cache entry.
CacheEntryEvent<?, ?> evt = lsnr.poll();
if (evt.getEventType() == EventType.CREATED)
cntCreated++;
else if (evt.getEventType() == EventType.EXPIRED)
cntExpired++;
else
fail("Unexpected event type: " + evt.getEventType());
}
assertEquals(100, cntCreated);
assertEquals(100, cntExpired);
assertTrue(lsnr.isQueueEmpty());
}
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class CacheEntryListenersTest method testContinuousQueriesWithIncludeExpired.
/**
* Test continuous queries with include expired parameter.
*/
@Test
public void testContinuousQueriesWithIncludeExpired() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testCQWithInclExp");
ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1);
ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2);
qry1.setIncludeExpired(false);
qry2.setIncludeExpired(true);
cache.query(qry1);
cache.query(qry2);
cache = cache.withExpirePolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 1)));
for (int i = 0; i < 100; i++) cache.put(i, i);
int cntCreated = 0;
int cntExpired = 0;
for (int i = 0; i < 100; i++) assertEquals(EventType.CREATED, lsnr1.poll().getEventType());
for (int i = 0; i < 200; i++) {
// There should be two events for each cache entry.
CacheEntryEvent<?, ?> evt = lsnr2.poll();
if (evt.getEventType() == EventType.CREATED)
cntCreated++;
else if (evt.getEventType() == EventType.EXPIRED)
cntExpired++;
else
fail("Unexpected event type: " + evt.getEventType());
}
assertEquals(100, cntCreated);
assertEquals(100, cntExpired);
assertTrue(lsnr1.isQueueEmpty());
assertTrue(lsnr2.isQueueEmpty());
}
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class GridCacheAbstractLocalStoreSelfTest method testEvict.
/**
* @throws Exception If failed.
*/
@Test
public void testEvict() throws Exception {
Ignite ignite1 = startGrid(1);
IgniteCache<Object, Object> cache = ignite1.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 100L)));
// Putting entry.
for (int i = 0; i < KEYS; i++) cache.put(i, i);
// Wait when entry
U.sleep(200);
// Check that entry is evicted from cache, but local store does contain it.
for (int i = 0; i < KEYS; i++) {
cache.localEvict(Arrays.asList(i));
assertNull(cache.localPeek(i));
assertEquals(i, (int) LOCAL_STORE_1.load(i).get1());
assertEquals(i, cache.get(i));
}
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class JavaThinCompatibilityTest method testExpiryPolicy.
/**
*/
private void testExpiryPolicy() throws Exception {
X.println(">>>> Testing expiry policy");
try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
ClientCache<Object, Object> cache = client.getOrCreateCache("testExpiryPolicy");
cache = cache.withExpirePolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 1)));
cache.put(1, 1);
doSleep(10);
assertFalse(cache.containsKey(1));
}
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class GridManagerAdapter method onKernalStart.
/**
* {@inheritDoc}
*/
@Override
public final void onKernalStart(boolean active) throws IgniteCheckedException {
for (final IgniteSpi spi : spis) {
try {
spi.onContextInitialized(new IgniteSpiContext() {
@Override
public boolean isStopping() {
return ctx.isStopping();
}
@Override
public Collection<ClusterNode> remoteNodes() {
return ctx.discovery().remoteNodes();
}
@Override
public Collection<ClusterNode> nodes() {
return ctx.discovery().allNodes();
}
@Override
public ClusterNode localNode() {
return ctx.discovery().localNode();
}
@Override
public Collection<ClusterNode> remoteDaemonNodes() {
final Collection<ClusterNode> all = ctx.discovery().daemonNodes();
return !localNode().isDaemon() ? all : F.view(all, new IgnitePredicate<ClusterNode>() {
@Override
public boolean apply(ClusterNode n) {
return n.isDaemon();
}
});
}
@Nullable
@Override
public ClusterNode node(UUID nodeId) {
A.notNull(nodeId, "nodeId");
return ctx.discovery().node(nodeId);
}
@Override
public boolean pingNode(UUID nodeId) {
A.notNull(nodeId, "nodeId");
try {
return ctx.discovery().pingNode(nodeId);
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public void send(ClusterNode node, Serializable msg, String topic) throws IgniteSpiException {
A.notNull(node, "node");
A.notNull(msg, "msg");
A.notNull(topic, "topic");
try {
if (msg instanceof Message)
ctx.io().sendToCustomTopic(node, topic, (Message) msg, SYSTEM_POOL);
else
ctx.io().sendUserMessage(Collections.singletonList(node), msg, topic, false, 0, false);
} catch (IgniteCheckedException e) {
throw unwrapException(e);
}
}
@Override
public void addLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
A.notNull(topic, "topic");
A.notNull(p, "p");
ctx.io().addUserMessageListener(topic, p);
}
@Override
public void removeLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
A.notNull(topic, "topic");
A.notNull(topic, "p");
ctx.io().removeUserMessageListener(topic, p);
}
@Override
public void addMessageListener(GridMessageListener lsnr, String topic) {
A.notNull(lsnr, "lsnr");
A.notNull(topic, "topic");
ctx.io().addMessageListener(topic, lsnr);
}
@Override
public boolean removeMessageListener(GridMessageListener lsnr, String topic) {
A.notNull(lsnr, "lsnr");
A.notNull(topic, "topic");
return ctx.io().removeMessageListener(topic, lsnr);
}
@Override
public void addLocalEventListener(GridLocalEventListener lsnr, int... types) {
A.notNull(lsnr, "lsnr");
ctx.event().addLocalEventListener(lsnr, types);
}
@Override
public boolean removeLocalEventListener(GridLocalEventListener lsnr) {
A.notNull(lsnr, "lsnr");
return ctx.event().removeLocalEventListener(lsnr);
}
@Override
public boolean isEventRecordable(int... types) {
for (int t : types) if (!ctx.event().isRecordable(t))
return false;
return true;
}
@Override
public void recordEvent(Event evt) {
A.notNull(evt, "evt");
if (ctx.event().isRecordable(evt.type()))
ctx.event().record(evt);
}
@Override
public void registerPort(int port, IgnitePortProtocol proto) {
ctx.ports().registerPort(port, proto, spi.getClass());
}
@Override
public void deregisterPort(int port, IgnitePortProtocol proto) {
ctx.ports().deregisterPort(port, proto, spi.getClass());
}
@Override
public void deregisterPorts() {
ctx.ports().deregisterPorts(spi.getClass());
}
@Nullable
@Override
public <K, V> V get(String cacheName, K key) {
return ctx.cache().<K, V>jcache(cacheName).get(key);
}
@Nullable
@Override
public <K, V> V put(String cacheName, K key, V val, long ttl) {
try {
if (ttl > 0) {
ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
return cache.getAndPut(key, val);
} else
return ctx.cache().<K, V>jcache(cacheName).getAndPut(key, val);
} catch (IgniteCheckedException e) {
throw CU.convertToCacheException(e);
}
}
@Nullable
@Override
public <K, V> V putIfAbsent(String cacheName, K key, V val, long ttl) {
try {
if (ttl > 0) {
ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
return cache.getAndPutIfAbsent(key, val);
} else
return ctx.cache().<K, V>jcache(cacheName).getAndPutIfAbsent(key, val);
} catch (IgniteCheckedException e) {
throw CU.convertToCacheException(e);
}
}
@Nullable
@Override
public <K, V> V remove(String cacheName, K key) {
return ctx.cache().<K, V>jcache(cacheName).getAndRemove(key);
}
@Override
public <K> boolean containsKey(String cacheName, K key) {
return ctx.cache().cache(cacheName).containsKey(key);
}
@Override
public int partition(String cacheName, Object key) {
return ctx.cache().cache(cacheName).affinity().partition(key);
}
@Override
public IgniteNodeValidationResult validateNode(ClusterNode node) {
for (GridComponent comp : ctx) {
IgniteNodeValidationResult err = comp.validateNode(node);
if (err != null)
return err;
}
return null;
}
@Nullable
@Override
public IgniteNodeValidationResult validateNode(ClusterNode node, DiscoveryDataBag discoData) {
for (GridComponent comp : ctx) {
if (comp.discoveryDataType() == null)
continue;
IgniteNodeValidationResult err = comp.validateNode(node, discoData.newJoinerDiscoveryData(comp.discoveryDataType().ordinal()));
if (err != null)
return err;
}
return null;
}
@Override
public Collection<SecuritySubject> authenticatedSubjects() {
try {
return ctx.security().authenticatedSubjects();
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public SecuritySubject authenticatedSubject(UUID subjId) {
try {
return ctx.security().authenticatedSubject(subjId);
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public MessageFormatter messageFormatter() {
return ctx.io().formatter();
}
@Override
public MessageFactory messageFactory() {
return ctx.io().messageFactory();
}
@Override
public boolean tryFailNode(UUID nodeId, @Nullable String warning) {
return ctx.discovery().tryFailNode(nodeId, warning);
}
@Override
public void failNode(UUID nodeId, @Nullable String warning) {
ctx.discovery().failNode(nodeId, warning);
}
@Override
public void addTimeoutObject(IgniteSpiTimeoutObject obj) {
ctx.timeout().addTimeoutObject(new GridSpiTimeoutObject(obj));
}
@Override
public void removeTimeoutObject(IgniteSpiTimeoutObject obj) {
ctx.timeout().removeTimeoutObject(new GridSpiTimeoutObject(obj));
}
@Override
public Map<String, Object> nodeAttributes() {
return ctx.nodeAttributes();
}
@Override
public boolean communicationFailureResolveSupported() {
return ctx.discovery().communicationErrorResolveSupported();
}
@Override
public void resolveCommunicationFailure(ClusterNode node, Exception err) {
ctx.discovery().resolveCommunicationError(node, err);
}
@Override
public ReadOnlyMetricRegistry getOrCreateMetricRegistry(String name) {
return ctx.metric().registry(name);
}
@Override
public void removeMetricRegistry(String name) {
ctx.metric().remove(name);
}
@Override
public Iterable<ReadOnlyMetricRegistry> metricRegistries() {
return ctx.metric();
}
@Override
public void addMetricRegistryCreationListener(Consumer<ReadOnlyMetricRegistry> lsnr) {
ctx.metric().addMetricRegistryCreationListener(lsnr);
}
/**
* @param e Exception to handle.
* @return GridSpiException Converted exception.
*/
private IgniteSpiException unwrapException(IgniteCheckedException e) {
// Avoid double-wrapping.
if (e.getCause() instanceof IgniteSpiException)
return (IgniteSpiException) e.getCause();
return new IgniteSpiException("Failed to execute SPI context method.", e);
}
});
} catch (IgniteSpiException e) {
throw new IgniteCheckedException("Failed to initialize SPI context.", e);
}
}
onKernalStart0();
}
Aggregations