当前位置: 首页>>代码示例>>Java>>正文


Java AbstractMessage类代码示例

本文整理汇总了Java中org.hawkular.bus.common.AbstractMessage的典型用法代码示例。如果您正苦于以下问题:Java AbstractMessage类的具体用法?Java AbstractMessage怎么用?Java AbstractMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


AbstractMessage类属于org.hawkular.bus.common包,在下文中一共展示了AbstractMessage类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: deserialize

import org.hawkular.bus.common.AbstractMessage; //导入依赖的package包/类
/**
 * Deserializes a JSON string in {@link #toHawkularFormat(BasicMessage) Hawkular format}.
 * The JSON object is returned.
 *
 * @param nameAndJson the string to be deserialized
 * @return the object represented by the JSON
 */
public <T extends BasicMessage> BasicMessageWithExtraData<T> deserialize(String nameAndJson) {
    String[] nameAndJsonArray = fromHawkularFormat(nameAndJson);
    String name = nameAndJsonArray[0];
    String json = nameAndJsonArray[1];

    // The name is the actual name of the POJO that is used to deserialize the JSON.
    // If not fully qualified with a package then assume it is in our package.
    if (name.indexOf(".") == -1) {
        name = String.format("%s.%s", API_PKG, name);
    }

    try {
        Class<T> pojo = (Class<T>) Class.forName(name);
        return new BasicMessageWithExtraData(AbstractMessage.fromJSON(json, pojo), null);
    } catch (Exception e) {
        throw new RuntimeException("Cannot deserialize: [" + nameAndJson + "]", e);
    }
}
 
开发者ID:hawkular,项目名称:hawkular-commons,代码行数:26,代码来源:ApiDeserializer.java

示例2: testReadFromInputStreamWithNoExtraData

import org.hawkular.bus.common.AbstractMessage; //导入依赖的package包/类
@Test
public void testReadFromInputStreamWithNoExtraData() throws IOException {
    ApiDeserializer ad = new ApiDeserializer();

    String nameAndJson = EchoRequest.class.getName() + "={\"echoMessage\":\"msg\"}";
    ByteArrayInputStream in = new UncloseableByteArrayInputStream(nameAndJson.getBytes());

    BasicMessageWithExtraData<AbstractMessage> map = ad.deserialize(in);
    AbstractMessage request = map.getBasicMessage();
    Assert.assertTrue(request instanceof EchoRequest);
    EchoRequest echoRequest = (EchoRequest) request;
    Assert.assertEquals("msg", echoRequest.getEchoMessage());

    // now make sure the stream is empty
    BinaryData leftover = map.getBinaryData();
    Assert.assertEquals(0, leftover.available());
    Assert.assertEquals(0, in.available());
}
 
开发者ID:hawkular,项目名称:hawkular-commons,代码行数:19,代码来源:ApiDeserializerTest.java

示例3: testReadFromInputStreamWithExtraData

import org.hawkular.bus.common.AbstractMessage; //导入依赖的package包/类
@Test
public void testReadFromInputStreamWithExtraData() throws IOException {
    // tests that this can extract the JSON even if more data follows in the stream
    ApiDeserializer ad = new ApiDeserializer();

    String nameAndJson = EchoRequest.class.getName() + "={\"echoMessage\":\"msg\"}";
    String extra = "This is some extra data";
    String nameAndJsonPlusExtra = nameAndJson + extra;

    ByteArrayInputStream in = new UncloseableByteArrayInputStream(nameAndJsonPlusExtra.getBytes());

    BasicMessageWithExtraData<AbstractMessage> map = ad.deserialize(in);
    AbstractMessage request = map.getBasicMessage();
    Assert.assertTrue(request instanceof EchoRequest);
    EchoRequest echoRequest = (EchoRequest) request;
    Assert.assertEquals("msg", echoRequest.getEchoMessage());

    // now make sure the stream still has our extra data that we can read now
    BinaryData leftover = map.getBinaryData();
    byte[] leftoverByteArray = new byte[leftover.available()];
    leftover.read(leftoverByteArray);

    String totalRemaining = new String(leftoverByteArray, "UTF-8");
    Assert.assertEquals(extra.length(), totalRemaining.length());
    Assert.assertEquals(extra, totalRemaining);

    // as a quick test, show that an exception results if we give bogus data in the input stream
    in = new UncloseableByteArrayInputStream("this is not valid data".getBytes());
    try {
        ad.deserialize(in);
        Assert.fail("Should have thrown an exception - the stream had invalid data");
    } catch (Exception expected) {
    }
}
 
开发者ID:hawkular,项目名称:hawkular-commons,代码行数:35,代码来源:ApiDeserializerTest.java


注:本文中的org.hawkular.bus.common.AbstractMessage类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。