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 scala.util.DynamicVariable
|
||||
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 scala.collection.mutable.ArrayBuffer
|
||||
import java.io.NotSerializableException
|
||||
|
|
@ -151,7 +153,7 @@ class Serialization(val system: ExtendedActorSystem) extends Extension {
|
|||
*/
|
||||
private[akka] val bindings: Seq[ClassSerializer] = {
|
||||
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))
|
||||
}
|
||||
sort(configuredBindings)
|
||||
|
|
|
|||
|
|
@ -52,6 +52,16 @@ trait Serializer {
|
|||
* the class should be loaded using ActorSystem.dynamicAccess.
|
||||
*/
|
||||
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).
|
||||
*/
|
||||
abstract class JSerializer extends Serializer {
|
||||
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef =
|
||||
fromBinary(bytes, manifest.orNull)
|
||||
final def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef =
|
||||
fromBinaryJava(bytes, manifest.orNull)
|
||||
|
||||
/**
|
||||
* This method should be overridden,
|
||||
* manifest and classLoader may be null.
|
||||
* This method must be implemented, manifest may be null.
|
||||
*/
|
||||
def fromBinary(bytes: Array[Byte], manifest: Class[_]): AnyRef
|
||||
protected def fromBinaryJava(bytes: Array[Byte], manifest: Class[_]): AnyRef
|
||||
}
|
||||
|
||||
object NullSerializer extends NullSerializer
|
||||
|
|
|
|||
|
|
@ -44,9 +44,8 @@ public class SerializationDocTestBase {
|
|||
|
||||
// "fromBinary" deserializes the given array,
|
||||
// using the type hint (if any, see "includeManifest" above)
|
||||
// into the optionally provided classLoader.
|
||||
@Override public Object fromBinary(byte[] bytes,
|
||||
Class clazz) {
|
||||
@Override public Object fromBinaryJava(byte[] bytes,
|
||||
Class<?> clazz) {
|
||||
// Put your code that deserializes here
|
||||
//#...
|
||||
return null;
|
||||
|
|
@ -74,7 +73,7 @@ public class SerializationDocTestBase {
|
|||
|
||||
// Turn it back into an object,
|
||||
// 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á!
|
||||
assertEquals(original, back);
|
||||
|
|
|
|||
|
|
@ -71,14 +71,14 @@ object ZeromqDocSpec {
|
|||
def receive = {
|
||||
// the first frame is the topic, second is the message
|
||||
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)) ⇒
|
||||
log.info("Used heap {} bytes, at {}", used, timestampFormat.format(new Date(timestamp)))
|
||||
case Left(e) ⇒ throw e
|
||||
}
|
||||
|
||||
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)) ⇒
|
||||
log.info("Load average {}, at {}", loadAverage, timestampFormat.format(new Date(timestamp)))
|
||||
case Left(e) ⇒ throw e
|
||||
|
|
@ -97,7 +97,7 @@ object ZeromqDocSpec {
|
|||
def receive = {
|
||||
// the first frame is the topic, second is the message
|
||||
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)) ⇒
|
||||
if ((used.toDouble / max) > 0.9) count += 1
|
||||
else count = 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue