1 Reply Latest reply on May 25, 2017 9:37 AM by Gregory Orciuch

    How to Register Marshaller and Schema for pojo containing LinkedHashMap<String,Object>

    Kumar Birari Newbie

      Hello Team,

       

      I am using infinispan server 8.2.4.

      I need to store the data into RemoteCache<String, Employee> where Employee class contains one instance variable i.e LinkedHashMap<String, InnerEmployee > pair = new LinkedHashMap<String,InnerEmployee>();

      I have created .proto file for the same and registered using ctx.registerProtoFiles().

      But I am facing difficulty while registering marshallers for linkedHashMap I can not use writer.writeCollection().

      How to register this marshaller containing LinkedHashMap ?

       

      1] createRemoteCacheManager :

       

      public RemoteCacheManager createRemoteCacheManager(String remoteAddress) throws TransportException{

              RemoteCacheManager remoteCacheManager = null;

              try {

                  ConfigurationBuilder clientBuilder = new ConfigurationBuilder();

                  Properties properties = getConfigurationProperties();

                  properties.put("infinispan.client.hotrod.server_list", remoteAddress + ":11222");

                  clientBuilder.connectionPool().withProperties(properties);

                  clientBuilder.marshaller(new ProtoStreamMarshaller());

                  remoteCacheManager = new RemoteCacheManager(clientBuilder.build());

                  registerSchemasAndMarshallers(remoteCacheManager);

              }catch (TransportException exception) {

                  LOG.error("TransportException in createRemoteCacheManager : " + exception.getMessage());

                  throw new TransportException("TransportException",new InetSocketAddress(11222));

              }catch (Exception exception) {

                  LOG.error("Exception in createRemoteCacheManager : " + exception.getMessage());

              }

              return remoteCacheManager;

          }

       

      2] registerSchemasAndMarshallers :

       

         private void registerSchemasAndMarshallers(RemoteCacheManager remoteCacheManager) throws IOException {

              SerializationContext ctx = ProtoStreamMarshaller.getSerializationContext(remoteCacheManager);

              ctx.registerProtoFiles(FileDescriptorSource.fromResources(InfinispanConstant.EMPLOYEE_RESOURCE));

        

              ctx.registerMarshaller(new EmployeeMarshaller());

              ctx.registerMarshaller(new InnerEmployeeMarshaller());

        

              // Register the schemas with the server too

              RemoteCache<String, String> metadataCache = remoteCacheManager.getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);

              metadataCache.put(InfinispanConstant.EMPLOYEE_RESOURCE, readResource(InfinispanConstant.EMPLOYEE_RESOURCE));

              String errors = metadataCache.get(ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX);

              if (errors != null) {

                  throw new IllegalStateException("Some Protobuf schema files contain errors:\n" + errors);

              }

          }

       

      3] Employee.java

      import java.util.LinkedHashMap;

       

      public class Employee {

          private LinkedHashMap<String, InnerEmployee> pair;

       

          public Employee() {

              this.pair = new LinkedHashMap<String, Employee.InnerEmployee>();

          }

       

          public LinkedHashMap<String, InnerEmployee> getPair() {

              return pair;

          }

       

          public void setPair(LinkedHashMap<String, InnerEmployee> pair) {

              this.pair = pair;

          }

       

          public class InnerEmployee {

              private String empNo;

              private String name;

              private String dateOfBirth;

       

              public String getEmpNo() {

                  return empNo;

              }

       

              public void setEmpNo(String empNo) {

                  this.empNo = empNo;

              }

       

              public String getName() {

                  return name;

              }

       

              public void setName(String name) {

                  this.name = name;

              }

       

              public String getDateOfBirth() {

                  return dateOfBirth;

              }

       

              public void setDateOfBirth(String dateOfBirth) {

                  this.dateOfBirth = dateOfBirth;

              }

       

              @Override

              public String toString() {

                  return "Employee [empNo=" + empNo + ", name=" + name

                          + ", dateOfBirth=" + dateOfBirth + "]";

              }

          }

      }

       

      4] EMPLOYEE_RESOURCE.proto

      option java_package = "com.auruspay.infinispan.pojo";

      option java_outer_classname = "Employee";

      /*

      @Indexed(true)

      */

      message Employee {

          repeated MapFieldEntry pairs = 1;

      }

      message MapFieldEntry {

        optional string key = 1;

        optional InnerEmployee value = 2;

      }

      message InnerEmployee {

             optional string empNo = 1;

          optional string name = 2;

          optional string dateOfBirth = 3;

      }

       

      5] InnerEmployeeMarshaller.java

       

      package com.auruspay.infinispan.marshaller;

       

      import java.io.IOException;

      import org.infinispan.protostream.MessageMarshaller;

      import com.auruspay.infinispan.pojo.Employee;

       

      public class InnerEmployeeMarshaller implements MessageMarshaller<Employee.InnerEmployee> {

       

          @Override

          public Class<? extends Employee.InnerEmployee> getJavaClass() {

              return Employee.InnerEmployee.class;

          }

       

          @Override

          public String getTypeName() {

              return "com.auruspay.infinispan.proto.InnerEmployee";

          }

       

          @Override

          public Employee.InnerEmployee readFrom(ProtoStreamReader reader) throws IOException {

              Employee employee = new Employee();

              Employee.InnerEmployee emp = employee. new InnerEmployee();

              emp.setEmpNo(reader.readString("empNo"));

              emp.setName(reader.readString("name"));

              emp.setDateOfBirth(reader.readString("dateOfBirth"));

              return emp;

          }

       

          @Override

          public void writeTo(ProtoStreamWriter writer,Employee.InnerEmployee emp) throws IOException {

              writer.writeString("empNo", emp.getEmpNo());

              writer.writeString("name", emp.getName());

              writer.writeString("dateOfBirth", emp.getDateOfBirth());

          }

      }

      6] How EmployeeMarshaller should be designed ?

       

      Thanks In Advance !!!