com.alipay.remoting.serialization.SerializerManager Java Examples

The following examples show how to use com.alipay.remoting.serialization.SerializerManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CounterServiceImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
private void applyOperation(final CounterOperation op, final CounterClosure closure) {
    if (!isLeader()) {
        handlerNotLeaderError(closure);
        return;
    }

    try {
        closure.setCounterOperation(op);
        final Task task = new Task();
        task.setData(ByteBuffer.wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(op)));
        task.setDone(closure);
        this.counterServer.getNode().apply(task);
    } catch (CodecException e) {
        String errorMsg = "Fail to encode CounterOperation";
        LOG.error(errorMsg, e);
        closure.failure(errorMsg, StringUtils.EMPTY);
        closure.run(new Status(RaftError.EINTERNAL, errorMsg));
    }
}
 
Example #2
Source File: CommandCodec.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * encode the command,returns the byte array.
 * @param obj
 * @return
 */
public static byte[] encodeCommand(Object obj) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(obj);
    } catch (final CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #3
Source File: CommandCodec.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Decode the command object from byte array.
 * @param content
 * @param clazz
 * @return
 */
public static <T> T decodeCommand(byte[] content, Class<T> clazz) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).deserialize(content, clazz.getName());
    } catch (final CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #4
Source File: CommandCodec.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * encode the command,returns the byte array.
 * @param obj
 * @return
 */
public static byte[] encodeCommand(Object obj) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(obj);
    } catch (CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #5
Source File: CommandCodec.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Decode the command object from byte array.
 * @param content
 * @param clazz
 * @return
 */
public static <T> T decodeCommand(byte[] content, Class<T> clazz) {
    try {
        return SerializerManager.getSerializer(SerializerManager.Hessian2).deserialize(content,
            clazz.getName());
    } catch (CodecException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #6
Source File: DBRequestProcessor.java    From KitDB with Apache License 2.0 5 votes vote down vote up
public void handle(DBCommandChunk dbCommandChunk) throws CodecException, InterruptedException, KitDBException {
    final DBClosure closure = new DBClosure();
    closure.setChunk(dbCommandChunk);
    final Task task = new Task();
    task.setDone(closure);
    task.setData(ByteBuffer
            .wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(dbCommandChunk)));
    kitRaft.getNode().apply(task);
    synchronized (closure) {
        closure.wait();
    }
    if (closure.getCode() != 0) {
        throw new KitDBException(ErrorType.STROE_ERROR, closure.getMsg());
    }
}
 
Example #7
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * test DeserializationException when deserial request
 * @throws Exception
 */
@Test
public void testRequestDeserialException() throws Exception {
    System.setProperty(Configs.SERIALIZER, Byte.toString(SerializerManager.Hessian2));
    ExceptionRequestBodyCustomSerializer s1 = new ExceptionRequestBodyCustomSerializer(false,
        false, true, false);
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody body = new RequestBody(1, "hello world!");
    String ret = null;
    try {
        ret = (String) client.invokeSync(addr, body, 1000);
        Assert.fail("Should not reach here!");
    } catch (DeserializationException e) {
        logger.error("", e);
        Assert.assertTrue(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertTrue(s1.isDeserialized());
        Assert.assertFalse(s2.isSerialized());
        Assert.assertFalse(s2.isDeserialized());
    } catch (Throwable t) {
        Assert.fail("Should not reach here!");
    }
}
 
Example #8
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * test RuntimeException when deserial request
 * @throws Exception
 */
@Test
public void testRequestDeserialRuntimeException() throws Exception {
    System.setProperty(Configs.SERIALIZER, Byte.toString(SerializerManager.Hessian2));
    ExceptionRequestBodyCustomSerializer s1 = new ExceptionRequestBodyCustomSerializer(false,
        false, false, true);
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody body = new RequestBody(1, "hello world!");
    String ret = null;
    try {
        ret = (String) client.invokeSync(addr, body, 1000);
        Assert.fail("Should not reach here!");
    } catch (DeserializationException e) {
        logger.error("", e);
        Assert.assertTrue(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertTrue(s1.isDeserialized());
        Assert.assertFalse(s2.isSerialized());
        Assert.assertFalse(s2.isDeserialized());
    } catch (Throwable t) {
        Assert.fail("Should not reach here!");
    }
}
 
Example #9
Source File: CounterStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onApply(final Iterator iter) {
    while (iter.hasNext()) {
        long current = 0;
        CounterOperation counterOperation = null;

        CounterClosure closure = null;
        if (iter.done() != null) {
            // This task is applied by this node, get value from closure to avoid additional parsing.
            closure = (CounterClosure) iter.done();
            counterOperation = closure.getCounterOperation();
        } else {
            // Have to parse FetchAddRequest from this user log.
            final ByteBuffer data = iter.getData();
            try {
                counterOperation = SerializerManager.getSerializer(SerializerManager.Hessian2).deserialize(
                    data.array(), CounterOperation.class.getName());
            } catch (final CodecException e) {
                LOG.error("Fail to decode IncrementAndGetRequest", e);
            }
        }
        if (counterOperation != null) {
            switch (counterOperation.getOp()) {
                case GET:
                    current = this.value.get();
                    LOG.info("Get value={} at logIndex={}", current, iter.getIndex());
                    break;
                case INCREMENT:
                    final long delta = counterOperation.getDelta();
                    final long prev = this.value.get();
                    current = this.value.addAndGet(delta);
                    LOG.info("Added value={} by delta={} at logIndex={}", prev, delta, iter.getIndex());
                    break;
            }

            if (closure != null) {
                closure.success(current);
                closure.run(Status.OK());
            }
        }
        iter.next();
    }
}