Created
April 28, 2016 16:35
-
-
Save johnou/e28d09483d114885234195ffbd9af618 to your computer and use it in GitHub Desktop.
This file contains 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
import cloud.orbit.actors.extensions.AbstractStorageExtension; | |
import cloud.orbit.actors.extensions.json.ActorReferenceModule; | |
import cloud.orbit.actors.runtime.DefaultDescriptorFactory; | |
import cloud.orbit.actors.runtime.RemoteReference; | |
import cloud.orbit.concurrent.Task; | |
import cloud.orbit.exception.UncheckedException; | |
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
import java.io.IOException; | |
/** | |
* @author Johno Crawford (johno@sulake.com) | |
*/ | |
@Component | |
public class JdbcStorageExtension extends AbstractStorageExtension { | |
@Autowired | |
private OrbitStateDao orbitStateDao; | |
private ObjectMapper mapper; | |
@Override | |
public Task<Void> start() { | |
mapper = new ObjectMapper(); | |
mapper.registerModule(new ActorReferenceModule(DefaultDescriptorFactory.get())); | |
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker(). | |
withFieldVisibility(JsonAutoDetect.Visibility.ANY) | |
.withGetterVisibility(JsonAutoDetect.Visibility.NONE) | |
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE) | |
.withSetterVisibility(JsonAutoDetect.Visibility.NONE) | |
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); | |
return Task.done(); | |
} | |
@Override | |
public Task<Void> clearState(RemoteReference<?> actorReference, Object state) { | |
orbitStateDao.delete(asKey(actorReference)); | |
return Task.done(); | |
} | |
@Override | |
public Task<Boolean> readState(RemoteReference<?> actorReference, Object state) { | |
try { | |
String readState = orbitStateDao.get(asKey(actorReference)); | |
if (readState != null) { | |
mapper.readerForUpdating(state).readValue(readState); | |
} | |
return Task.fromValue(readState != null); | |
} catch (RuntimeException | IOException e) { | |
throw new UncheckedException(e); | |
} | |
} | |
@Override | |
public Task<Void> writeState(RemoteReference<?> actorReference, Object state) { | |
try { | |
String serializedState = mapper.writeValueAsString(state); | |
orbitStateDao.update(asKey(actorReference), serializedState); | |
return Task.done(); | |
} catch (RuntimeException | IOException e) { | |
throw new UncheckedException(e); | |
} | |
} | |
static final String KEY_SEPARATOR = "|"; | |
private String asKey(final RemoteReference<?> reference) { | |
return RemoteReference.getInterfaceClass(reference).getSimpleName() + KEY_SEPARATOR + String.valueOf(RemoteReference.getId(reference)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment