コード例 #1
0
 /**
  * Registers proto files and marshallers.
  *
  * @param ctx the serialization context
  * @throws org.infinispan.protostream.DescriptorParserException if a proto definition file fails
  *     to parse correctly
  * @throws IOException if proto file registration fails
  */
 public static void registerMarshallers(SerializationContext ctx) throws IOException {
   ctx.registerProtoFiles(FileDescriptorSource.fromResources(PROTOBUF_RES));
   ctx.registerMarshaller(new UserMarshaller());
   ctx.registerMarshaller(new GenderMarshaller());
   ctx.registerMarshaller(new AddressMarshaller());
   ctx.registerMarshaller(new AccountMarshaller());
   ctx.registerMarshaller(new TransactionMarshaller());
 }
  @BeforeClass
  protected void registerProtobufSchema() throws Exception {
    // initialize client-side serialization context
    String authorSchemaFile =
        "/* @Indexed */\n"
            + "message Author {\n"
            + "   required int32 id = 1;\n"
            + "   /* @IndexedField */\n"
            + "   required string name = 2;\n"
            + "}";
    SerializationContext serializationContext =
        ProtoStreamMarshaller.getSerializationContext(remoteCacheManager);
    serializationContext.registerProtoFiles(
        FileDescriptorSource.fromString("author.proto", authorSchemaFile));
    serializationContext.registerMarshaller(
        new MessageMarshaller<Author>() {
          @Override
          public Author readFrom(ProtoStreamReader reader) throws IOException {
            int id = reader.readInt("id");
            String name = reader.readString("name");
            Author author = new Author();
            author.setId(id);
            author.setName(name);
            return author;
          }

          @Override
          public void writeTo(ProtoStreamWriter writer, Author author) throws IOException {
            writer.writeInt("id", author.getId());
            writer.writeString("name", author.getName());
          }

          @Override
          public Class<? extends Author> getJavaClass() {
            return Author.class;
          }

          @Override
          public String getTypeName() {
            return "Author";
          }
        });

    ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder();
    String memoSchemaFile =
        protoSchemaBuilder.fileName("memo.proto").addClass(Memo.class).build(serializationContext);

    // initialize server-side serialization context
    RemoteCache<String, String> metadataCache =
        remoteCacheManager.getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);
    metadataCache.put("author.proto", authorSchemaFile);
    metadataCache.put("memo.proto", memoSchemaFile);
    assertFalse(metadataCache.containsKey(ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX));
  }