protected KeyValuePair<String, Session> readEvent(ClientCacheEntryCustomEvent<byte[]> event) { byte[] eventData = event.getEventData(); ByteBuffer rawData = ByteBuffer.wrap(eventData); byte[] rawKey = readElement(rawData); byte[] rawValue = readElement(rawData); String key = dataFormat.keyToObj(rawKey, whitelist); KeyValuePair keyValuePair; if (rawValue == null) { // This events will hold either an old or a new value almost every time. But there are some corner cases // during rebalance where neither a new or an old value will be present. This if handles this case keyValuePair = new KeyValuePair<>(key, new MapSession(key)); } else { keyValuePair = new KeyValuePair<>(key, dataFormat.valueToObj(rawValue, whitelist)); } return keyValuePair; }
@Test public void testHotRodInteroperability() throws Exception { remoteCache.clear(); // Put object via Hot Rod remoteCache.put("BTC", new CryptoCurrency("Bitcoin", 1)); remoteCache.put("ETH", new CryptoCurrency("Ethereum", 2)); remoteCache.put("XRP", new CryptoCurrency("Ripple", 3)); remoteCache.put("CAT", new CryptoCurrency("Catcoin", 618)); assertEquals(remoteCache.get("CAT").getDescription(), "Catcoin"); assertEquals(remoteCache.size(), 4); Query query = Search.getQueryFactory(remoteCache).create("FROM " + getEntityName() + " c where c.rank < 10"); List<CryptoCurrency> highRankCoins = query.list(); assertEquals(highRankCoins.size(), 3); // Read as Json CryptoCurrency btc = readCurrencyViaJson("BTC"); assertEquals("Bitcoin", btc.getDescription()); assertEquals(Integer.valueOf(1), btc.getRank()); // Write as Json writeCurrencyViaJson("LTC", "Litecoin", 4); // Assert inserted entity is searchable query = Search.getQueryFactory(remoteCache).create("FROM " + getEntityName() + " c where c.description = 'Litecoin'"); CryptoCurrency litecoin = (CryptoCurrency) query.list().iterator().next(); assertEquals(litecoin.getDescription(), "Litecoin"); assertTrue(litecoin.getRank() == 4); // Read as JSON from the Hot Rod client Object jsonResult = remoteCache.withDataFormat(DataFormat.builder().valueType(MediaType.APPLICATION_JSON).build()).get("LTC"); JsonNode jsonNode = new ObjectMapper().readTree((byte[]) jsonResult); assertEquals("Litecoin", jsonNode.get("description").asText()); }
@Override public byte[] getEventData() { RemoteCache cache = remoteCacheManager.getCache(); int keySizeEstimate = cache.getRemoteCacheManager().getConfiguration().keySizeEstimate(); int valueSizeEstimate = cache.getRemoteCacheManager().getConfiguration().valueSizeEstimate(); byte[] key = cache.getDataFormat().keyToBytes(sessionId, keySizeEstimate, valueSizeEstimate); int capacity = UnsignedNumeric.sizeUnsignedInt(key.length) + key.length; byte[] out = new byte[capacity]; int offset = UnsignedNumeric.writeUnsignedInt(out, 0, key.length); System.arraycopy(key, 0, out, offset, key.length); return out; }
@Test public void testStringKeysAndStringValues() throws Exception { // Write via Hot Rod (the HR client is configured with a String marshaller) stringRemoteCache.put("key", "Hello World"); assertEquals(stringRemoteCache.get("key"), "Hello World"); // Read via Rest Object bytesFromRest = new RestRequest().cache(STRING_CACHE_NAME).key("key").accept(TEXT_PLAIN).read(); assertEquals(asString(bytesFromRest), "Hello World"); // Write via rest new RestRequest().cache(STRING_CACHE_NAME).key("key2").value("Testing").write(); // Read via Hot Rod assertEquals(stringRemoteCache.get("key2"), "Testing"); // Get values as JSON from Hot Rod Object jsonString = stringRemoteCache.withDataFormat(DataFormat.builder() .valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build()) .get("key"); assertEquals("\"Hello World\"", jsonString); }
@Test @SuppressWarnings("unchecked") public void testDataConversionsWithDefaultRemoteCache() throws Exception { String key = "key-byte-array-1"; byte[] value = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21}; // Hello! String stringValue = new String(value); DataFormat writeValueUnmarshalled = DataFormat.builder().valueType(APPLICATION_OCTET_STREAM).build(); // Avoid marshalling values when writing RemoteCache<String, byte[]> octetStreamCacheValue = this.remoteCache.withDataFormat(writeValueUnmarshalled); octetStreamCacheValue.put(key, value); assertArrayEquals(value, octetStreamCacheValue.get(key)); // Read as UTF Object utfValue = this.remoteCache .withDataFormat(DataFormat.builder().valueType(TEXT_PLAIN).valueMarshaller(new UTF8StringMarshaller()).build()) .get(key); assertEquals(stringValue, utfValue); // Read as XML Object xmlValue = this.remoteCache .withDataFormat(DataFormat.builder().valueType(APPLICATION_XML).valueMarshaller(new UTF8StringMarshaller()).build()) .get(key); assertEquals("<string>" + stringValue + "</string>", xmlValue); // Read as JSON Object jsonValue = this.remoteCache .withDataFormat(DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build()) .get(key); assertEquals("\"Hello!\"", jsonValue); }
DataFormat format = DataFormat.builder() .keyType(TEXT_PLAIN) .valueType(APPLICATION_JBOSS_MARSHALLING).valueMarshaller(IdentityMarshaller.INSTANCE) DataFormat.builder().keyType(TEXT_PLAIN).build(); byte[] result = (byte[]) defaultMarshalledRemoteCache .withDataFormat(DataFormat.builder().keyType(TEXT_PLAIN).build()).get(otherKey); assertArrayEquals(otherValue, result);