Java Code Examples for com.alipay.remoting.CustomSerializerManager#registerCustomSerializer()

The following examples show how to use com.alipay.remoting.CustomSerializerManager#registerCustomSerializer() . 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: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * normal test
 * @throws Exception
 */
@Test
public void testNormalCustomSerializer() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    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 = (String) client.invokeSync(addr, body, 1000);
    Assert.assertEquals(RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM", ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());
    Assert.assertTrue(s2.isSerialized());
    Assert.assertTrue(s2.isDeserialized());
}
 
Example 2
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test custom serializer using invoke contxt in sync
 *
 * @throws Exception
 */
@Test
public void testInvokeContextCustomSerializer_SYNC() throws Exception {
    NormalRequestBodyCustomSerializer_InvokeContext s1 = new NormalRequestBodyCustomSerializer_InvokeContext();
    NormalStringCustomSerializer_InvokeContext s2 = new NormalStringCustomSerializer_InvokeContext();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody body = new RequestBody(1, "hello world!");
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE1_value);
    String ret = (String) client.invokeSync(addr, body, invokeContext, 1000);
    Assert.assertEquals(RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM", ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());

    invokeContext.clear();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE2_value);
    ret = (String) client.invokeSync(addr, body, invokeContext, 1000);
    Assert.assertEquals(NormalStringCustomSerializer_InvokeContext.UNIVERSAL_RESP, ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());
}
 
Example 3
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test RuntimeException when deserial response
 * @throws Exception
 */
@Test
public void testResponseDeserialzeRuntimeException() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    ExceptionStringCustomSerializer s2 = new ExceptionStringCustomSerializer(false, false,
        false, true);
    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.assertFalse(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertTrue(s1.isDeserialized());
        Assert.assertTrue(s2.isSerialized());
        Assert.assertTrue(s2.isDeserialized());
    } catch (Throwable t) {
        Assert.fail("Should not reach here!");
    }
}
 
Example 4
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test DeserializationException when deserial response
 * @throws Exception
 */
@Test
public void testResponseDeserialzeException() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    ExceptionStringCustomSerializer s2 = new ExceptionStringCustomSerializer(false, false,
        true, false);
    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.assertFalse(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertTrue(s1.isDeserialized());
        Assert.assertTrue(s2.isSerialized());
        Assert.assertTrue(s2.isDeserialized());
    } catch (Throwable t) {
        Assert.fail("Should not reach here!");
    }
}
 
Example 5
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test RuntimeException when serial response
 * @throws Exception
 */
@Test
public void testResponseSerialRuntimeException() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    ExceptionStringCustomSerializer s2 = new ExceptionStringCustomSerializer(false, true,
        false, false);
    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 (SerializationException e) {
        logger.error("", e);
        Assert.assertTrue(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertTrue(s1.isDeserialized());
        Assert.assertTrue(s2.isSerialized());
        Assert.assertFalse(s2.isDeserialized());
    } catch (Throwable t) {
        Assert.fail("Should not reach here!");
    }
}
 
Example 6
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test SerializationException when serial response
 * @throws Exception
 */
@Test
public void testResponseSerialException() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    ExceptionStringCustomSerializer s2 = new ExceptionStringCustomSerializer(true, false,
        false, false);
    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 (SerializationException e) {
        logger.error("", e);
        Assert.assertTrue(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertTrue(s1.isDeserialized());
        Assert.assertTrue(s2.isSerialized());
        Assert.assertFalse(s2.isDeserialized());
    } catch (Throwable t) {
        Assert.fail("Should not reach here!");
    }
}
 
Example 7
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test RuntimeException when serial request
 * @throws Exception
 */
