fix up Java API for serializers (both use and implement)
This commit is contained in:
parent
d2f28a06cd
commit
09897459d6
4 changed files with 24 additions and 14 deletions
|
|
@ -7,7 +7,9 @@ package akka.serialization
|
||||||
import akka.AkkaException
|
import akka.AkkaException
|
||||||
import scala.util.DynamicVariable
|
import scala.util.DynamicVariable
|
||||||
import com.typesafe.config.Config
|
import com.typesafe.config.Config
|
||||||
import akka.actor.{ Extension, ExtendedActorSystem, Address }
|
import akka.actor.{ Extension, ExtendedActorSystem, Address, DynamicAccess }
|
||||||
|
import akka.event.Logging
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import akka.util.NonFatal
|
import akka.util.NonFatal
|
||||||
import scala.collection.mutable.ArrayBuffer
|
import scala.collection.mutable.ArrayBuffer
|
||||||
import java.io.NotSerializableException
|
import java.io.NotSerializableException
|
||||||
|
|
@ -151,7 +153,7 @@ class Serialization(val system: ExtendedActorSystem) extends Extension {
|
||||||
*/
|
*/
|
||||||
private[akka] val bindings: Seq[ClassSerializer] = {
|
private[akka] val bindings: Seq[ClassSerializer] = {
|
||||||
val configuredBindings = for ((k: String, v: String) ← settings.SerializationBindings if v != "none") yield {
|
val configuredBindings = for ((k: String, v: String) ← settings.SerializationBindings if v != "none") yield {
|
||||||
val c = ReflectiveAccess.getClassFor(k, system.internalClassLoader).fold(throw _, identity[Class[_]])
|
val c = system.dynamicAccess.createClassFor(k).fold(throw _, identity[Class[_]])
|
||||||
(c, serializers(v))
|
(c, serializers(v))
|
||||||
}
|
}
|
||||||
sort(configuredBindings)
|
sort(configuredBindings)
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,16 @@ trait Serializer {
|
||||||
* the class should be loaded using ActorSystem.dynamicAccess.
|
* the class should be loaded using ActorSystem.dynamicAccess.
|
||||||
*/
|
*/
|
||||||
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef
|
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java API: deserialize without type hint
|
||||||
|
*/
|
||||||
|
final def fromBinary(bytes: Array[Byte]): AnyRef = fromBinary(bytes, None)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java API: deserialize with type hint
|
||||||
|
*/
|
||||||
|
final def fromBinary(bytes: Array[Byte], clazz: Class[_]): AnyRef = fromBinary(bytes, Option(clazz))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -61,14 +71,13 @@ trait Serializer {
|
||||||
* the JSerializer (also possible with empty constructor).
|
* the JSerializer (also possible with empty constructor).
|
||||||
*/
|
*/
|
||||||
abstract class JSerializer extends Serializer {
|
abstract class JSerializer extends Serializer {
|
||||||
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef =
|
final def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef =
|
||||||
fromBinary(bytes, manifest.orNull)
|
fromBinaryJava(bytes, manifest.orNull)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method should be overridden,
|
* This method must be implemented, manifest may be null.
|
||||||
* manifest and classLoader may be null.
|
|
||||||
*/
|
*/
|
||||||
def fromBinary(bytes: Array[Byte], manifest: Class[_]): AnyRef
|
protected def fromBinaryJava(bytes: Array[Byte], manifest: Class[_]): AnyRef
|
||||||
}
|
}
|
||||||
|
|
||||||
object NullSerializer extends NullSerializer
|
object NullSerializer extends NullSerializer
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,8 @@ public class SerializationDocTestBase {
|
||||||
|
|
||||||
// "fromBinary" deserializes the given array,
|
// "fromBinary" deserializes the given array,
|
||||||
// using the type hint (if any, see "includeManifest" above)
|
// using the type hint (if any, see "includeManifest" above)
|
||||||
// into the optionally provided classLoader.
|
@Override public Object fromBinaryJava(byte[] bytes,
|
||||||
@Override public Object fromBinary(byte[] bytes,
|
Class<?> clazz) {
|
||||||
Class clazz) {
|
|
||||||
// Put your code that deserializes here
|
// Put your code that deserializes here
|
||||||
//#...
|
//#...
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -74,7 +73,7 @@ public class SerializationDocTestBase {
|
||||||
|
|
||||||
// Turn it back into an object,
|
// Turn it back into an object,
|
||||||
// the nulls are for the class manifest and for the classloader
|
// the nulls are for the class manifest and for the classloader
|
||||||
String back = (String)serializer.fromBinary(bytes, Option.<Class<?>>none().asScala());
|
String back = (String)serializer.fromBinary(bytes);
|
||||||
|
|
||||||
// Voilá!
|
// Voilá!
|
||||||
assertEquals(original, back);
|
assertEquals(original, back);
|
||||||
|
|
|
||||||
|
|
@ -71,14 +71,14 @@ object ZeromqDocSpec {
|
||||||
def receive = {
|
def receive = {
|
||||||
// the first frame is the topic, second is the message
|
// the first frame is the topic, second is the message
|
||||||
case m: ZMQMessage if m.firstFrameAsString == "health.heap" ⇒
|
case m: ZMQMessage if m.firstFrameAsString == "health.heap" ⇒
|
||||||
ser.deserialize(m.payload(1), classOf[Heap], None) match {
|
ser.deserialize(m.payload(1), classOf[Heap]) match {
|
||||||
case Right(Heap(timestamp, used, max)) ⇒
|
case Right(Heap(timestamp, used, max)) ⇒
|
||||||
log.info("Used heap {} bytes, at {}", used, timestampFormat.format(new Date(timestamp)))
|
log.info("Used heap {} bytes, at {}", used, timestampFormat.format(new Date(timestamp)))
|
||||||
case Left(e) ⇒ throw e
|
case Left(e) ⇒ throw e
|
||||||
}
|
}
|
||||||
|
|
||||||
case m: ZMQMessage if m.firstFrameAsString == "health.load" ⇒
|
case m: ZMQMessage if m.firstFrameAsString == "health.load" ⇒
|
||||||
ser.deserialize(m.payload(1), classOf[Load], None) match {
|
ser.deserialize(m.payload(1), classOf[Load]) match {
|
||||||
case Right(Load(timestamp, loadAverage)) ⇒
|
case Right(Load(timestamp, loadAverage)) ⇒
|
||||||
log.info("Load average {}, at {}", loadAverage, timestampFormat.format(new Date(timestamp)))
|
log.info("Load average {}, at {}", loadAverage, timestampFormat.format(new Date(timestamp)))
|
||||||
case Left(e) ⇒ throw e
|
case Left(e) ⇒ throw e
|
||||||
|
|
@ -97,7 +97,7 @@ object ZeromqDocSpec {
|
||||||
def receive = {
|
def receive = {
|
||||||
// the first frame is the topic, second is the message
|
// the first frame is the topic, second is the message
|
||||||
case m: ZMQMessage if m.firstFrameAsString == "health.heap" ⇒
|
case m: ZMQMessage if m.firstFrameAsString == "health.heap" ⇒
|
||||||
ser.deserialize(m.payload(1), classOf[Heap], None) match {
|
ser.deserialize(m.payload(1), classOf[Heap]) match {
|
||||||
case Right(Heap(timestamp, used, max)) ⇒
|
case Right(Heap(timestamp, used, max)) ⇒
|
||||||
if ((used.toDouble / max) > 0.9) count += 1
|
if ((used.toDouble / max) > 0.9) count += 1
|
||||||
else count = 0
|
else count = 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue