This discussion is archived
1 Reply Latest reply: Jan 28, 2013 6:13 PM by pmackin RSS

Default / non-default POF Serializer

987379 Newbie
Currently Being Moderated
Hello,

I am working now on some training excercise to try working with pof serialization and I have encountered problem that I can't solve. In config I have serializer PersonSerializer set for remote scheme. I have also AnotherPersonSerializer with different behavior, set as default serializer. I test serialization by putting and getting Person objects to and from both caches. Serializers are made in such way that they always respond with object having one field set to specific value (not read from serialized object). I put object with value 222, PersonSerializer changes it to 111, AnotherPersonSerializer to 333. I see that serializer set for remote scheme is really working, but default serializer doesn't work (it isn't even listed in logs as loaded xml and value returned is still 222). Can you look at these files and see why aren't both serializers used?

To see these files with indents, go to http://wklej.org/hash/1193a8c22a1/

Client.java
package org.training;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

public class Client
{
     public static void main(String[] args)
     {
          CacheFactory.ensureCluster();
          NamedCache cache = CacheFactory.getCache("local-blabla");
          cache.put(0, new Person("A","B",222));
*//expect 111 here, but have 222*
          Person p1 = (Person) cache.get(0);
          System.out.println(p1.toString());
          NamedCache cache2 = CacheFactory.getCache("remote-blabla");
          cache2.put(0, new Person("A","B",222));
*//expect 333 here, and have it*
          Person p2 = (Person) cache2.get(0);
          System.out.println(p2.toString());
     }
}


coherence-cache-config.xml

<?xml version="1.0"?>


<!-- Note: This XML document is an example Coherence Cache Configuration
     deployment descriptor that should be customized (or replaced) for your particular
     caching requirements. The cache mappings and schemes declared in this descriptor
     are strictly for demonstration purposes and are not required. For detailed
     information on each of the elements that can be used in this descriptor please
     see the Coherence Cache Configuration deployment descriptor guide included
     in the Coherence distribution or the "Cache Configuration Elements" page
     on the Coherence Wiki (http://wiki.tangosol.com). -->

<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
     xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">
     <!-- The defaults element defines factory-wide default settings. -->
     <defaults>
          <!-- Note: This element defines the default serializer for all services
               defined within this cache configuration descriptor. Valid values include
               full serializer definitions, as well as named references to serializers defined
               within the "serializers" element of the operational configuration. Example
               values include: java, pof. Default value is java. -->
          <serializer>
               <instance>
                    <class-name>com.tangosol.io.pof.ConfigurablePofContext
                    </class-name>
                    <init-params>
                         <init-param>
                              <param-type>String</param-type>
                              <param-value>another-pof-config.xml</param-value>
                         </init-param>
                    </init-params>
               </instance>
          </serializer>

          <!-- Note: This element defines the default socket-provider for all Coherence
               Extend services defined within this cache configuration descriptor. Valid
               values include full socket-provider definitions, as well as named references
               to providers defined within the "socket-providers" element of the operational
               configuration. This setting only specifies the default socket-provider for
               Coherence Extend services; TCMP's socket-provider is specified within the
               "unicast-listener" of the operational configuration. Example values include:
               system, ssl. Default value is system. -->
          <socket-provider system-property="tangosol.coherence.socketprovider" />
     </defaults>

     <caching-scheme-mapping>

          <cache-mapping>
               <cache-name>local-*</cache-name>
               <scheme-name>example-object-backing-map</scheme-name>
          </cache-mapping>
          <cache-mapping>
               <cache-name>*</cache-name>
               <scheme-name>extend-dist</scheme-name>
          </cache-mapping>
     </caching-scheme-mapping>

     <caching-schemes>

          <!-- A scheme used by backing maps that may store data in object format
               and employ size limitation and/or expiry eviction policies. -->
          <local-scheme>
               <scheme-name>example-object-backing-map</scheme-name>

               <eviction-policy>HYBRID</eviction-policy>
               <high-units>{back-size-limit 0}</high-units>
               <expiry-delay>{back-expiry 1h}</expiry-delay>

               <cachestore-scheme></cachestore-scheme>
          </local-scheme>

          <remote-cache-scheme>
               <scheme-name>extend-dist</scheme-name>
               <service-name>ExtendTcpCacheService</service-name>
               <initiator-config>
                    <tcp-initiator>
                         <remote-addresses>
                              <socket-address>
                                   <address>localhost</address>
                                   <port>51234</port>
                              </socket-address>
                         </remote-addresses>
                         <connect-timeout>10s</connect-timeout>
                    </tcp-initiator>
                    <outgoing-message-handler>
                         <request-timeout>5s</request-timeout>
                    </outgoing-message-handler>
                    <serializer>
                         <instance>
                              <class-name>com.tangosol.io.pof.ConfigurablePofContext
                              </class-name>
                              <init-params>
                                   <init-param>
                                        <param-type>String</param-type>
                                        <param-value>custom-pof-config.xml</param-value>
                                   </init-param>
                              </init-params>
                         </instance>
                    </serializer>
               </initiator-config>
          </remote-cache-scheme>

          <remote-invocation-scheme>
               <scheme-name>extend-invocation</scheme-name>
               <service-name>ExtendTcpInvocationService</service-name>
               <initiator-config>
                    <tcp-initiator>
                         <remote-addresses>
                              <socket-address>
                                   <address>localhost</address>
                                   <port>51234</port>
                              </socket-address>
                         </remote-addresses>
                         <connect-timeout>10s</connect-timeout>
                    </tcp-initiator>
                    <outgoing-message-handler>
                         <request-timeout>5s</request-timeout>
                    </outgoing-message-handler>
                    <serializer>
                         <instance>
                              <class-name>com.tangosol.io.pof.ConfigurablePofContext
                              </class-name>
                              <init-params>
                                   <init-param>
                                        <param-type>String</param-type>
                                        <param-value>custom-pof-config.xml</param-value>
                                   </init-param>
                              </init-params>
                         </instance>
                    </serializer>
               </initiator-config>
          </remote-invocation-scheme>
     </caching-schemes>
</cache-config>

another-pof-config.xml

<?xml version="1.0"?>
<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config coherence-pof-config.xsd">
<user-type-list>
<!-- coherence POF user types -->
<include>coherence-pof-config.xml</include>

<user-type>
<type-id>1001</type-id>
<class-name>org.training.Person</class-name>
<serializer>
<class-name>org.training.AnotherPersonSerializer</class-name>
</serializer>
</user-type>
</user-type-list>

<allow-interfaces>true</allow-interfaces>
<allow-subclasses>true</allow-subclasses>
</pof-config>

custom-pof-config.xml

<?xml version="1.0"?>
<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config coherence-pof-config.xsd">
<user-type-list>
<!-- coherence POF user types -->
<include>coherence-pof-config.xml</include>

<user-type>
<type-id>1001</type-id>
<class-name>org.training.Person</class-name>
<serializer>
ONLY DIFFERENT SERIALIZER CLASS USED
<class-name>org.training.PersonSerializer</class-name>
</serializer>
</user-type>
</user-type-list>

<allow-interfaces>true</allow-interfaces>
<allow-subclasses>true</allow-subclasses>
</pof-config>

PersonSerializer.java

package org.training;
import java.io.IOException;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofSerializer;
import com.tangosol.io.pof.PofWriter;


public class PersonSerializer implements PofSerializer
{
     @Override
     public Object deserialize(PofReader in) throws IOException
     {
          String name = (String) in.readString(0);
          String surname = (String) in.readString(1);
          in.readInt(2);
DEFAULT BEHAVIOR
          int someValue = 111;

          in.readRemainder();
          return new Person(name, surname, someValue);
     }

     @Override
     public void serialize(PofWriter out, Object o) throws IOException
     {
          Person p = (Person) o;
          out.writeString(0, p.getName());
          out.writeString(1, p.getSurname());
          out.writeInt(2, p.getSomeValue());

          out.writeRemainder(null);
     }

}

AnotherPersonSerializer.java

package org.training;
import java.io.IOException;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofSerializer;
import com.tangosol.io.pof.PofWriter;


public class AnotherPersonSerializer implements PofSerializer
{
     @Override
     public Object deserialize(PofReader in) throws IOException
     {
          String name = (String) in.readString(0);
          String surname = (String) in.readString(1);
          in.readInt(2);
HERE WE HAVE DIFFERENT BEHAVIOR
          int someValue = 333;

          in.readRemainder();
          return new Person(name, surname, someValue);
     }

     @Override
     public void serialize(PofWriter out, Object o) throws IOException
     {
          Person p = (Person) o;
          out.writeString(0, p.getName());
          out.writeString(1, p.getSurname());
          out.writeInt(2, p.getSomeValue());

          out.writeRemainder(null);
     }

}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points