@Test
public void testRequestSerialRuntimeException() throws Exception {
    ExceptionRequestBodyCustomSerializer s1 = new ExceptionRequestBodyCustomSerializer(false,
        true, false, 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 (SerializationException e) {
        logger.error("", e);
        Assert.assertFalse(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertFalse(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 6 votes vote down vote up
/**
 * test SerializationException when serial request
 * @throws Exception
 */
@Test
public void testRequestSerialException() throws Exception {
    ExceptionRequestBodyCustomSerializer s1 = new ExceptionRequestBodyCustomSerializer(true,
        false, false, 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 (SerializationException e) {
        logger.error("", e);
        Assert.assertFalse(e.isServerSide());
        Assert.assertEquals(null, ret);
        Assert.assertTrue(s1.isSerialized());
        Assert.assertFalse(s1.isDeserialized());
        Assert.assertFalse(s2.isSerialized());
        Assert.assertFalse(s2.isDeserialized());
    } catch (Throwable t) {
        Assert.fail("Should not reach here!");
    }
}
 
Example 9
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 10
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 11
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * test custom serializer using invoke contxt in future
 * @throws Exception
 */
@Test
public void testInvokeContextCustomSerializer_FUTURE() throws Exception {
    NormalRequestBodyCustomSerializer_InvokeContext s1 = new NormalRequestBodyCustomSerializer_InvokeContext();
    NormalStringCustomSerializer_InvokeContext s2 = new NormalStringCustomSerializer_InvokeContext();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody body = new RequestBody(1, "hello world!");
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE1_value);
    RpcResponseFuture future = client.invokeWithFuture(addr, body, invokeContext, 1000);
    String ret = (String) future.get(1000);
    Assert.assertEquals(RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM", ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());

    invokeContext.clear();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE2_value);
    future = client.invokeWithFuture(addr, body, invokeContext, 1000);
    ret = (String) future.get(1000);
    Assert.assertEquals(NormalStringCustomSerializer_InvokeContext.UNIVERSAL_RESP, ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());
}
 
Example 12
Source File: CustomSerializerCodecTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneway() throws InterruptedException {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody req = new RequestBody(2, "hello world oneway");
    for (int i = 0; i < invokeTimes; i++) {
        try {
            byte testCodec = (byte) i;
            InvokeContext invokeContext = new InvokeContext();
            invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, testCodec);
            client.oneway(addr, req, invokeContext);

            Assert.assertEquals(testCodec, s1.getContentSerializer());
            Assert.assertEquals(-1, s2.getContentSerializer());
            Thread.sleep(100);
        } catch (RemotingException e) {
            String errMsg = "RemotingException caught in oneway!";
            logger.error(errMsg, e);
            Assert.fail(errMsg);
        }
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 13
Source File: SofaRpcSerializationRegister.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * we can override or rewrite the method
 */
protected static void innerRegisterCustomSerializer() {
    // 注册序列化器到bolt
    if (CustomSerializerManager.getCustomSerializer(SofaRequest.class.getName()) == null) {
        CustomSerializerManager.registerCustomSerializer(SofaRequest.class.getName(),
            RPC_SERIALIZATION);
    }
    if (CustomSerializerManager.getCustomSerializer(SofaResponse.class.getName()) == null) {
        CustomSerializerManager.registerCustomSerializer(SofaResponse.class.getName(),
            RPC_SERIALIZATION);
    }
}
 
Example 14
Source File: BoltRaftRpcFactory.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void registerProtobufSerializer(final String className, final Object... args) {
    CustomSerializerManager.registerCustomSerializer(className, ProtobufSerializer.INSTANCE);
}
 
Example 15
Source File: CustomSerializerCodecTest.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testServerSyncUsingConnection() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    Connection clientConn = client.createStandaloneConnection(ip, port, 1000);

    for (int i = 0; i < invokeTimes; i++) {
        byte testCodec = (byte) i;
        InvokeContext invokeContext = new InvokeContext();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, testCodec);

        RequestBody req1 = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
        String serverres = (String) client.invokeSync(clientConn, req1, invokeContext, 1000);
        Assert.assertEquals(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec, s1.getContentSerializer());
        Assert.assertEquals(testCodec, s2.getContentSerializer());

        Assert.assertNotNull(serverConnectProcessor.getConnection());
        Connection serverConn = serverConnectProcessor.getConnection();

        invokeContext.clear();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, (byte) (testCodec + 1));
        s1.reset();
        s2.reset();

        RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
        String clientres = (String) server.getRpcServer().invokeSync(serverConn, req,
            invokeContext, 1000);
        Assert.assertEquals(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec + 1, s1.getContentSerializer());
        Assert.assertEquals(testCodec + 1, s2.getContentSerializer());
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 16
Source File: CustomSerializerCodecTest.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testServerSyncUsingAddress() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    Connection clientConn = client.createStandaloneConnection(ip, port, 1000);
    String remote = RemotingUtil.parseRemoteAddress(clientConn.getChannel());
    String local = RemotingUtil.parseLocalAddress(clientConn.getChannel());
    logger.warn("Client say local:" + local);
    logger.warn("Client say remote:" + remote);

    for (int i = 0; i < invokeTimes; i++) {
        byte testCodec = (byte) i;
        InvokeContext invokeContext = new InvokeContext();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, testCodec);

        RequestBody req1 = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
        String serverres = (String) client.invokeSync(clientConn, req1, invokeContext, 1000);
        Assert.assertEquals(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec, s1.getContentSerializer());
        Assert.assertEquals(testCodec, s2.getContentSerializer());

        invokeContext.clear();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, (byte) (testCodec + 1));
        s1.reset();
        s2.reset();

        Assert.assertNotNull(serverConnectProcessor.getConnection());
        // only when client invoked, the remote address can be get by UserProcessor
        // otherwise, please use ConnectionEventProcessor
        String remoteAddr = serverUserProcessor.getRemoteAddr();
        RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
        String clientres = (String) server.getRpcServer().invokeSync(remoteAddr, req,
            invokeContext, 1000);
        Assert.assertEquals(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec + 1, s1.getContentSerializer());
        Assert.assertEquals(testCodec + 1, s2.getContentSerializer());
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 17
Source File: BasicUsage_ExecutorSelector_Test.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void classInit() {
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(),
        new CustomHeaderSerializer());
}