Created
July 14, 2016 12:13
-
-
Save shahamit/80b1c63e9c9c89b3ff32578a77a54b54 to your computer and use it in GitHub Desktop.
Class definition not found when working with Binary Objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void testLoad() { | |
IgniteCache<BinaryObject, BinaryObject> cache = ignite.getOrCreateCache(getConfig()).withKeepBinary(); | |
String keyValue = "3M Company"; | |
BinaryObjectBuilder keyBuilder = ignite.binary().builder("keyType") | |
.setField("f1", keyValue).hashCode(fieldValue.hashCode()); | |
BinaryObject value = cache.get(keyBuilder.build()); | |
logger.info("Loaded value {} for key {}", value, keyValue); | |
} | |
public static CacheConfiguration<BinaryObject, BinaryObject> getConfig() { | |
CacheConfiguration<BinaryObject, BinaryObject> cfg = new CacheConfiguration<>(); | |
cfg.setName("testCache); | |
cfg.setCacheMode(CacheMode.REPLICATED); | |
cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); | |
cfg.setBackups(1); | |
cfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.PRIMARY_SYNC); | |
cfg.setCacheStoreFactory(FactoryBuilder.factoryOf(MyCacheStore.class)); | |
// Configure JDBC session listener. | |
cfg.setCacheStoreSessionListenerFactories(() -> { | |
CacheJdbcStoreSessionListener lsnr = new CacheJdbcStoreSessionListener(); | |
lsnr.setDataSource(JdbcConnectionPool.create(connectionUrl); | |
return lsnr; | |
}); | |
cfg.setReadThrough(true); | |
cfg.setWriteThrough(true); | |
return cfg; | |
} | |
public class MyCacheStore extends CacheStoreAdapter<BinaryObject, BinaryObject> { | |
@CacheStoreSessionResource | |
private CacheStoreSession ses; | |
@IgniteInstanceResource | |
private Ignite ignite; | |
private Logger logger = LoggerFactory.getLogger(CustomerAttributeCacheStore.class); | |
@Override | |
public BinaryObject load(BinaryObject key) throws CacheLoaderException { | |
logger.info("Loading value for key"); | |
Connection conn = ses.attachment(); | |
try(PreparedStatement st = conn.prepareStatement("select F2, F3 from CACHE_TABLE where F1 = ?")) { | |
st.setString(1, key.field("F1")); | |
ResultSet rs = st.executeQuery(); | |
return rs.next() ? ignite.binary().builder(table.getCacheValueType()) | |
.setField("F2", rs.getString("F2")) | |
.setField("F3", rs.getString("F3")).build() : null; | |
} catch (SQLException e) { | |
throw new CacheLoaderException("Failed to load object [key=" + key + ']', e); | |
} | |
} | |
//skipping the write and delete methods | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment